Tuesday, September 23, 2008

Cursor Use

This is my First Blog on Database, that how to use the Cursor.
simple example of cursor is:-


--Copy Department for ScenarioId
DECLARE Cur_Department CURSOR
FOR SELECT DepartmentID FROM Department
WHERE ScenarioID=@intScenarioID AND (Deleted IS NULL OR Deleted=0)
OPEN Cur_Department
FETCH NEXT FROM Cur_Department INTO @intOldDepartmentID
WHILE @@FETCH_STATUS=0
BEGIN
INSERT INTO Department
(
ScenarioID,
DepartmentName,
DepartmentX,
DepartmentY,
DepartmentW,
DepartmentDescription
)
SELECT @intNewScenarioID,
DepartmentName,
DepartmentX,
DepartmentY,
DepartmentW,
DepartmentDescription
FROM Department
WHERE ScenarioID=@intScenarioID AND DepartmentID=@intOldDepartmentID AND (Deleted IS NULL OR Deleted=0)
FETCH NEXT FROM Cur_Department INTO @intOldDepartmentID
END
CLOSE Cur_Department
DEALLOCATE Cur_Department


this simple example show how to copy the previous record.

Thanks And Regards
MAHINDRAKAR G GANGADHAR