2014-05-03 58 views
0

我有這樣的storedprcocedure:同時加入case語句得到錯誤的存儲過程

SELECT 

    k.HBarcode, m.make,t.Compl, 
    t.plateno,t.self,t.dtime, v.vtype, l.locname,[dbo].[EDTCAL](t.TBarcode)as EDT, t.locid,t.vtid,t.lsttic, 
    c.Colname, te.UniqueName 

    FROM transaction_tbl t 

    left JOIN KHanger_tbl k ON t.transactID = k.transactID 
    left JOIN make_tbl m ON t.mkid = m.mkid 

    left JOIN vtype_tbl v ON v.vtid = t.vtid 
    left JOIN Location_tbl l ON t.locid = l.locid 

    left JOIN Color_tbl C ON t.colid = c.colid 
    left JOIN Terminals_tbl te ON k.tid = te.tid 

WHERE t.tbarcode = '0213' 

我的功能是這樣的:

ALTER function [dbo].[EDTCAL](@cardID VARCHAR(50)) RETURNS int 
as 
begin 
declare 
@locid int, 
@EDT int, 
@minEDT int, 
@buffEDT int, 
@value int 

select @locid= t.Locid from Transaction_tbl t where [email protected] 
select @EDT= l.EDT from Location_tbl l where [email protected] 
select @minEDT=l.MinEDT from Location_tbl l where [email protected] 
select @buffEDT=l.BuffrEDT from Location_tbl l where [email protected] 
if (@EDT is null) or (@EDT=0) 
begin 
select @[email protected][email protected] 
end 
else 
begin 
select @[email protected][email protected] 
end 
return @value 
end 

所以我的存儲過程的執行時間s slow..so我從我的程序中刪除了我的功能,並且我編寫了如下代碼:

SELECT 

    k.HBarcode, m.make,t.Compl, 
    t.plateno,t.self,t.dtime, v.vtype, l.locname,case when l.edt is null or l.edt =0 then l.minEdt +l.bufferEdt else l.edt + l.bufferEdt end as EDT, t.locid,t.vtid,t.lsttic, 
    c.Colname, te.UniqueName 

    FROM transaction_tbl t 

    left JOIN KHanger_tbl k ON t.transactID = k.transactID 
    left JOIN make_tbl m ON t.mkid = m.mkid 

    left JOIN vtype_tbl v ON v.vtid = t.vtid 
    left JOIN Location_tbl l ON t.locid = l.locid 

    left JOIN Color_tbl C ON t.colid = c.colid 
    left JOIN Terminals_tbl te ON k.tid = te.tid 

WHERE t.tbarcode = '0213' 

*但出現錯誤: 消息207,級別16,狀態1,行4 無效的列名稱'bufferEdt'。 消息207,級別16,狀態1,行4 無效的列名稱bufferEdt'

我添加額外僅此代碼

的情況下,當l.edt爲空或l.edt = 0,則升。 minEdt + l.bufferEdt else l.edt + l.bufferEdt end to EDT

回答

0

tbarcode ='0213'在執行計劃中出現在哪裏?它應該在那裏。使用它可能沒有優勢。如果在一般情況下你想提高查詢的執行時間,你可能會完全擺脫udf,並從基於連接的case語句中獲得@ minEDT + @ buffEDT和@ value = @ EDT + @ buffEDT。

對不起,在手機上這樣做。關鍵是你不需要單獨的功能。用下面的替換呼叫到UDF: 情況下,當l.edt爲空或l.edt = 0然後l.minEdt + l.bufferEdt別的l.edt + l.bufferEdt端作爲EDT

的一點是您的udf確實爲返回的每一行分隔了增加執行時間的選擇。您不需要它,因爲這些字段已經作爲select語句的一部分返回。

+0

先生我怎麼可以加入我的功能特定的存儲過程? – user3262364

+0

先生你能否顯示示例代碼? – user3262364

+0

當l.edt爲空或l.edt = 0時,不要在您的select語句使用情況下調用EDTCAL,那麼l.minEdt + l.bufferEdt else l.edt + l.bufferEdt以EDT結束。正如我所說,我正在通過電話進行此操作。我會稍後發佈代碼,但試試這個 –