2011年3月13日 星期日

Stored Procedure中的temp table

前一陣子為了特殊目需要在stored procedure中依不同的原則來宣告table variable
由於無法達成,所以改成create temp table (#xxxTable)的方式然後依不同原則再用alter table drop column 的方式來做
寫在這篇 if .. else .. create table variable

今天在想,用temp table的方式除了是否因為有transaction可能會有performance的考量外, 如果是多人同時間呼叫 sp時, temp table的欄位數和資料內容會不會發瘋??

所以自己做了一個小小的測試, 用二個不同的session來執行以下的sp
 IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[zspBev]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[zspBev]
GO

create procedure zspBev (@type int)
as
begin
    IF  EXISTS (SELECT * FROM tempdb.sys.objects WHERE object_id = OBJECT_ID(N'tempdb..#tmpTable') AND type in (N'U'))
    drop table #tmpTable
    create table #tmpTable
    (col1 int,col2 int)
    begin transaction t1
    if @type = 1
    begin
    insert into #tmpTable values(1,2)
    commit transaction t1
    WAITFOR DELAY '00:00:05';
    select * from  #tmpTable
    end
    else
    begin
        insert into #tmpTable values(3,4)
        commit transaction t1
        WAITFOR DELAY '00:00:10';
        select * from  #tmpTable
    end

    select * from  #tmpTable
end



看來彼此間是不會影響到的. 
在tempdb中查看Temporary Tables目錄下的物件,同時間會有二個#tmpTable開頭的物件,但往後仔細看物件名稱, sql server自己已經有所區別,只是table名稱係漏漏等....
dbo.#tmpTable________________________________________________________________________________________________________000000000036
dbo.#tmpTable________________________________________________________________________________________________________000000000037

所以....就別擔心太多了



沒有留言:

張貼留言

IIS 網站無法下載中文檔名檔案因為取消高位元字元

 網址+中文檔案名稱方式下載檔案顯示 404 - 找不到檔案或目錄。 但英數字檔案名稱則可下載。 因為設定GCB IIS,其中1項 將高位元字元預設取消勾選了。 27 TWGCB-04-014-0028 要求篩選與其他限制模組 允許高位元字元 這項原則設定決定查詢...