Tomb_Raider_Legend Tomb_Raider_Legend - 1 year ago 187
SQL Question

Must declare the scalar variable - SQL Server

I have a table called CourceInstance which includes a Period and a Year attribute. I am trying to put the values of Period attribute from CourseInstance into my table variable. It can run without a problem however every time I run the

EXEC @tablePeriod
part or the whole code it gives me the error:

Must declare the scalar variable "@tablePeriod".

What can the problem be of? @tablePeriod is already declared.

DECLARE @tablePeriod TABLE ( Period INT ) INSERT INTO @tablePeriod (Period)
SELECT Period FROM CourseInstance WHERE Year = 2015

EXEC @tablePeriod

Answer Source

I'd expect

EXEC @tablePeriod

should be something like

select * from @tablePeriod

Then it won't return error but you have to run whole code in one block.

Running the code that uses the variable separately from the declaration statement is giving you error.

Edit: Now I guess I know what you want to achieve. Actually the DECLARE and INSERT statement are enough for that. No need for something else.


select * from @tablePeriod

in same block will show you that the values are inserted into table variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download