2
我有以下查詢:不知道臨時表時使用SQL Server執行
if OBJECT_ID('tempdb..#tmpTables') is not null
drop table #tmpTables
Execute('select TABLE_NAME into #tmpTables from '[email protected]+'.INFORMATION_SCHEMA.TABLES')
while (select COUNT(*) from #tmpTables)>0
begin
//here is my statement
end
當我執行此查詢時,我收到此錯誤:
無效的對象名稱# tmpTables'。
但是,當查詢更改爲此:
if OBJECT_ID('tempdb..#tmpTables') is not null
drop table #tmpTables
select TABLE_NAME into #tmpTables from INFORMATION_SCHEMA.TABLES
while (select COUNT(*) from #tmpTables)>0
begin
//here is my code
end
它的工作原理。
我該怎麼做?
,你需要把引用它在動態SQL代碼的其餘部分,因爲它只能在執行完成之前一直存在。爲什麼不在每個數據庫中創建相同的存儲過程,那麼代碼不必關心數據庫上下文或動態SQL,只有調用者必須確定要使用的正確數據庫。 – 2015-03-31 11:11:41