Sql server –Example of sql cursor with syntax

Sql server –Example of sql cursor with syntax

In this Post we take an Example of sql cursor.

We consider a table in my sql databse named is my_testing. The data base table name is Login_table.

Now we want to make a cursor named my_cursor. And want to find all recored in login_table.

Example of Cursor in sql :


DECLARE @Name varchar(50), @Password varchar(50)

 -- Declare cursor with name my_cursor.

DECLARE my_cursor CURSOR

LOCAL SCROLL STATIC

FOR
 -- select recored from table.

Select Name, Password FROM Login_table

 -- open declared cursor

OPEN my_cursor

FETCH NEXT FROM my_cursor

   INTO @Name, @Password
  
    -- print the name
   
   PRINT @Name + ' from ' + @Password

WHILE @@FETCH_STATUS = 0

BEGIN

   FETCH NEXT FROM my_cursor

   INTO @Name, @Password

    -- print the name
   
   PRINT @Name + ' from ' + @Password

END

 -- close the cursor

CLOSE my_cursor

DEALLOCATE my_cursor


Sql server –Example of sql cursor with syntax


Other Sql server Related Post :

Comments

  1. I have been a DBA for over 13 years and have had to actually use a cursor maybe 4 times. Remember, when using SQL Server and writing T-SQL you don't need a cursor about 99.99 percent of the time.

    Cursors usually don't scale well in T-SQL.

    Cursors are almost always used by people who don't understand relational database set-based theory.

    If you want to use a cursor, ask an experienced DBA first, then ask another. If they both say yes, then you _may_ need a cursor.

    Remember: Friends don't let friends use cursors.

    ReplyDelete

Post a Comment