Set parameter to sql, query not working

I have this code here:

DECLARE @inputform varchar
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 LIKE '@inputform'

      

This won't give me the output I want, but when I like it, it works:

DECLARE @inputform varchar
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 LIKE 'J61807017B'

      

Any idea?

+3


source to share


1 answer


You need to specify the size of the variable and strip the quotes when you use it:

DECLARE @inputform varchar(20)
SET @inputform = 'J61807017B'

SELECT * FROM test where text1 = @inputform
-- or text1 like '%'+ @inputform +'%' if you want to do partial matching

      

If you do not specify a size for a char / varchar value, the size will be 1.



From the manual:

If n is not specified in the data definition or variable declaration, the default length is 1.

+6


source







All Articles