how to use cursor in sql server 2008 with example
  • anyone please give me simple example of cursor use in sql server with simple and short code.
  • 2 Comments sorted by
  • CREATE TABLE Employee
    (
    EmpID int PRIMARY KEY,
    EmpName varchar (50) NOT NULL,
    Salary int NOT NULL,
    Address varchar (200) NOT NULL,
    )
    GO
    INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(1,'Mohan',12000,'Noida')
    INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(2,'Pavan',25000,'Delhi')
    INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(3,'Amit',22000,'Dehradun')
    INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(4,'Sonu',22000,'Noida')
    INSERT INTO Employee(EmpID,EmpName,Salary,Address) VALUES(5,'Deepak',28000,'Gurgaon')
    GO
    SELECT * FROM Employee

  • DECLARE @AccountID INT
    DECLARE @getAccountID CURSOR
    SET @getAccountID = CURSOR FOR
    SELECT Account_ID
    FROM Accounts
    OPEN @getAccountID
    FETCH NEXT
    FROM @getAccountID INTO @AccountID
    WHILE @@FETCH_STATUS = 0
    BEGIN
    PRINT @AccountID
    FETCH NEXT
    FROM @getAccountID INTO @AccountID
    END
    CLOSE @getAccountID
    DEALLOCATE @getAccountID

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

    Optimum Creative