Load variable with dynamic sql executed value

  • I am working on a moderately sophisticated product pricing algorithm.  A part of the solution is to take a dynamically built formula execute it to get a value that can be used in other parts of the algorithm.  I have seen this done before some years ago, but I have not been able to get the syntax.

    Here is a sample of what I need to do:

    declare @val as money

    declare @sql as varchar(1000)

    set @sql = 'select (329.02 * 1.2) * 1.20'

    exec @val = (@SQL)

    But this SQL server thinks I am looking for a procedure with this syntax.  I have tried a couple of variations to no avail.

    Thanks in advance for your help.

    David

  • You can try with sp_ExecuteSql:

    DECLARE @sql nvarchar(4000),  @val money

              

       SELECT @sql = 'select @val=(329.02 * 1.2) * 1.20'

       EXEC sp_executesql @sql, N'@Val money OUTPUT', @val OUTPUT

       SELECT @val

    Hope this may help you.

    Cheers,
    Prithiviraj Kulasingham

    http://preethiviraj.blogspot.com/

  • That works for what I am doing. 

    Thank you Preethiviraj Kulasingham for your help.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply