0

This is case is for sybase ase 12.5. No CTE.

suppose I have a a table mytab. I want to go through all rows in this table and do something for each row. One easy solution is cursor like

Declare mycur Cursor For
  Select * from mytab For Read Only

Open mycur 

Fetch mycur Into ...

While @@sqlstatus = 0

Begin
  ....
  insert into tab2(...)
  Fetch mycur Into ...

End

Close mycur

Deallocate Cursor mycur

this solution has bad performance because of cursor.

what's the best solution with best performance?

KentZhou
  • 24,805
  • 41
  • 134
  • 200
  • 4
    Depends on what you want to do for every row. Some things you can do in a single query. Some not. Please be more specific. – juergen d May 15 '14 at 19:33
  • for each row, I need to insert data to another tab. – KentZhou May 15 '14 at 19:37
  • 1
    The best solution might be to not do it at all. Just use the data you already have. What is the overall objective and why did you lament the lack of cte? – Dan Bracuk May 15 '14 at 19:41

1 Answers1

4

To insert data from one table to another you can do

insert into tab2(col1, col2)
Select col4, col7 
from mytab
juergen d
  • 201,996
  • 37
  • 293
  • 362