question

SSC avatar image
SSC asked

execute formula stored in table

Hello, I want to understand how can I use the formula stored in text/varchar field ? Let me put an example : There is a tables say Table_A Table_a stores Codes, Description, Percentage and Base code. Tax code Description Percentage Base Code 100 Amount - - 101 Tax A 10% - 102 Tax B 0.03 % 101 While calculating the tax currently I put the tax calculation hard code logic in store proc i.e `((fltAmount*fltTaxA %)*fltTaxB %)` What I wantto achieve is I will store the formula `((fltAmount*fltTaxA %)*fltTaxB %)` into the table column having varchar data type. At the time of execution I will simply get the formula store it to a variable and evaluate the formula stored in string variable so it produces the required result. Some what similar behaviour can be achieved by microsoft scripting object. Is it possible to do it in sql ? If yes How? I hope I am clear in putting my idea   Thanks and best regards,  Milind Shevade
stored-proceduresdynamic-sql
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

·
Fatherjack avatar image
Fatherjack answered
I am not certain why you want to store the formula or where you want to store it. Do you want to put it into TableA? Why not just store the result of the formula in the table? You have a couple of options here as far as I see it, you can create a computed column that uses your formula to generate the value you want - see ([ http://msdn.microsoft.com/en-us/library/ms191250.aspx][1] - SQL 2008 R2 link as you dont specify your version) or, if you really want to store the formula and then use it in TSQL then you need to review and understand dynamic TSQL - see [ http://msdn.microsoft.com/en-us/library/ms188001.aspx][2] [1]: http://msdn.microsoft.com/en-us/library/ms191250.aspx [2]: http://msdn.microsoft.com/en-us/library/ms188001.aspx
10 |1200

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

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.