Konsultan IT Bandung
Konsultan IT Bandung

Reputation: 151

Set Variable for Select Result in SQL Server

I have SQL statement like these:

DECLARE @bDate DATE;
SET @bDate = SELECT birth_date FROM person WHERE id='1';

SELECT @bDate;

But the result always is an error:

Must Declare the scalar variable for @bDate

Can anyone help me? I have asked from any forum but the result are same.

Thanks

Upvotes: 0

Views: 3970

Answers (1)

Squirrel
Squirrel

Reputation: 24813

Your syntax is wrong for the SET

It should be

DECLARE @bDate DATE;
SET @bDate = (SELECT birth_date FROM person WHERE id='1');

SELECT @bDate;

OR, you can use SELECT

DECLARE @bDate DATE;
SELECT @bDate = birth_date FROM person WHERE id='1';

SELECT @bDate;

Upvotes: 5

Related Questions