2017-07-31 53 views
0

請幫我解決以下問題。如何從單個參數獲取多個值到SQL Server中的過程中

我想從單個參數獲取多個值到SQL Server中的過程,我正在學習SQL和C#。

這裏是我的代碼:

ALTER PROCEDURE AProc_Getback 
    (@fcode VARCHAR(10)) 
AS 
BEGIN 
    UPDATE t_PALM_PersonnelFileMst 
    SET fdflag = 0, frdate = null, fddate = null, 
     fdtype = null, fdreason = null 
    WHERE fcode IN ('@fcode') 

    DELETE FROM t_pald_dimissiontrm 
    WHERE fcode IN ('@fcode') 
END 

Exec AProc_Getback '512888,512889' 

非常感謝

+0

我編輯的問題,除了格式化,「512888'512889」改變了你的輸入值「512888,512889」。即逗號分隔值。 – ViKiNG

回答

3

Use Table-Valued Parameters

CREATE TYPE FCodes AS TABLE ( 
    Code VARCHAR(32) -- Use type of "fcode" column 
); 

存儲過程

ALTER PROCEDURE AProc_Getback 
(
    @fcode FCodes 
) 
AS 
BEGIN 
    UPDATE t_PALM_PersonnelFileMst SET 
     fdflag = 0, 
     frdate = null, 
     fddate = null, 
     fdtype = null, 
     fdreason = null 
    WHERE fcode IN (SELECT Code FROM @fcode) 

    DELETE FROM t_pald_dimissiontrm 
    WHERE fcode IN (SELECT Code FROM @fcode) 
END 

執行

DECLARE @Codes AS FCodes 
INSERT INTO @Codes VALUES ('123'), ('456'), ('789') 

EXEC AProc_Getback @Codes 

從C#您可以創建類型的參數與DataTable的價值,並將其傳遞到存儲過程

var codesParameter = new SqlParameter 
{ 
    ParameterName = "@fcodes", 
    SqlDbType = SqlDbType.Structure, 
    TypeName = "dbo.FCodes", // Important! - name of type in database 
    Value = dataTableOfCodes // Should contain column with name "Code" 
}; 

using (var connection = new SqlConnection(connectionString)) 
using (var command = connection.CreateCommand()) 
{ 
    command.CommandText = "dbo.AProc_Getback"; 
    command.CommandType = CommandType.StoredProcedure; 
    command.Parameters.Add(codesParameter); 

    connection.Open(); 
    command.ExecuteNonQuery(); 
} 
+0

程序的參數是@fcode FCodes,我不明白。 –

+0

@ThànhNguyễn,'FCodes'是參數'@ fcode'的類型。您將通過執行第一個腳本CREATE TYPE FCodes AS TABLE ...' – Fabio

+0

upvoted for a comprehensive and detailed answer來創建此類型。 Bravo Fabio – ViKiNG

-1
Alter PROCEDURE AProc_Getback (@fcode varchar(Max) 

    ) AS 

    Begin update t_PALM_PersonnelFileMst set fdflag = 0,frdate = null,fddate = null,fdtype = null, fdreason = null where fcode in (select Item 
from dbo.SplitString(@fcode, ',')) 
    delete from t_pald_dimissiontrm where fcode in (select Item 
from dbo.SplitString(@fcode, ',')) end 

然後

Exec AProc_Getback '512888,512889' 
+0

尊敬的Hiran nuwanga,我在asp.net上創建了web窗體,當我在文本框中輸入值時,如果該值不在t_PALM_PersonnelFileMst表中退出,它將向用戶顯示錯誤,我該怎麼辦? –

0

首先,你的參數必須能夠保持輸入數據。目前,它只能接收最多10個字符的數據。見@fcode varchar(10)

如果您沒有可用的分割功能,您可以創建一個。或者,您可以使用一些內聯代碼來實現這一點。例如:

ALTER PROCEDURE AProc_Getback(@Fcode VARCHAR(MAX)) 
AS 
BEGIN 

    CREATE TABLE #FCodes (Item VARCHAR(30)); 
    DECLARE @Insert VARCHAR(MAX) = 'INSERT INTO #FCodes VALUES ('''+REPLACE(@Fcode,',','''),(''')+''');'; 
    EXEC (@Insert); 


    UPDATE t_PALM_PersonnelFileMst 
    SET 
     fdflag = 0, 
     frdate = NULL, 
     fddate = NULL, 
     fdtype = NULL, 
     fdreason = NULL 
    WHERE fcode IN(SELECT Item FROM #FCodes); 


    DELETE FROM t_pald_dimissiontrm 
    WHERE fcode IN(SELECT Item FROM #FCodes); 
END; 
+0

尊敬的先生,非常感謝,讓我親嘗試 –

+0

,如何執行這個程序?我執行EXEC AProc_Getback'000009','000010',所以它說「過程或函數AProc_Getback指定的參數太多。」 –

+0

僅用作'000009,000010',或者如果您有更多值,請使用'000009,000010,000009,000010'。這意味着所有值之間只有一對「'圍繞所有值和逗號(,)。舉例如下: – ViKiNG

相關問題