s there any difference in using cursor and table?

  • is there any difference in using cursor and table ?

    any performance issues ?

    sample are shown below

    example 1:

    declare rs_cursor CURSOR for select name from master.dbo.sysdatabases

    example 2:

    DECLARE @temp as table (RowID int not null primary key identity(1,1),

    Filename varchar(500))

    insert into @temp select name from master.dbo.sysdatabases

  • Yes there is typically a huge difference depending on what you want to do.

    Cursors are typical RBAR meaning it processes a row at a time, where as with a table it can process a "set" of data at a time which is more effective.

  • ^ This, and they're usually not compared to each other, as they have totally different uses. What you mentioned is a table variable and is more aptly compared to a temp table, while cursors to while loops. You can use a cursor on a temp table, to do a per-row transaction/DML, but not vice versa. A good analogy would be like comparing a subject with an adjective.

  • is it possible to fetch multiple columns (using cursor)

  • sumith1andonly1 (11/8/2012)


    is it possible to fetch multiple columns (using cursor)

    Yes it is possible to fetch multiple columns using cursors. You need to do a little research on Cursors.....the following link would help you a lot:

    MSSQL Cursor

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • sumith1andonly1 (11/8/2012)


    is it possible to fetch multiple columns (using cursor)

    Let's be clear here:

    cursors are sloooooooooooow.

    Only use them if you have a very good reason to use them.

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

  • Koen Verbeeck (11/8/2012)


    sumith1andonly1 (11/8/2012)


    is it possible to fetch multiple columns (using cursor)

    Let's be clear here:

    cursors are sloooooooooooow.

    Only use them if you have a very good reason to use them.

    Yes, I agree. Cursors are slow because they do row by row transactions instead of set based transactions which are much much much faster than row by row transactions.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

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

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