问题是它何时尝试运行以下语句
SELECT @rowCount = COUNT(1) FROM @currentTable
我收到一条错误消息“必须声明变量'@currentTable'。”
这是完整的代码:
CREATE TABLE #RequiredTables (
ID INT IDENTITY(1,1),
TableName VARCHAR(200)
)
DECLARE @counter INT
DECLARE @rowCount INT
DECLARE @tableCount INT
DECLARE @currentTable VARCHAR(200)
DECLARE @query VARCHAR(6000)
SET @counter = 1
INSERT INTO #RequiredTables
SELECT 'Areas'
UNION ALL
SELECT 'Brands'
UNION ALL
SELECT 'CardsEnrolled'
UNION ALL
SELECT 'CardsMatrix'
UNION ALL
SELECT 'CardsUtilized'
UNION ALL
SELECT 'Clients'
UNION ALL
SELECT 'Customers'
UNION ALL
SELECT 'DoctorMatrix'
UNION ALL
SELECT 'DoctorMatrixPromo'
UNION ALL
SELECT 'Doctors'
UNION ALL
SELECT 'DoctorClass'
UNION ALL
SELECT 'Employees'
UNION ALL
SELECT 'MedCenters'
UNION ALL
SELECT 'pretagging_dsm_allocations'
UNION ALL
SELECT 'pretagging_new_cards'
UNION ALL
SELECT 'pretagging_data'
UNION ALL
SELECT 'pretagging_cards'
UNION ALL
SELECT 'pretagging_status'
UNION ALL
SELECT 'SKUs'
UNION ALL
SELECT 'Specializations'
UNION ALL
SELECT 'TAs'
UNION ALL
SELECT 'Territories'
UNION ALL
SELECT 'TerritoryMatrix'
SELECT @tableCount = COUNT(1) FROM #RequiredTables
WHILE @counter <= @tableCount
BEGIN
SELECT @currentTable = TableName FROM #RequiredTables WHERE ID = @counter
SELECT @rowCount = COUNT(1) FROM @currentTable
SET @counter = @counter + 1
END
答案 0 :(得分:0)
我认为你不能使用变量将tablename传递给from子句。
当我尝试错误时,“必须声明表变量”@currentTable“。”
我认为当SQL服务器执行查询时,它正在寻找一个表变量而不是一个varchar变量。
希望这有帮助。