Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2004
    Posts
    29

    Unanswered: Printing inside a cursor

    How can I print the variable @sum_points to the screen or save it to a file (test.txt).

    ----------------

    DECLARE cons_cursor_1 CURSOR FOR
    SELECT a.points
    FROM player_stats_history a, employee b, game c
    WHERE a.game_id like '00203%' AND
    a.employee_id = b.employee_id AND
    a.game_id = c.game_id AND
    c.date_est >= 'Mar 22, 2004' AND
    c.date_est <= 'Mar 28, 2004' AND
    a.employee_id = 708
    GO

    DECLARE @counter integer
    DECLARE @game_points integer
    DECLARE @sum_points integer

    SELECT @counter = 0
    SELECT @game_points = 0
    SELECT @sum_points = 0

    OPEN cons_cursor_1
    FETCH cons_cursor_1 INTO @game_points

    WHILE @@sqlstatus = 0
    BEGIN

    SELECT @sum_points = @sum_points + @game_points
    FETCH cons_cursor_1 INTO @game_points

    END


    CLOSE cons_cursor_1
    DEALLOCATE CURSOR cons_cursor_1

  2. #2
    Join Date
    Nov 2002
    Posts
    207
    Using a select or print statement.
    To output to a file, use redirecting the output using ">" symbol.

  3. #3
    Join Date
    Apr 2004
    Posts
    29
    Thanks, can you give me an example of this syntax, with a print statement.

  4. #4
    Join Date
    Nov 2002
    Posts
    207
    declare @aa integer
    select @aa = 123
    print "Value of AA: %1!", @aa

    Output:

    Value of AA: 123
    (1 row affected)

  5. #5
    Join Date
    Apr 2004
    Posts
    29
    Thank you very much.
    One last question, how would you print a decimal value, such as 10.45

  6. #6
    Join Date
    Nov 2002
    Posts
    207
    declare @aa float
    select @aa = 123.45
    print "Value of AA: %1!", @aa

    Output:

    Value of AA: 123.45
    (1 row affected)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •