Search code examples
sqlsql-server-2012table-valued-parameters

How to UPDATE/SET inside a function


This is a part of my code inside the table-valued function:

DECLARE @Temp1 table (tempid int not null, tasknr varchar(50) null, devcat varchar(50) null, taskop datetime null, taskcl datetime null)
(...)
DECLARE @Temp1a table (tempid int, tasknr nvarchar(50), cat nvarchar (50), taskop datetime, taskcl datetime)
(...)

UPDATE
        @Temp1
    SET 
        @Temp1.tasknr = @Temp1a.tasknr,
        @Temp1.devcat = @Temp1a.devcat,
        @Temp1.taskop = @Temp1a.taskop,
        @Temp1.taskcl = @Temp1a.taskcl
    FROM
        @Temp1a
    INNER JOIN
        @Temp1
    ON
        @Temp1.tempid = @Temp1a.tempid

What I need is to have a possibility to UPDATE values from one table using values from the other table.

But I'm getting an error

Must declare a scalar variable "@Temp1a".

and

Incorrect syntax near '.'.

How can I make this work?


Solution

  • UPDATE
        t
    SET 
        tasknr = a.tasknr,
        devcat = a.devcat,
        taskop = a.taskop,
        taskcl = a.taskcl
    FROM
        @Temp1a a
    INNER JOIN
        @Temp1 t
    ON
        t.tempid = a.tempid