Quantcast
Channel: Questions in topic: "variables"
Viewing all articles
Browse latest Browse all 49

Error when EXEC is used with a temp table variable

$
0
0
This is a simplified version of what I am doing. I am building a sql statement using a loop, which creates code for a pivot table. When I run this with the select statement it runs fine, but when I use the EXEC(@sql) I get an error, *Must declare the table variable "@GetCertPathsDetailsTable".* DECLARE @GetCertPathsDetailsTable TABLE --create temp table (CatalogTitle VARCHAR(MAX), CompletionDate DATETIME, First_Name VARCHAR(128), Last_Name VARCHAR(128)) INSERT INTO @GetCertPathsDetailsTable VALUES('Basic Courses', '10/09/2004', 'Joe', 'Blue') INSERT INTO @GetCertPathsDetailsTable VALUES('Basic Courses', '12/09/2005', 'Joey', 'Doe') INSERT INTO @GetCertPathsDetailsTable VALUES('Journeyman', '08/07/2007', 'Joe', 'Blue') INSERT INTO @GetCertPathsDetailsTable VALUES('Journeyman', '07/04/2006', 'Joey', 'Doe') INSERT INTO @GetCertPathsDetailsTable VALUES('Master', '10/19/2000', 'Joey', 'Doe') DECLARE @AsOfDate DATETIME SET @AsOfDate = '10/09/2008' DECLARE @sql Varchar(1000) SET @sql = 'SELECT * FROM ( SELECT Last_Name, First_Name, CompletionDate, CatalogTitle FROM @GetCertPathsDetailsTable WHERE CompletionDate <= @AsOfDate ) x PIVOT (MAX(CompletionDate) FOR CatalogTitle IN ([Basic Courses], [Journeyman],[Master])) AS x' EXEC(@sql)

Viewing all articles
Browse latest Browse all 49

Trending Articles