Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Stored Procedure In T-SQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-04-27 : 09:09:29
nibedita writes "I am having a problem in the syntax of Cursors to be used in a transact SQL script.

The For Loops are not supported in the SQL Server. How do we replace the following code in PL/SQL to migrate it to T-SQL:

Declare Cursor SubOrdinates
Select posnid
From collegue_view
Where SuprorId = p_IniSelfId
v_cSubOrdGoal Varchar(200)
Begin
For SubOrd In SubOrdinates
Loop
Exit When SubOrdinates%NOTFOUND
Begin
Select Var_spror_goal_id
Into v_cSubOrdGoal
From Tao_GoalSet_Tran
Where Num_dfnr_posn_id = Subord.posnid
And Num_dfne_posn_id = Subord.posnid
v_cFamilyGoal = v_cFamilyGoal + v_cSubOrdGoal
if SQL%ROWCOUNT=0 then
null
End If
End
End Loop
End


Thanks In Advance"

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-04-27 : 09:19:23
T-SQL uses a WHILE construct to perform loops:

WHILE @ctr<10
BEGIN
INSERT INTO myTable (col1) VALUES (@ctr)
SET @ctr=@ctr+1
END


You really can't convert PL/SQL to T-SQL, they are different languages even though there are many similarities. I think you posted something similar here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=15343

In fact, if you did convert this to T-SQL, it would not be as efficient as a set based solution would be. From what I can tell, it appears that you are building a string from multiple rows of data by consolidating them. We have an article here that describes how to do it without using cursors:

http://www.sqlteam.com/item.asp?ItemID=2368

And this thread also has some techniques:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9978

In fact, the latter thread could very likely work in Oracle as a pure SQL batch, without needing a cursor at all. I don't know if it will be faster, but it might be.

Go to Top of Page
   

- Advertisement -