If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > cursur

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-06-10, 01:50
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
Question cursur

i am using 4 different cursurs to retrive the value.
but i want to use 1 cursur
help me!!!!!!!

thanks,



LGDD CURSOR for t1
DMUI CURSOR for t2
IMP CURSOR for t3

DMC CURSOR for t4


OPEN LGDD
FETCH NEXT FROM LGDD INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL =

EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM LGDD INTO @TableName
END
CLOSE LGDD
DEALLOCATE LGDD


OPEN DMUI
FETCH NEXT FROM DMUI INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL =

EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM DMUI INTO @TableName
END
CLOSE DMUI
DEALLOCATE DMUI


OPEN IMP
FETCH NEXT FROM IMP INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL =
EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM IMP INTO @TableName
END
CLOSE IMP
DEALLOCATE IMP


OPEN DMC
FETCH NEXT FROM DMC INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL =

EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM DMC INTO @TableName
END
CLOSE DMC
DEALLOCATE DMC

Last edited by shantanu4u; 02-06-10 at 02:15. Reason: different tables for different cursurs
Reply With Quote
  #2 (permalink)  
Old 02-06-10, 04:33
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Your code has numerous errors and doesn't make any sense to me. Could you explain exactly what you are trying to achieve. Don;'t tell us how you intend to do it. Tell us what end result you want.
Reply With Quote
  #3 (permalink)  
Old 02-06-10, 04:44
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
i am using 4 cursurs
instead of that i want to use 1.

i want to get top 1 entry of 4 different tables according to logdate.

my cursur gives me exact result.

but the problem is i am using 4 cursurs

DECLARE LGDD CURSOR FOR select name from sys.Tables where name like '%LogLGDataCL%'--LGDD

DECLARE DMUI CURSOR FOR select name from sys.Tables where name like '%LogUIDDC%'
or name like '%LogUIEDC%' and name not like '%LogUIEDC8%'--DMUI

DECLARE IMP CURSOR FOR select name from sys.Tables where name like '%LogIMP%'


DECLARE DMC CURSOR FOR select name from sys.Tables where name like '%LogDMC%'



OPEN LGDD
FETCH NEXT FROM LGDD INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @SQL =
'SELECT top 1 * FROM [' + @TableName + ']order by logdate desc'
EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM LGDD INTO @TableName
END
CLOSE LGDD
DEALLOCATE LGDD

three more---------loops for remaining 3 cursurs.


it works perfect.

but can we use single cursur within for loop
instead of creating 4 diff cursurs?
Reply With Quote
  #4 (permalink)  
Old 02-06-10, 09:06
PracticalProgram PracticalProgram is offline
Registered User
 
Join Date: Sep 2001
Location: Chicago, Illinois, USA
Posts: 551
Is the result set, after running all four cursors, supposed to be a single record, four records, or more than four records (depending upon the total number of table names returned in all of the cursors)?

Are you using the wildcard characters in the cursor definitions because, at execution time, you do not know the names of the tables? Or is it because you want to return more than one record in any, several, or all, of the cursors?

I'm just a beginner at this, but I can tell you that cursors are the very last option to returning a dataset. I see no reason why this entire logic could not be written in a single SELECT statement, which is far preferable to these four cursors.
__________________
Ken

Maverick Software Design

(847) 864-3600 x2
Reply With Quote
  #5 (permalink)  
Old 02-08-10, 05:37
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
How about no cursors?
Code:
DECLARE    @sql    AS NVARCHAR(4000)

SELECT  @sql    = COALESCE(@sql, N'') + N'SELECT top 1 * FROM [' + name + '] ORDER BY logdate DESC; '
FROM    sys.Tables
WHERE   name LIKE '%LogUIDDC%' 

EXEC    dbo.sp_executesql @sql
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #6 (permalink)  
Old 02-10-10, 01:52
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
thanks,

it gives me same result as my cursur.
but your idea is better than me.

now i want to save that result into a table.
please help me!1

thanks !!
Reply With Quote
  #7 (permalink)  
Old 02-10-10, 02:10
shantanu4u shantanu4u is offline
Registered User
 
Join Date: Feb 2010
Posts: 9
no need i have done it.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On