|
Hi there, I'm running MS SQL2008 and I want to store an SQL statement in the DB. The statement works OK in the query window: However I can't make it in the UPDATE... fails with "The data types varchar and varchar are incompatible in the modulo operator." Best Regards, Mikko
(comments are locked)
|
|
Try this then.... Hi Kev, SQL statement is stored perfectly in the table: SELECT * FROM LabelReport WHERE ReportID = @Var1 AND CompanyIdList LIKE '%' + @Var2 + '%' Unfortunately when I use it in my program I get: {"Incorrect syntax near the keyword 'LIKE'."}. If I execute the stored query in the query window it works perfectly. I'm totally lost now... Best regards, Mikko
Sep 09 '10 at 03:57 AM
mlipsane
how are you executing the sql from your 'program'?
Sep 09 '10 at 04:16 AM
Kev Riley ♦♦
Here is the executor, it works if I have only Vars without LIKE:
Sep 09 '10 at 04:26 AM
mlipsane
Ahh over to someone who can read that then .....I'm not a developer, just a SQL guy. What does the query look like if you capture it in profiler?
Sep 09 '10 at 04:33 AM
Kev Riley ♦♦
Thanks to everyone! I close this for my part as I use another solution. Anyone interested can dig this further. If you don't use LIKE then stored queries with variables is a nice way to have some programs which can be modified to a certain extent without touching the code. Mikko (who dislikes LIKE)
Sep 09 '10 at 05:28 AM
mlipsane
(comments are locked)
|
|
try this Hi Cyborg, Thank you for your answer! It removes the error, but I got a new error: Msg 137, Level 15, State 2, Line 2 Must declare the scalar variable "@CNT". The idea is to fetch the SQL statement from the table and give the input variable in my program. This seems to be hard though. Best regards, Mikko
Sep 09 '10 at 12:54 AM
mlipsane
Hi again, In the table I only have a query and it works fine if I e.g.: UPDATE LabelReport SET LabelQry = 'SELECT * FROM LabelReport WHERE Company = @Var1' The LabelQry gets stored in the table and sql server does not ask any declarations. The problem is with LIKE and '%'.
Sep 09 '10 at 01:04 AM
mlipsane
you should have to declare the variables used in your query DECLARE @CNT varchar(10) SET @CNT = '(3)' UPDATE LabelReport SET LabelQry = 'SELECT * FROM LabelReport WHERE CompanyIdList LIKE ''%'+@CNT+'%'''
Sep 09 '10 at 12:56 AM
Cyborg
Hi, Thanks again! I just want the SQL statement in the table. E.g. this works fine: UPDATE LabelReport SET LabelQry = 'SELECT * FROM LabelReport WHERE Company = @Var1' I can then use this query and assign @Var1 with a value. The problem seems to be with the LIKE '%' which fails.
Sep 09 '10 at 01:08 AM
mlipsane
I hope you should go through this article http://www.sqlteam.com/article/introduction-to-dynamic-sql-part-1
Sep 09 '10 at 01:21 AM
Cyborg
Hi all, I have chosen another strategy to solve this problem. If anyone gets solution to the execution problem I'm curious. Thanks to everyone! BR,
Sep 09 '10 at 05:05 AM
mlipsane
(comments are locked)
|

