question

bluytelaar avatar image
bluytelaar asked

execute string stored in table

Hi, Can somebody please help me with the following problem: This t-sql code works fine: declare @temp nvarchar(4000) set @temp = 'select * from dbo.mytable where mycondition = ''mytext''' exec(@temp) However when I store the above string (select * from dbo.mytable where mycondition = ''mytext'')in a table (othertable, columnname = Query, type =nvarchar(4000) the following code gives an error: declare @temp nvarchar(4000) set @temp = (select Query from dbo.othertable where ID = 1) exec(@temp) Error: Incorrect syntax near ''. Please help how to execute a string (that has string condition) that is stored in a table? Thanks
execute
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
bluytelaar avatar image
bluytelaar answered
Found the answer already myself: The trick is that you store the following string in the table: select * from dbo.mytable where mycondition = 'mytext' instead of select * from dbo.mytable where mycondition = ''mytext''
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Glad to hear you have it worked out. Make sure you mark your answer as the correct one by clicking on the check box.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.