create a cursor in ms sql server

create a cursor in ms sql server

Cursor is used to fetch records(rows) from any table sequentially and records can be manipulated, calculated as per your need.

To create a cursor use this sql query:

declare cursor_name cursor for
select id from tbl_Category
open cursor_name
fetch next from cursor_name into @Id
while @@fetch_status=0
insert into table_name(id) values (@Id)
fetch next from cursor_name into @Id

close cursor_name
deallocate cursor_name


Declare cursor is used  to declare a cursor and we need to open the cursor to use it.

fetch next is used to to fetch records sequentially and condition while @@fetch_status=0 verifies the sequence till end.

At last close the cursor by using close cursor and deallocate cursor.

Happy coding

Author: admin

3 thoughts on “create a cursor in ms sql server

  1. I was just looking at your create a cursor in ms sql server – Sweksha website and see that your website has the potential to get a lot of visitors. I just want to tell you, In case you didn’t already know… There is a website service which already has more than 16 million users, and the majority of the users are interested in topics like yours. By getting your site on this service you have a chance to get your site more visitors than you can imagine. It is free to sign up and you can find out more about it here: – Now, let me ask you… Do you need your site to be successful to maintain your way of life? Do you need targeted traffic who are interested in the services and products you offer? Are looking for exposure, to increase sales, and to quickly develop awareness for your website? If your answer is YES, you can achieve these things only if you get your site on the service I am describing. This traffic network advertises you to thousands, while also giving you a chance to test the service before paying anything at all. All the popular sites are using this network to boost their traffic and ad revenue! Why aren’t you? And what is better than traffic? It’s recurring traffic! That’s how running a successful site works… Here’s to your success! Find out more here:

Leave a Reply

Your email address will not be published. Required fields are marked *