Results 1 to 7 of 7

Thread: [RESOLVED] Cursor stays on first record in table

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Resolved [RESOLVED] Cursor stays on first record in table

    Hello!! I am using SQL Server 2008 and I am attempting to create a stored procedure to Truncate certain tables in my database so that I can rewrite records to those tables on a nightly basis from Access tables. The problem I am having is that my fetch always returns only the first record from my table (AccessCopyTables). Can someone please tell me what I am doing wrong? Here is the sproc:

    Code:
    USE [Mercury]
    GO
    /****** Object:  StoredProcedure [dbo].[TruncTables]    Script Date: 04/12/2010 14:52:30 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    ALTER PROCEDURE [dbo].[TruncTables] AS
    DECLARE @TName nchar(30)
    DECLARE @query nvarchar(max)
    DECLARE @curs cursor
    
    SET @curs = CURSOR FOR SELECT TableName FROM AccessCopyTables
    OPEN @curs
    FETCH  NEXT FROM @curs INTO @TName
    WHILE @@FETCH_STATUS = 0
    	BEGIN
    		PRINT 'Table Name ' + @TName
    		set @query = N'TRUNCATE TABLE ' + @TName
    		exec sp_executesql @query
    	END
    	FETCH NEXT FROM @curs INTO @TName
    END
    CLOSE @curs
    DEALLOCATE @curs
    Thanks for the help in advance!!
    Last edited by conwa02976; Apr 12th, 2010 at 02:23 PM.

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Cursor stays on first record in table

    Are all the table names exactly 20 chars?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Re: Cursor stays on first record in table

    No, anywhere from 6 characters to 30.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Cursor stays on first record in table

    Then I would not use nchar for the tablename var but NVARCHAR(30)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Re: Cursor stays on first record in table

    I will try that next. I had not tried to actually compile the sproc yet so I just tried it and I got an error. I got an "Incorrect syntax near the keyword 'CLOSE'. There is a squiggly line there and when I hover over it I get: Expecting CONVERSATION. Where in the world is that coming from?

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Cursor stays on first record in table

    Here is an example I use to drop some tables

    sql Code:
    1. DECLARE
    2.     @TNAme VARCHAR(255) ,
    3.     @sql NVARCHAR(MAX)
    4. DECLARE GettmTables_cursor CURSOR LOCAL FAST_FORWARD FOR
    5. SELECT LTRIM(RTRIM(TABLE_NAME)) FROM INFORMATION_SCHEMA.tables
    6. WHERE TABLE_NAME LIKE 'tmp%' AND TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'dbo' AND TABLE_NAME <> 'TMPLATE'
    7. OPEN GettmTables_cursor
    8. FETCH NEXT FROM GettmTables_cursor INTO @TName
    9. WHILE @@FETCH_STATUS = 0
    10.     BEGIN
    11.         SET @sql = 'DROP TABLE ' + @TNAme
    12.         EXECUTE sys.sp_executesql
    13.             @sql
    14.         FETCH NEXT FROM GettmTables_cursor INTO @TName
    15.     END
    16. CLOSE GettmTables_cursor
    17. DEALLOCATE GettmTables_cursor
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    93

    Re: Cursor stays on first record in table

    Gary, thanks for the help. I copied your sproc over mine and updated the select statement with my select statement and it worked like a charm. I see a few syntax differences but nothing really major so I still don't know what was wrong with my original but it doesn't matter now since yours works.
    I will mark this as closed!!

Posting Permissions

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



Click Here to Expand Forum to Full Width