當需要呼叫外部的Stored Proceduce時,可以使用以下的範例來查詢到本身以外的資料
1.先建立一個tempTable
2.在連到Server將資料庫、帳號密碼輸入完看要執行哪個程序
3.就可以將資料暫存到tempTable裡面了
EX:
use Itemxxxxx --資料庫名稱
go
if OBJECT_ID(N'tempdb.dbo.#Lon_tempItemFiltered') is not null --檢查有無temptable
drop table #Lon_tempItemFiltered
Select a.* into #Lon_tempItemFiltered
FROM OPENROWSET('SQLNCLI','server=stapxxx;database=Itemxxxx;Uid=sa;Pwd=xxxxx;','SET FMTONLY OFF;SET NOCOUNT ON; EXEC [Itemxxxxxxx].[dbo].[UP_xxxxxxxxxx]
@xxxx= 0,
@xxx= 10,
@xx= ''DESC'',
@xx= ''num'',
@xx= NULL,
@xx= 33,
@xx= NULL,
@xx= NULL,
@xx= 1,
@xx= 0,
@xx= NULL,
@xx= ''USA'',
@xx= NULL,
@xx = NULL,
@xx= 0') AS a;
select * from #Lon_tempItemFiltered
------------------------------------------------------------------------------------
--執行預存程序的字串內容
EXECUTE sp_executesql @SQLString
EXECUTE sp_executesql N''--N是指轉uncode