GVillani82
GVillani82

Reputation: 17439

Multiple variables assignment using SET and SELECT

I use the following t-sql code for assign to variable a value obtained from select statement

 DECLARE @cfMitt nvarchar(16)

 SET @cfMitt = (SELECT CfMittente
    FROM Messaggi
    WHERE IDMessaggio = @IDMessaggio)

If I want use multiple assignement I try with the following code, but something is wrong:

DECLARE @cfMitt nvarchar(16)
DECLARE @cfDest nvarchar(16)

SET @cfMitt, @cfDest= (SELECT CfMittente, CfDestinatario
FROM Messaggi
WHERE IDMessaggio = @IDMessaggio)

Where is the error?

Upvotes: 0

Views: 3535

Answers (2)

Kaf
Kaf

Reputation: 33839

Variable declaration can also be;

DECLARE @V1 VarType, @V2 VarType,...

Assignment;

SELECT @V1 = C1, @V2 = C2,...@Vn = Cn
FROM [Table]
WHERE Conditions

Upvotes: 1

podiluska
podiluska

Reputation: 51514

Set only assigns one value at a time.

You should use

   SELECT @cfMitt = CfMittente, 
          @cfDest = CfDestinatario
   FROM Messaggi
   WHERE IDMessaggio = @IDMessaggio

Upvotes: 6

Related Questions