what is the best alternative for cursors and while loops in SQL server stored procedure?

  • What is the best way to replace cursors and while loops in sql server stored procedure?

    I want to insert data from one table from another table in a same server.what is the best way to transfer the data without loops and cursors..,

    I need to use some business logic for the transformation..,

    Pls suggest me how to avoid Cursors in my stored procedure...:-)

  • Is there more logic behind it than just "insert data from one table from another table"?

    If not, a simple INSERT INTO ... SELECT will do just fine.

    INSERT (Transact-SQL)

    If yes, please elaborate more on this logic.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Or SSIS could do as well.... hard to say without any specifics.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • As stated, it depends. But here are options that I use:

    1) I read the data into a table variable or cte and do processing, then insert it into the new table.

    2) Most recently, I utilized a MERGE statement to do the work.

  • We cannot help you without LOTS more information. I also think this may go way beyond a forum-level assistance need. Consider getting a professional on board to help you figure out what you need and help you get there.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Please give more info, but why would you use a loop for something like that?

    Never stop learning or you will be left behind.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply