General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Working with cursors
I know how to create a cursor and print to screen, however I need to be able to use these cursors as an output to excel and also as columns instead of rows. The code used is set out below, can anyone expand on this?
DECLARE @startdate DATETIME, @enddate DATETIME
DECLARE Over_cursor CURSOR FOR
SELECT ah_date
FROM actionhistory ah
INNER JOIN actions ON ac_id = ah_actionid
WHERE ah_claimid = (SELECT cl_id FROM claim WHERE cl_clref '0001')
AND ac_short = 'VOVERPAY'
ORDER BY ah_date
OPEN Over_cursor
-- Get the first date
FETCH NEXT FROM Over_cursor
INTO @startdate
FETCH NEXT FROM Over_cursor
INTO @enddate
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT 'Start date'
PRINT @startdate
PRINT 'End date'
PRINT @enddate
PRINT ''
SET @startdate = @enddate
FETCH NEXT FROM Over_cursor
INTO @enddate
IF @@FETCH_STATUS <> 0
BEGIN
SET @enddate = GETDATE()
END
END
PRINT 'Start date'
PRINT @startdate
PRINT 'End date'
PRINT @enddate
PRINT ''
CLOSE Over_cursor
DEALLOCATE Over_cursor
GO
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only