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();
}
我編輯的問題,除了格式化,「512888'512889」改變了你的輸入值「512888,512889」。即逗號分隔值。 – ViKiNG