8

The following works in MySQL:

DECLARE myInt INT(20);
DECLARE myDate DATETIME;

SELECT someInt, someDate
INTO myInt, myDate
FROM someTable
WHERE myName = someName

Is it possible to do something like that in SQL Server?

SQL Server complaints about "Incorrect syntax" in the INTO line f that is attempted local @variables.

2 Answers 2

11

Not sure if I understand your issue. If you want to set multiple variables at once:

DECLARE @myInt INT;
DECLARE @myDate DATETIME;

SELECT @myInt = someInt, @myDate = someDate
FROM someTable
WHERE myName = someName

Also note if the select fetches many rows the variables will hold the last rows values. In fact the variables will be set for each row fetched.

Also note in SQL Server you don't have a parameter to the Int declaration. Maybe you want to use Decimal(20).

Sign up to request clarification or add additional context in comments.

1 Comment

To clafify the remark about the int declaration, in SQL Server int is a 32-bit integer, and bigint is a 64-bit integer, so if you need a different precision then you can use the decimal type.
1

Yes, do it like below. Also, prefix @ sign for declaring parameter.

DECLARE @myInt INT;
DECLARE @myDate DATETIME;

SELECT @myInt = someInt, @myDate = someDate
FROM someTable
WHERE myName = someName

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.