Using EXECUTE with a Character for UPDATE and INSERT

  • ,  I have another case where I need to use EXECUTE TO UPDATE certain table, and also to INSERT...

    DECLARE @tbl nvarchar(125), @lookupvalue nvarchar(125), @lookuporder nvarchar(125), @lvid nvarchar(125)

    I want to use those variable for UPDATE... using EXEC sp_executesql...

    SET @sql=('UPDATE @tbl SET fieldname=@fieldname, lookupvalue=@lookupvalue, lookuporder=@lookuporder WHERE lvid=@lvid')

      EXEC sp_executesql @sql, N'@lvid nvarchar(125), @lookupvalue nvarchar(125), @lookuporder numeric(9), @fieldname nvarchar(125), @tbl nvarchar(125)'....im not sure what code to fill in here.

    I will also use the same variables(except @lvid) for INSERT to @tbl.

    I just want my StoredProcedure to be flexible.

    Need Help...

  • You also need to learn sql programming. This is basically the worst possible way to use sql server's stored proc. You need to create one statement for each possible operation if you want to harnest the server's power.

  • I agree.  You either have the cat by the tailpiece, or you are giving us insufficient info, hehehe .

    Anyway, you can use at least some of this:

    Replace the BOLD RED with your values...

    DECLARE @sql nvarchar(4000),

     @fieldname nvarchar(50),

     @tbl nvarchar(125),

     @lookupvalue nvarchar(125),

     @lookuporder nvarchar(125),

     @lvid nvarchar(125)

    SET  @fieldname = 'FIELDNAME'

    set @tbl = 'TBL'

    set @lookupvalue = 'LOOKUPVALUE'

    set @lookuporder = 'LOOKUPORDER'

    set @lvid = 'LVID'

    SET  @sql = 'UPDATE ' + @tbl + ' SET fieldname=' + @fieldname + ', lookupvalue='

    set @sql = @sql + @lookupvalue + ', lookuporder=' + @lookuporder + ' WHERE lvid=' + @lvid

    print @sql --use this to check first

    EXEC @sql

     

     

     

    <hr noshade size='1' width='250' color='#BBC8E5'>Kindest Regards,

    Roelof
    <a href='http://' class='authorlink' target='_blank'></a>

    --There are only 10 types of people in the world. Those who understand binary, and those who don't.

  • Yup! it worked! thanks buddy. Actually, its not a sufficient info, my info are just enough thats why you(Roelof) created the solution. For me, if it is insufficient, you will be askin for more info. Anyway Guys, THANK YOU VERY MUCH.

  • Happy to help.

    i had a lucky guess, i guess...

    <hr noshade size='1' width='250' color='#BBC8E5'>Kindest Regards,

    Roelof
    <a href='http://' class='authorlink' target='_blank'></a>

    --There are only 10 types of people in the world. Those who understand binary, and those who don't.

  • Dude it's just not the way to program in sql, you relly need to get aquinaed with these article.

    The Curse and Blessings of Dynamic SQL

Viewing 6 posts - 1 through 5 (of 5 total)

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