Can someone explain to me why this does not work?
I declared a table variable as follows:
DECLARE @sqlSTRING varchar(3000)
DECLARE @MyTable TABLE
(EmpName varchar (50), EmpAge int, EmpJobID varchar(10))
I tried to run the following EXEC:
SET @sqlSTRING = 'INSERT INTO @MyTABLE (EmpName, EmpAge, EmpJobID)'
SET @sqlSTRING = @sqlSTRING + ' SELECT EmpName, EmpAge, EmpJobID FROM MyCompany'
EXEC (@sqlSTRING)
MyCompany is a table in the database with approx 10 records.
I get the error:
Server: Msg 137, Level 15, State 2, Line 1
Must declare the variable '@MyTable
But as you can see, I already declared it.
Could it be that SQL Server 2K doesn't recognize @MyTable when you try to pass it as a string using the EXEC function? If that's the case, what are my options? I NEED to dynamically create this Trans-SQL statement.
I declared a table variable as follows:
DECLARE @sqlSTRING varchar(3000)
DECLARE @MyTable TABLE
(EmpName varchar (50), EmpAge int, EmpJobID varchar(10))
I tried to run the following EXEC:
SET @sqlSTRING = 'INSERT INTO @MyTABLE (EmpName, EmpAge, EmpJobID)'
SET @sqlSTRING = @sqlSTRING + ' SELECT EmpName, EmpAge, EmpJobID FROM MyCompany'
EXEC (@sqlSTRING)
MyCompany is a table in the database with approx 10 records.
I get the error:
Server: Msg 137, Level 15, State 2, Line 1
Must declare the variable '@MyTable
But as you can see, I already declared it.
Could it be that SQL Server 2K doesn't recognize @MyTable when you try to pass it as a string using the EXEC function? If that's the case, what are my options? I NEED to dynamically create this Trans-SQL statement.