Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:40:30+00:00 2026-06-15T15:40:30+00:00

I had a stored procedure which has to be input with more than 8000

  • 0

I had a stored procedure which has to be input with more than 8000 rows at a single button click that is used to close the daily attendance of employees.

I plan to send the input to stored procedure as datatable rather than sending it as row by row each time

I managed to create a table type user-defined type and used a parameter with that user-defined type as input parameter

  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

but now what I want is inside the stored procedure I want to loop the Datatable and check a condition

eg like this

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

Can anyone suggest a better solution to loop a datatable in a stored procedure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:40:32+00:00Added an answer on June 15, 2026 at 3:40 pm

    Since you’re on SQL Server 2008 – this is exactly the scenario for using the MERGE statement! No looping or anything needed – just a single MERGE statement will do!

    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
    

    Of course, you can also do other things – and you can define more matching/non-matching criteria, if needed.

    This MERGE statement is run once and does all the work in a single pass – no looping, no RBAR (row-by-agonizing-row) processing – nothing of that sort. One nice, fast, set-based statement and you’re done!

    Update: from your updated question, I see you have three major errors in your MERGE statement:

     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()) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MS SQL Server has a TABLE data type which can be used in stored
I am converting a stored procedure which I had previously written as a string
I do a call to an oracle stored procedure using JDBCTemplate which has an
I've got a stored procedure which used to take around 25s to execute. I've
I had a need to pass an integer array to a stored procedure from
I'm using paging which uses a stored procedure to filter by letter. I need
I have a stored procedure that goes like this: ALTER PROCEDURE [dbo].[AuthenticateUser] @AzUserName varchar(20),
I'm trying to call an Oracle Stored Procedure which returns XMLType data, but all
I have a Stored Procedure(SP) in MS SQL 2008 R2 I'm building that requires
Has anyone ever used Solrnet inside CLR Stored Procedures? I would really appreciate pointers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.