Update with a variable

  • I am trying to get a query to work and running into a brick wall

    create procedure set_interface_flag

    @lnkey varchar(20),

    @Tname varchar(25),

    @fname varchar(25)

    as

    update @Tname

    set @fname = getdate()

    where lnkey = @lnkey

    it tells me I need to declare the @Tname variable. Any thoughts?

  • You need to use dynamic SQL, it won't automatically parse your table name in.

    create procedure set_interface_flag

    @lnkey varchar(20),

    @Tname varchar(25),

    @fname varchar(25)

    as

    declare @sql nvarchar ( 4000 )

    set @sql = 'update ' + @Tname + '

    set ' + @fname + ' = getdate()

    where lnkey = ''' + @lnkey + '''

    exec sp_executesql @sql

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

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