當執行以下存儲過程時,我得到無效的對象名稱dbo.Approved。對象dbo.Approved確實存在,所以大概這是與我傳遞表名作爲參數的方式有關嗎?無效的對象名稱 - SQL服務器2005
我還應該補充一點,我通過.NET執行程序或從SMSS內部執行程序來獲得錯誤。
@tableName as nvarchar(100)
AS
BEGIN
EXEC('
UPDATE T1
SET T1.NPTid = dbo.Locations.NPT_ID
FROM ' + '[' + @tableName + '] As T1
INNER JOIN dbo.Locations ON T1.Where_Committed = dbo.Locations.Location_Name
')
END
編輯從喬和JNK接到求救後,現在的存儲過程是這樣的,但我得到的錯誤
Msg 102, Level 15, State 1, Procedure sp_Updater, Line 14
Incorrect syntax near 'QUOTENAME'.
新的存儲過程
@tableName as nvarchar(100),
@schemaName as nvarchar(20)
AS
BEGIN
EXEC('
--Update NPT
UPDATE T1
SET T1.NPTid = dbo.Locations.NPT_ID
FROM ' + QUOTENAME(@schemaName) + '.' + QUOTENAME(@tableName) + ' As T1
INNER JOIN dbo.Locations ON T1.Where_Committed = dbo.Locations.Location_Name
')
END
你是如何將表名作爲參數傳遞的? 「dbo.Approved」或「Approved」 – Ramy 2010-12-17 16:38:41
是的,您可以忽略Alter Proc位 – MrBliz 2010-12-17 16:39:41