我有一個存儲過程,必須在單擊按鈕時輸入超過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
}
}
任何人都可以提出一個更好的解決方案來循環存儲過程中的數據表?
能否請您解釋一下什麼是你想與表'中的數據做dbo.Employeedata'?你在試圖合併數據嗎?相反,可能有一個簡單的解決方案。 –
由於你在SQL Server 2008上 - 使用MERGE語句! **完全** MERGE'語句的場景 - 插入新行並更新現有的行... –
employe表只是一個輸入參數....當用戶給一個按鈕單擊時...數據從一個網格視圖被捕獲並通過@closingemployee傳遞給存儲過程..... –