Print out cursor values

I am modifying a stored procedure which is using a cursor, I want to be able to print out or output the values from the cursor that is inserting into a table but am having a hard time trying to figure out how to do this. I need to check all the values for example that get inserted in the orders table by this stored procedure.

I cant do

  • Transfer data from one database to another database
  • Query returning users, first record and last record
  • How to handle sql transaction in this scenario?
  • Set Locale of a SQL Server 2005
  • MS SQL connection with PHP fatal error
  • SQL Server equivalent of MySQL's NOW()?
  • Print @prodId, @orderQuantity, @orderDate, @orderLocationId, @shipmentId,  @shipmentDate

    How can I output/print all the values the cursor has in these columns for each record it goes through? An example with how to do this in code would be appreciated.

    4 Solutions collect form web for “Print out cursor values”

    You can capture & store the inserted rows in the correct order by outputting them into a temp table with an identity;

    create table #inserted(insertorder int identity(0,1), prodId int, orderQuantity int ... <matches inserted row>)
    --cursor loop
        insert destination_table(prodId, orderQuantity, ...)
           output inserted.* into #inserted   /*output inserted.* will just select it */
        values @prodId, @orderQuantity ...
    --end loop
    --inserted rows;
    select * from #inserted order by insertorder 

    If using MS SQL Server then you could use RAISERROR. On this link ypu have a good explanation with samples on how to use them and differences between them. Check out RAISERROR sintaxis here


         PRINT STR(@prodId) + STR(@orderQuantity) + STR(@orderDate) + STR(@orderLocationId) + STR(@shipmentId) + STR(@shipmentDate)

    If that doesnt work then (for the non int variables) try

        CAST(@orderDate as NVARCHAR(MAX))

    Also look at this article.

    Best of Luck

    When using PRINT, a simple trick instead of STR() which doesn’t exist, or CONVERT(varchar(50), @orderQuantity)
    Which is cumbersome, just use RTRIM(@orderQuantlty) for fast implicit conversion from numeric to string.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.