SqlServer存儲過程傳入Table參數

今天是週日,恰好有空閒時間整理一下這些天工做業務中遇到的問題。數據庫

有時候咱們有這樣一個需求,就是在後臺中傳過來一個IList<類>的泛型集合數據,該集合是某個類的實例集合體,而後將該集合中的實例的數據一個個地插入到數據庫或者更新到數據庫中去。一開始我想到的方法是拼接字符串,而後經過存儲過程對接收到的字符串進行截取,再一個個地插入或者更新到數據庫中去,這是最原始的方法,不過過程會比較複雜,想到這就頭疼。後來查找發現說SqlServer2008中爲存儲過程添加了一個新特性,能夠傳遞表類型的參數,既然能夠傳遞表類型參數,那問題就變得簡單啦。如下是開發中寫的code.asp.net

1.asp.net後臺:ide

 1         /// <summary>
 2         /// Add the PayrollCycle
 3         /// </summary>
 4         /// <param name="payrollCycle">payrollCycle</param>
 5         /// <returns>bool</returns>
 6         public bool AddPayrollCycle(IList<PayrollCycle> payrollCycles)
 7         {
 8             DataTable dataTable=new DataTable();
 9             dataTable.Columns.Add("Year",typeof(int));
10             dataTable.Columns.Add("Month",typeof(int));
11             dataTable.Columns.Add("CutoffDate",typeof(int));
12             dataTable.Columns.Add("PayrollDate",typeof(int));
13             dataTable.Columns.Add("EnterUser",typeof(string));
14             dataTable.Columns.Add("EnterDate",typeof(DateTime));
15             dataTable.Columns.Add("LastUpdatedUser",typeof(string));
16             dataTable.Columns.Add("LastUpdatedDate",typeof(DateTime));
17             foreach (PayrollCycle p in payrollCycles)
18             {
19                 DataRow dataRow = dataTable.NewRow();
20                 dataRow["Year"] = p.Year;
21                 dataRow["Month"] = p.Month;
22                 dataRow["CutoffDate"] = p.CutoffDate;
23                 dataRow["PayrollDate"] = p.PayrollDate;
24                 dataRow["EnterUser"] = UserSession.LogOnUserAccount;
25                 dataRow["EnterDate"] = DateTime.Now;
26                 dataRow["LastUpdatedUser"] = UserSession.LogOnUserAccount;
27                 dataRow["LastUpdatedDate"] = DateTime.Now;
28                 dataTable.Rows.Add(dataRow);
29             }
30             
31             SqlParameter[]paras=new SqlParameter[]
32                                     {
33                                         new SqlParameter("@PayrollCycles",dataTable)
34                                     };
35             return SqlHelper.ExecuteNonQuery("MCU.USP_AddPayrollCycles", paras) > 0;
36         }

爲dataTable添加column的時候,必須明確該列的typeof,不然在存儲過程中會把傳入的該列當成varchar類型看待,致使某些類型轉換失敗spa

2.在SqlServer中先定義一個Table類型的Type:.net

 1 CREATE TYPE [PayrollCycleType] AS TABLE(
 2     [YEAR] [int] NOT NULL,
 3     [Month] [int] NOT NULL,
 4     [CutoffDate] [int] NOT NULL,
 5     [PayrollDate] [int] NOT NULL,
 6     [EnterUser] [varchar](20) NULL,
 7     [EnterDate] [datetime] NULL,
 8     [LastUpdatedUser] [varchar](20) NULL,
 9     [LastUpdatedDate] [datetime] NULL
10 )
11 GO

接着編寫一個傳入上步驟中定義的表類型的參數的存儲過程,該參數爲Readonly(做爲表類型參數必須爲可讀),代碼以下:3d

插入操做:code

 1 CREATE PROCEDURE [MCU].[USP_AddPayrollCycles]
 2 (
 3     @PayrollCycles MCU.PayrollCycleType Readonly 
 4 )
 5 AS
 6 BEGIN
 7     SET NOCOUNT ON
 8     BEGIN TRANSACTION
 9     INSERT INTO MCU.PayrollCycle
10             ( [Year] ,
11               [Month] ,
12               CutoffDate ,
13               PayrollDate ,
14               EnterUser ,
15               EnterDate ,
16               LastUpdatedUser ,
17               LastUpdatedDate
18             )
19     SELECT    [Year] ,
20               [Month] ,
21               CutoffDate ,
22               PayrollDate ,
23               EnterUser ,
24               EnterDate ,
25               LastUpdatedUser ,
26               LastUpdatedDate 
27               FROM @PayrollCycles
28     COMMIT TRANSACTION           
29 END
30 
31 GO
View Code

更新操做:blog

 1 CREATE PROCEDURE [MCU].[USP_UpdatePayrollCycle]
 2 (
 3     @PayrollCycles MCU.PayrollCycleType READONLY,
 4     @TypeOfDate NVARCHAR(7)
 5 )
 6 AS
 7 BEGIN
 8     SET NOCOUNT ON
 9     --declare an table
10     DECLARE @temp AS MCU.PayrollCycleType
11     --insert into @temp from @PayrollCycles
12     INSERT INTO @temp
13             ( [YEAR] ,
14               [Month] ,
15               CutoffDate ,
16               PayrollDate 
17             )
18     SELECT 
19               [YEAR] ,
20               [Month] ,
21               CutoffDate ,
22               PayrollDate 
23               FROM @PayrollCycles
24     
25     --Update the PayrollCycle
26     IF(@TypeOfDate='Payroll')
27          BEGIN 
28             UPDATE MCU.PayrollCycle 
29             SET 
30                 PayrollDate=t.PayrollDate
31             FROM
32                 @temp t
33                 WHERE  MCU.PayrollCycle.[Year]=t.[Year] AND MCU.PayrollCycle.[Month]=t.[Month]
34          END
35              
36     IF(@TypeOfDate='Cut-off')
37         BEGIN
38             UPDATE MCU.PayrollCycle
39             SET
40                 CutoffDate=t.CutOffDate
41             FROM @temp t
42                    WHERE  MCU.PayrollCycle.[Year]=t.[Year] AND MCU.PayrollCycle.[Month]=t.[Month]
43         END                   
44 END
45 
46 GO
View Code

就這樣大功告成,用起來至關方便,就不用經過拼接字符串進行數據的插入,更新操做了。開發

相關文章
相關標籤/搜索