August 26, 2011 at 5:32 am
Hi everyone,
I am using dynamic query.
Declare @testquery varchar(1000),
@test-2 varchar(200)
set @test-2 = '*Ma.01.01'
select @testquery =
'
select code,id from public where code like '% +@test + %' '
exec (@testquery)
I am getting following error
Msg 402, Level 16, State 1, Line 3
The data types %s and %s are incompatible in the %s operator
Could anyone help me to process how to use LIKE with table variable..
Thanks,
Tony
August 26, 2011 at 5:36 am
I don't see a table variable anywhere there. What's the definition of the table 'public'?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 26, 2011 at 5:38 am
I have mentioned the table for the example..
Thanks,
tony
August 26, 2011 at 5:40 am
I still don't see a table variable anywhere, nor is there any definition (create table) for the table public.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 26, 2011 at 5:44 am
@test-2 is the table variable.
Public table contains two columns.(Testcode and testid)
Using dynamic query
select @testquery =
'select testcode,testid from public where testcode like '% + @test-2 +%' '
exec (@testquery)
--testcode like '% + @test-2 +%' ' -- this Like condition is not working with table variable (@test)
August 26, 2011 at 5:52 am
Test is not a table variable. It's a scalar variable, a 200 character varchar.
Declare @testquery varchar(1000),
@test varchar(200)
set @test-2 = '*Ma.01.01'
Can you post the CREATE TABLE statement for the table public please.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 26, 2011 at 5:55 am
CREATE TABLE PUBLIC
(
TestId INT,
TestCode Nvarchar(100)
)
August 26, 2011 at 6:00 am
Your code was a mess of string and quote-related errors.
With the table you gave me, this works.
Declare @testquery varchar(1000),
@test-2 varchar(200)
set @test-2 = '*Ma.01.01'
select @testquery = 'select TestCode, TestId from [public] where TestCode like ''%' +@test + '%'' '
exec (@testquery)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 26, 2011 at 6:03 am
tonyarp05 61903 (8/26/2011)
Hi everyone,Declare @testquery varchar(1000),
@test-2 varchar(200)
set @test-2 = '*Ma.01.01'
select @testquery =
'select code,id from public where code like '% +@test + %' '
I am getting following error
Msg 402, Level 16, State 1, Line 3
The data types %s and %s are incompatible in the %s operator
As Gail said, there are no table variables. Your problem is with how you're plugging your variable into the dynamic select statement.
Try this for the dynamic select, the wild card '%' should work appropriately.
'select code,id from public where code like ''%' + @test-2 + '%'''
August 26, 2011 at 6:08 am
Three minutes too slow on a Friday morning.
Tony,
A great way to troubleshoot dynamic SQL related errors is to print the statement instead of trying to execute it.
Change exec (@testquery)
to print @testquery
and you'll see the select statement that is trying to be executed. Sometimes it makes obvious otherwise mysterious errors.
August 26, 2011 at 6:12 am
This doesn't work, I am getting an error
Msg 402, Level 16, State 1, Line 369
The data types varchar and varchar are incompatible in the modulo operator.
August 26, 2011 at 6:17 am
I tested mine, it runs fine.
Post the exact code that is failing for you.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 26, 2011 at 6:18 am
HELLO I AM NEW MEMBER.. HELLO EVERY ONE
August 26, 2011 at 7:37 am
tonyarp05 61903 (8/26/2011)
This doesn't work, I am getting an errorMsg 402, Level 16, State 1, Line 369
The data types varchar and varchar are incompatible in the modulo operator.
That means that you still don't have the quotes right because "%" is both a wild-card and a mathematical operator. The code Gail posted works just fine.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply