我有以下(簡化的)存儲過程:添加列到存儲過程有條件
CREATE FUNCTION [dbo].[UDF_FulfilmentBatch](@FulfilmentID INT) RETURNS
@Result TABLE (
[sequence] INT,
membershipid BIGINT,
membershipNo VARCHAR(255)
)
AS
BEGIN
DECLARE @_sequence INT
DECLARE @_membershipid BIGINT
DECLARE @_membershipNo VARCHAR(255)
SET @_sequence = 1
IF @FulfilmentID = 4
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_HH
END
IF @FulfilmentID = 3
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_ID
END
ELSE IF @FulfilmentID = 2
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_Art
END
ELSE
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_Tha
OPEN FulfilCursor
FETCH NEXT FROM FulfilCursor INTO @_membershipid, @_membershipNo
WHILE @@FETCH_STATUS = 0 BEGIN
INSERT INTO @Result
VALUES (@_sequence, @_membershipid, @_membershipNo)
SET @_sequence = @_sequence + 1
FETCH NEXT FROM FulfilCursor INTO @_membershipid, @_membershipNo
END
CLOSE FulfilCursor
DEALLOCATE FulfilCursor
RETURN
END
GO
我的問題是,當FulfilmentID = 4,I灣添加一個額外的場 - 「投放」
如果已經嘗試了以下內容:
CREATE FUNCTION [dbo].[UDF_FulfilmentBatch](@FulfilmentID INT) RETURNS
@Result TABLE (
[sequence] INT,
membershipid BIGINT,
membershipNo VARCHAR(255)
IF @FulfilmentID = 4
BEGIN
,Delivery VARCHAR(255)
END
)
AS
BEGIN
DECLARE @_sequence INT
DECLARE @_membershipid BIGINT
DECLARE @_membershipNo VARCHAR(255)
IF @FulfilmentID = 4
BEGIN
DECLARE @_Delivery VARCHAR(255)
END
SET @_sequence = 1
IF @FulfilmentID = 4
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_HH
END
IF @FulfilmentID = 3
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_ID
END
ELSE IF @FulfilmentID = 2
BEGIN
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_Art
END
ELSE
DECLARE FulfilCursor CURSOR FAST_FORWARD FOR
SELECT * from VW_FulfilmentExtract_Tha
OPEN FulfilCursor
FETCH NEXT FROM FulfilCursor INTO @_membershipid, @_membershipNo
WHILE @@FETCH_STATUS = 0 BEGIN
INSERT INTO @Result
VALUES (@_sequence, @_membershipid, @_membershipNo IF @FulfilmentID=4 BEGIN @_Delivery )
SET @_sequence = @_sequence + 1
FETCH NEXT FROM FulfilCursor INTO @_membershipid, @_membershipNo
END
CLOSE FulfilCursor
DEALLOCATE FulfilCursor
RETURN
END
GO
但這並沒有工作(請原諒任何語法錯誤,這是這麼一個粗略的打字)
表單搜索網頁似乎沒有太多關於此。可以做到嗎?
你可以用一個簡單的UNION合併查詢其'WHERE'語句檢查參數值來代替這一切。由於避免了遊標,程序將會*更簡單,執行更快*。實際上,N倍更快,其中N是返回記錄的數量 –
您說你有一個存儲過程,但是你創建了一個'FUNCTION' ......並且'@ Result'的原因是什麼?你會用它做什麼? – NickyvV
我會建議*從不*創建過程,爲不同的參數組合返回不同的模式。對於功能這是自動照顧。 – Arvo