Hi. I want to create a stored procedure whereby the variable comes after the LIKE operator. For example, the code would look something like this:
CREATE PROCEDURE SPFIND
@VARIABLE1 VARCHAR
SELECT *
FROM TABLE1
WHERE FIELD1 LIKE '%@VARIABLE1%'
Then to execute the procedure,
EXEC SPFIND 'VARIABLENAME'
I can't get this to work using the LIKE operator. I would appreciate any help.
Software/Hardware used:
SQL Server 2005
ASKED:
June 7, 2010 12:38 PM
UPDATED:
June 8, 2010 7:26 PM
Are you getting an error? What is the error?
OR
does it just seem to run forever?
No. I just don’t get any results. The only way I’m able to get results is if I execute the stored procedure as shown below:
EXEC spFIND @variable1=’%variablename’
I think it should work by simply saying EXEC spFIND @variable1=’variablename’
Thank you for your help. I tried what you wrote and it worked just fine. Another solution I found that worked was this modification:
CREATE PROCEDURE SPFIND
@VARIABLE1 VARCHAR(20)
SELECT *
FROM TABLE1
WHERE FIELD1 LIKE LIKE ‘%’ + @VARIABLE1+ ‘%’
Then to execute I would just pass the variable like this:
EXEC VARIABLENAME