This is very much a beginner question, but how do variables actually work in SSMS? I have the following 3 statements:
DECLARE @p1 TABLE (col2 VARCHAR(30))
UPDATE t
SET col2 = 'Peter', col3 = GETDATE()
OUTPUT CAST(INSERTED.col3 AS VARCHAR(30)) i
INTO @p1
WHERE col1 = 1
SELECT * FROM @p1
In SSMS, if I highlight all 3 statements and execute them together, the statements work as expected. But if I select & run them one at a time, I receive an error message
Msg 1087, Level 15, State 2, Line 71
Must declare the table variable "@p1"
Are T-SQL variables just available during specific execution? Is there another technique to actually "set" a variable to persist throughout a Session?