Results 1 to 7 of 7

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

Threaded View

  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.

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