Results 1 to 7 of 7

Thread: cursur

  1. #1
    Join Date
    Feb 2010
    Posts
    9

    Question Unanswered: 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 03:15. Reason: different tables for different cursurs

  2. #2
    Join Date
    Dec 2007
    Location
    London, UK
    Posts
    741
    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.

  3. #3
    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?

  4. #4
    Join Date
    Sep 2001
    Location
    Chicago, Illinois, USA
    Posts
    601
    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

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    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:
    pootle flump
    ur codings are working excelent.

  6. #6
    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 !!

  7. #7
    Join Date
    Feb 2010
    Posts
    9
    no need i have done it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •