Click here to Skip to main content
15,889,820 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..
how to create simple cursor in ms sql server and mysql like oracle,i know in oracle following is example
SQL
DECLARE
    CURSOR C IS SELECT * FROM tbinstrument;
BEGIN
    FOR CV IN C
    LOOP
        INSERT INTO tbnseinstrument VALUES(CV.ins_id,CV.ins_name);
    END LOOP;
END;
/
Posted

1 solution

The cursor is quite similar in other systems. In Sql Server, have a look at DECLARE CURSOR[^]

So your cursor definition would look something like:
SQL
DECLARE c CURSOR FOR SELECT * FROM tbinstrument;

In MySql, have a look at Cursor DECLARE Syntax[^]
 
Share this answer
 
v2
Comments
chetankhatri 12-Jan-13 5:39am    
if i want to fetch rows into variable and with help of variable how can i access column value in ms sql server like following in oracle

DECLARE
CURSOR C IS SELECT * FROM TBTEST;
CV C%ROWTYPE;
BEGIN
OPEN C;
LOOP
FETCH C INTO CV;
EXIT WHEN C%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(CV.FIRST_NAME||CV.MID_NAME);

END LOOP;
CLOSE C;

END;
/
Wendelius 12-Jan-13 5:49am    
There's no direct equivalent for ROWTYPE in Sql Server. One way is to declare a table type variable, see DECLARE[^] or to list the columns and fetch the values into separate variables.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900