0
Declare @ID nvarchar(14) = '12345678912345'  
DECLARE @Key int = (SELECT  key
                           FROM    table
                           WHERE   id = @ID) 

Declare @indicator varchar(1) = 'A' 

Declare @rdate Datetime = (SELECT LTRIM(RTRIM(Left(CAST(GetDate() AS Datetime2),23))))


SELECT @ID 
GO
SELECT @Key 
GO
SELECT @indicator 
GO
SELECT @date 
GO

I get error when I execute the above code. I see @ID print and it goes away and I get the following messages.

"Must declare the scalar variable "@Key"

"Must declare the scalar variable "@indicator"

"Must declare the scalar variable "@date"

Datatypes in the table: ID = nvarchar(14), key = int, indicator = varchar(1), and date = timestamp

1 Answer 1

2

Go will end the scope and destroy the variables.

Use a ; instead

SELECT @ID;SELECT @Key;SELECT @indicator;SELECT @rdate;
Sign up to request clarification or add additional context in comments.

1 Comment

Wow...that solved it. Now if I have a bunch of DML statements and each ends with GO, is it OK if I remove GO after each DML statements?

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.