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
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
Which is cumbersome, just use
RTRIM(@orderQuantlty) for fast implicit conversion from numeric to string.