我正在尝试我的第一个动态sql存储过程。我需要将完全相同的记录追加到具有相同列名的多个表中。我编译的内容,但是当它运行时,我得到'无效的列名'TradeDate。驱动程序sproc首先在下面,然后是包含动态语句的sproc。如果有人可以提供帮助,那就太好了..
ALTER PROCEDURE dbo.StoredProcedure2
AS
DECLARE @tableName varchar(120)
SET @tableName = 'tblDailyATR'
EXEC sprocAddDatesAndSymbolsToAggregatedStudy @tableName
RETURN
ALTER PROCEDURE dbo.sprocAddDatesAndSymbolsToAggregatedStudy
@table varchar(120)
AS
DECLARE @tableName varchar(120)
SET @tableName = @table
EXEC(
'INSERT INTO ' + @tableName + '(Symbol, TradeDate)
SELECT Symbol, TradingDate
FROM (SELECT tblSymbolsMain.Symbol, tblTradingDays.TradingDate
FROM tblSymbolsMain CROSS JOIN tblTradingDays
WHERE (tblTradingDays.TradingDate <= dbo.NextAvailableDataDownloadDate())) AS T1
WHERE (NOT EXISTS (SELECT TradeDate, Symbol
FROM' + @tableName +
' WHERE (TradeDate = T1.TradingDate) AND (Symbol = T1.Symbol)))')
RETURN
答案 0 :(得分:2)
你在这一行的“FROM”后遗漏了一个空格:
FROM' + @tableName +
应该是
FROM ' + @tableName +
否则它将尝试运行SELECT FROMTABLE。