SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Cursor Help


SQL Cursor Help

Author
Message
DBayliss
DBayliss
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 157
I need to amend the below Cursor to output into a table off which I can Query/join the output of the cursor etc . I'm new to cursors so any help and explanation would be greatly appreciated.

DECLARE @SQL varchar(MAX)
DECLARE @TABLENAME varchar(MAX)
DECLARE @Call_Cursor Cursor

SET @CALL_CURSOR = CURSOR FAST_FORWARD FOR
SELECT
[SO].[Name]
FROM sys.sysobjects AS [SO]
WHERE [SO].[Xtype] = 'U'
AND [SO].[name] NOT IN
(
'Queues'
,'Statuses'
,'Banned_Numbers'
,'SMS_Settings'
,'Predictive_Dialler_Detail'
,'Predictive_Dialler_Stats'
,'Blocked_Email_Addresses'
,'Contact_List'
,'CampaignSettings'
,'CampaignCall'
,'Extensions'
,'MessageSummary'
,'Files_Status'
,'Teams'
,'Agents'
,'convalues'
,'sysdiagrams'
)
ORDER BY [SO].[name]

OPEN @Call_Cursor

FETCH NEXT FROM @Call_Cursor INTO @TABLENAME
SET @SQL = ''
WHILE (@@FETCH_STATUS != -1)
BEGIN

SET @SQL = @SQL + 'SELECT ''' + @TABLENAME + ''' AS [User],CONVERT(Datetime,CONVERT(varchar(10),[Start_Time],103),121) AS [Date], CONVERT(Datetime,[Start_Time],121) AS [Start_Time],CONVERT(Datetime,[End_Time],121) AS [End_Time],[Record_Type],[User_Action],[Direction],[Action_Data],[From_addr],[To_addr],[Subject],[Detail],[Company],[Contact],[FullPath],[dbo].[svfGetFormattedTime] (CONVERT(Datetime,[Start_Time],121),CONVERT(Datetime,[End_Time],121)) AS [Duration] FROM [dbo].[' + @TABLENAME + '] WHERE [Record_Type] <> ''Predictive'' AND CONVERT(Datetime,[Start_Time],121) >= ''' + CONVERT(varchar(20),@DateFrom,113) + ''' AND CONVERT(Datetime,[Start_Time],121)<= ''' + CONVERT(varchar(20),@DateTo, 113) + ''' AND CONVERT(Datetime,[End_Time],121) >= ''' + CONVERT(varchar(20),@DateFrom,113) + ''' AND CONVERT(Datetime,[End_Time],121)<= ''' + CONVERT(varchar(20),@DateTo, 113) + ''''

FETCH NEXT FROM @Call_Cursor INTO @TABLENAME
IF @@FETCH_STATUS != -1
BEGIN
SET @SQL = @SQL + ' UNION ALL '
END
END
EXEC (@SQL)
SELECT @SQL
CLOSE @Call_Cursor
DEALLOCATE @Call_Cursor
END
Cadavre
Cadavre
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14699 Visits: 8511
Create a table, then change this: -
EXEC (@SQL)
SELECT @SQL



To this: -
INSERT INTO yourTable
EXEC (@SQL);



If you're doing this to learn the syntax for a cursor, then ignore the next part of this post. Otherwise, you could remove the cursor altogether by building your dynamic SQL directly and concatenating with FOR XML. If you need help doing this, let me know.


Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
DBayliss
DBayliss
SSC-Enthusiastic
SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)SSC-Enthusiastic (152 reputation)

Group: General Forum Members
Points: 152 Visits: 157
Thanks for your response, just what I needed.
Sean Pearce
Sean Pearce
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6270 Visits: 3436
Note, @@FETCH_STATUS contains one of the following values:

0: The FETCH statement was successful.
-1: The FETCH statement failed or the row was beyond the result set.
-2: The row fetched is missing.

Therefore it's best to use
WHILE @@FETCH_STATUS = 0





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Sean Lange
Sean Lange
SSC Guru
SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)SSC Guru (101K reputation)

Group: General Forum Members
Points: 101535 Visits: 18186
Artoo22 (9/11/2012)
Note, @@FETCH_STATUS contains one of the following values:

0: The FETCH statement was successful.
-1: The FETCH statement failed or the row was beyond the result set.
-2: The row fetched is missing.

Therefore it's best to use
WHILE @@FETCH_STATUS = 0





I would suggest it is better to not use a cursor for this. :-D A cursor is the slow way to get this data.

In those rare cases where a cursor is needed it is however much better to use the check like our little droid recommends.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search