0

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?

4

2 に答える 2

6

Set only assigns one value at a time.

You should use

   SELECT @cfMitt = CfMittente, 
          @cfDest = CfDestinatario
   FROM Messaggi
   WHERE IDMessaggio = @IDMessaggio
于 2012-11-05T14:49:06.890 に答える
1

変数宣言も可能です。

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

割り当て;

SELECT @V1 = C1, @V2 = C2,...@Vn = Cn
FROM [Table]
WHERE Conditions
于 2012-11-05T14:50:16.037 に答える