我有一个存储过程,必须单击一次按钮输入超过 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
}
}
谁能建议一个更好的解决方案来循环存储过程中的数据表?