1

我有一个存储过程,必须单击一次按钮输入超过 8000 行,用于关闭员工的日常考勤。

我计划将输入作为数据表发送到存储过程,而不是每次都逐行发送

我设法创建了一个表类型用户定义类型并使用具有该用户定义类型的参数作为输入参数

  USE [ATCHRM.MDF]
GO

/****** Object:  UserDefinedTableType [dbo].[employeeswipedclose123]    Script Date: 12/11/2012 12:04:34 ******/
CREATE TYPE [dbo].[employeeswipedclose123] AS TABLE(
    [empid] [int] NULL,
    [datetoday] [datetime] NULL,
    [Swipepk] [int] NULL
)
GO
  CREATE PROCEDURE dbo.CloseAttendance (@closingemployee dbo.employeeswipedclose123 READONLY )
AS
BEGIN

   MERGE EmployeSwipeDaily_tbl AS Target

   USING @closingemployee AS Source

   ON (Target.empid = Source.empid) and (Target.Date = Source.datetoday)

   WHEN MATCHED THEN 
       BEGIN 
           UPDATE    Target
            SET Target. IsCompleted = N'Y'
             WHERE     (Source.swipePK = Target.Swipepk) AND (Source.empid = Target.empid) AND (Target.Date = Source.datetoday)   

         WHEN NOT MATCHED THEN

       INSERT INTO Target
       (empid, Swipin, SwipeOut, Date, Duration, deviceid, InStatus, Outstatus, Invalue, OutValue, IsCompleted, CompletedDate)
        VALUES     (@empid, CONVERT(DATETIME, ' 00:00:00', 102), CONVERT(DATETIME, ' 00:00:00', 102),@datetoday, CONVERT(DATETIME, 
                      ' 00:00:00', 102), 0, N'A', N'A', 0, 0, N'Y',(select GETDATE()) )

END


GO

但现在我想要的是在存储过程中我想循环Datatable并检查一个条件

比如像这样

for(int i=0 ;i<dt.count;i++)
{
   if(dt.rows[i][swipepk]==0)
   {
       insert into employe swipe tbl()
   }
   else 
   {
       update employee tbl
   }
}

谁能建议一个更好的解决方案来循环存储过程中的数据表?

4

1 回答 1

5

由于您使用的是 SQL Server 2008 - 这正是使用该MERGE语句的场景!无需循环或任何需要 - 只需一条MERGE语句即可!

CREATE PROCEDURE dbo.CloseAttendance (@closingemployee dbo.Employeedata READONLY)
BEGIN
   -- this is the target of your MERGE - where the data is supposed to go
   MERGE dbo.Employees AS Target
   -- this is the source where the data to be merged comes from
   USING @closingemployee AS Source
   -- set up a "join" condition for those two sets of data
   ON Target.empid = Source.empid
   -- now define what to do when that JOIN condition matches
   WHEN MATCHED THEN 
       UPDATE 
          SET Target.SomeColumn = Source.Swipepk  --- or WHATEVER you need to update here!
   -- now define what to do when that JOIN condition DOES NOT match (e.g. new employee)
   WHEN NOT MATCHED THEN
      INSERT(EmpId, CategoryName, Swipepk)
      VALUES(Source.EmpId, Source.CategoryName, Source.Swipepk) ;
END

当然,您还可以做其他事情 - 如果需要,您可以定义更多匹配/不匹配标准。

MERGE语句运行一次,并且一次完成所有工作 - 没有循环,没有 RBAR(逐行痛苦)处理 - 没有那种类型。一个不错的、快速的、基于集合的语句,你就完成了!

更新:从您更新的问题中,我看到您的陈述中有三个主要错误:MERGE

 WHEN MATCHED THEN 
    BEGIN                                 <=== ERROR #1 : you CANNOT have a BEGIN here!
        UPDATE Target
        SET Target.IsCompleted = N'Y'
        WHERE (Source.swipePK = Target.Swipepk)  <=== ERROR #2 : you CANNOT have 
          AND (Source.empid = Target.empid)                      additional WHERE 
          AND (Target.Date = Source.datetoday)                   clause on the UPDATE

WHEN NOT MATCHED THEN
   INSERT INTO Target              <=== ERROR #3: you CANNOT define a INSERT INTO ....
                                      the "INTO" table is already a given by the MERGE
   (empid, Swipin, SwipeOut, Date, Duration, deviceid, InStatus, Outstatus, Invalue, OutValue, IsCompleted, CompletedDate)
    VALUES     (@empid, CONVERT(DATETIME, ' 00:00:00', 102), CONVERT(DATETIME, ' 00:00:00', 102),@datetoday, CONVERT(DATETIME, 
                  ' 00:00:00', 102), 0, N'A', N'A', 0, 0, N'Y',(select GETDATE()) )
于 2012-12-09T08:53:47.800 回答