Dynamic Select in cursor

  • Hi sorry for question, but I didn`t find answer.

    can i do something like;

    declare @i int

    cursor

    begin

    select [ @i] from table -- or some other name that i can select like [a1] = [a@1]

    SET @i = @i + 1

    end;

    i have Excel with 1000 mixed up data which I have to order in SQL.

    Thanks for answer.

  • Be aware that cursors are not always the best way of doing things.

    Here is the standard cursor code. Promise not to abuse it 😉

    DECLARE @i INT;

    DECLARE @COUNT INT = 0;

    DECLARE myCursorName CURSOR FAST_FORWARD FOR

    SELECT i FROM yourTable WHERE ... etc ...

    OPEN myCursorName

    FETCH NEXT FROM myCursorName INTO @i

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @COUNT = @COUNT + @i;

    PRINT 'Value = ' + CAST(@COUNT AS VARCHAR)

    FETCH NEXT FROM myCursorName INTO @i

    END

    CLOSE myCursorName;

    DEALLOCATE myCursorName;

  • gorenak (9/14/2012)


    Hi sorry for question, but I didn`t find answer.

    can i do something like;

    declare @i int

    cursor

    begin

    select [ @i] from table -- or some other name that i can select like [a1] = [a@1]

    SET @i = @i + 1

    end;

    i have Excel with 1000 mixed up data which I have to order in SQL.

    Thanks for answer.

    Please post your actual requirement as suggested in the solution that Cursor or loop is not always the best approach.

    --rhythmk
    ------------------------------------------------------------------
    To post your question use below link

    https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
    🙂

  • hi

    this code doesn`t work

    my table look like:

    1 2 3.. n

    a c d g

    b d f g

    I will go with cursor over collums and do string stuff. I understand that this aproach is not the best. But this stored proc will be used only once.

    I change and still don`t understand SELECT [@i] what is syntax here.

    DECLARE @i INT;

    DECLARE @COUNT INT

    SET @COUNT = 0;

    DECLARE myCursorName CURSOR FAST_FORWARD FOR

    SELECT [ @i ] --

    --FROM yourTable WHERE ... etc ...

    OPEN myCursorName

    FETCH NEXT FROM myCursorName INTO @i

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @COUNT = @COUNT + @i;

    PRINT 'Value = ' + CAST(@COUNT AS VARCHAR)

    FETCH NEXT FROM myCursorName INTO @i

    END

    CLOSE myCursorName;

    DEALLOCATE myCursorName;

  • SELECT a column from your table, then put it into the @Variable in the FETCH Statement.

    Here is a full example.

    CREATE TABLE #Tmp (

    i INT IDENTITY(1,1),

    val VARCHAR(50) );

    INSERT INTO #Tmp (val)

    SELECT 'Sql' UNION

    SELECT 'Server' UNION

    SELECT 'Central'

    DECLARE @i INT;

    DECLARE @val varchar(50);

    DECLARE @COUNT INT = 0;

    DECLARE myCursorName CURSOR FAST_FORWARD FOR

    SELECT i, val FROM #Tmp ORDER BY val

    OPEN myCursorName

    FETCH NEXT FROM myCursorName INTO @i, @val

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @COUNT = @COUNT + @i;

    PRINT 'Value = ' + @val + ' I= ' + CAST(@i AS VARCHAR) + ' COUNT = ' + CAST(@COUNT AS VARCHAR)

    FETCH NEXT FROM myCursorName INTO @i, @val

    END

    CLOSE myCursorName;

    DEALLOCATE myCursorName;

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

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