我不明白爲什麼我不斷收到此錯誤。任何人都可以幫助我理解爲什麼這個錯誤即將到來,將不勝感激。我正在嘗試更新大型數據庫,並試圖用'D:'替換所有附件的默認位置字符串。我覺得我很接近,但我不明白這個錯誤。這個查詢成功地遍歷了我所有保存在@Tables中的表名,但是在每個表中我都會得到這個錯誤。這裏是我的代碼... 錯誤:必須聲明標量變量「@oldPath」必須聲明標量變量
DECLARE @newPath VARCHAR(500);
SET @newPath = 'D:\temp';
DECLARE @oldPath VARCHAR(500);
SET @oldPath = (SELECT Value FROM Settings WHERE Name = 'AttachmentDirectoryPath');
DECLARE @Tables TABLE
(
myTableName VARCHAR(500)
)
insert into @Tables(myTableName) values ('WorkOrderMasterAttachments');
insert into @Tables(myTableName) values ('BillOfMaterialAttachments');
insert into @Tables(myTableName) values ('CustomerAttachments');
insert into @Tables(myTableName) values ('ToolAttachments');
insert into @Tables(myTableName) values ('FacilityAttachments');
insert into @Tables(myTableName) values ('LocationAttachments');
insert into @Tables(myTableName) values ('AssetAttachments');
insert into @Tables(myTableName) values ('ContractorAttachments');
insert into @Tables(myTableName) values ('VendorAttachments');
insert into @Tables(myTableName) values ('WorkOrderAttachments');
insert into @Tables(myTableName) values ('ClosedWorkOrderAttachments');
insert into @Tables(myTableName) values ('EmployeeAttachments');
insert into @Tables(myTableName) values ('PurchaseOrderAttachments');
insert into @Tables(myTableName) values ('PurchaseOrderHistoryAttachments');
insert into @Tables(myTableName) values ('PartAttachments');
insert into @Tables(myTableName) values ('AssetSystemAttachments');
DECLARE cursor_tables cursor
FOR SELECT myTableName from @Tables
OPEN cursor_tables
DECLARE @loopTableName VARCHAR(100)
fetch next from cursor_tables
into @loopTableName
DECLARE @updateSql VARCHAR(2000);
WHILE @@FETCH_STATUS = 0
BEGIN
SET @updateSql = ' UPDATE ' + @loopTableName +
' SET Path = REPLACE(Path,@oldPath,@newPath)'
--print @updateSql
EXEC(@updatesql)
fetch next from cursor_tables
into @loopTableName
END
CLOSE cursor_tables
DEALLOCATE cursor_tables
難道這不是一個很簡單的只寫你的15-16更新語句,而不是在循環中這樣做嗎?這會花費很少的打字,而且會更簡單。 –
是的,但我將加載帶有名稱LIKE'%Attachments%'而不是所有插入語句的所有表的@Tables。 – techkris
即使你正在使用動態sql,我也不會使用循環。使用動態sql來構建sql語句並一次執行它。 –