question

Wenjing avatar image
Wenjing asked

How to run expression in string format

Hello, I have created a table to store formulae for different projects. How can I convert data to expression in SQL to return value instead of formula?
common-table-expression
2 comments
10 |1200

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

Can you provide a closer information about the "expression" - I mean what kind of formulas you are storing in the table.
0 Likes 0 ·
A similar question here , but as Pavel suggests, we can better help if you provide more detail preferably with examples.
0 Likes 0 ·

1 Answer

·
Håkan Winther avatar image
Håkan Winther answered
Let's assume you have strings like "column1 * column2/column4" stored in a column called expression1,another string like "column4 < column5" in condition1, in tableX, you can create SQL statements in your select and execute it by sp_executeSQL, something like this: DECLARE @sql AS nvarchar(4000); DECLARE curr CURSOR FAST_FORWARD FOR SELECT 'SELECT ' + expression1 + ' FROM tableY WHERE ' + condition1 FROM tableX OPEN curr; FETCH NEXT FROM curr INTO @sql; WHILE @@FETCH_STATUS = 0 BEGIN EXEC sp_executeSQL @sql; FETCH NEXT FROM curr INTO @sql; END CLOSE curr; DEALLOCATE curr; GO This will give you a resultset from tableY for each record in the tableX where the condition is met. This is only a brief sample based on you "undefined" question. If you add more information, we could help you more.
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.

Thank you for your help. I used as suggested above and it worked well.
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.