Wednesday, October 25, 2017

Cursor in SQL

I have a task in which there are some records that are missing in my child table and need to get insert from parent table, records are more then 1000
so by using simple inserting script we can use cursor, I need to insert those users who have  FreeSignUp=1 and call my store procedure do rest of insert thing.




DECLARE @Customer_id numeric
 DECLARE cur_emp CURSOR
STATIC FOR
SELECT  Customer_id from customer where FreeSignUp=1
OPEN cur_emp
IF @@CURSOR_ROWS > 0
 BEGIN
 FETCH NEXT FROM cur_emp INTO @Customer_id
 WHILE @@Fetch_status = 0
 BEGIN
  EXEC sp_add_Customer @Customer_id

 FETCH NEXT FROM cur_emp INTO @Customer_id
 END
END
CLOSE cur_emp
DEALLOCATE cur_emp
SET NOCOUNT OFF


For Ref:
http://www.dotnettricks.com/learn/sqlserver/sql-server-basics-of-cursors

No comments:

Post a Comment