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?

2 Answers 2

6

Set only assigns one value at a time.

You should use

   SELECT @cfMitt = CfMittente, 
          @cfDest = CfDestinatario
   FROM Messaggi
   WHERE IDMessaggio = @IDMessaggio
Sign up to request clarification or add additional context in comments.

Comments

1

Variable declaration can also be;

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

Assignment;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.