2013-02-14 25 views
0

好吧,我已经为这个问题奋斗了近一个星期了,我不能为我的生活找出问题所在。BCP查询命令创建文本文件,但在事务中的作业步骤中执行时挂起

问题:BCP实用程序创建txt文件,但没有任何反应后。该文件只是坐在那里,是空白的。 BCP几乎挂起。我必须结束流程才能制止它。 BCP命令位于作业步骤内的事务内部的存储过程中。如果我将这个存档文件本身并在管理工作室中运行,则创建该文件时不会出现问题。如果我创建了一个SQL作业,并且只是运行BCP命令的sproc,它也可以。

这是作业步骤:

BEGIN TRANSACTION 

BEGIN TRY 

EXEC dbo.DataManipulation1 
EXEC dbo.DataManipulation2 
EXEC dbo.DataManipulation3 
EXEC dbo.DataManipulation4 
EXEC dbo.DataManipulation5 

EXEC dbo.spCreateFiles 0 

EXEC dbo.spSendEmail 'PASS' 

COMMIT TRANSACTION 
END TRY 

BEGIN CATCH 

ROLLBACK TRANSACTION 

EXEC dbo.spGetDatabaseErrorInfo 
EXEC dbo.spCreateFiles 1 
EXEC dbo.spSendEmail 'FAIL' 
END CATCH 

这里的spCreateFiles存储过程。高级别概述:Sproc生成系统文件夹,然后对txt文件执行查询。而已。如果传递给sproc的参数是0,它将根据sproc的执行生成文件,如果该参数不是0,则会生成空白文件。共有4个文件。由于显而易见的原因,BCP命令的用户名和密码被删除。从我在线阅读的部分内容来看,这可能是因为某些东西在锁定文件...也许是sproc或SQL作业,甚至是转换,然后当BCP实用程序尝试使用它时,什么都不会发生。

ALTER PROCEDURE [dbo].[spCreateFiles] @errorCode BIT 
AS 
    BEGIN 
     BEGIN TRY 
      SET NOCOUNT ON; 

      DECLARE @year CHAR(4) 
      DECLARE @month CHAR(2) 
      DECLARE @day CHAR(2) 

      DECLARE @rootDir VARCHAR(200) 
      DECLARE @yearDir VARCHAR(200) 
      DECLARE @monthDir VARCHAR(200) 
      DECLARE @dayDir VARCHAR(200) 
      DECLARE @dirsTable TABLE (directory VARCHAR(200)) 

      DECLARE @baseFileName VARCHAR(8) 
      DECLARE @detailFile VARCHAR(500) 
      DECLARE @detailNydFile VARCHAR(500) 
      DECLARE @summaryFile VARCHAR(500) 
      DECLARE @summaryNydFile VARCHAR(500) 

      DECLARE @cmdQueryout VARCHAR(2000) 

      SET @rootDir = 'C:\Test\' 
      SET @year = DATEPART(YEAR, GETDATE()) 
      SET @month = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(MONTH, GETDATE())), 2) 
      SET @day = RIGHT('00' + CONVERT(NVARCHAR(2), DATEPART(DAY, GETDATE())), 2) 
      SET @yearDir = @rootDir + @year + '\' 
      SET @monthDir = @rootDir + @year + '\' + @year + @month + '\' 
      SET @dayDir = @rootDir + @year + '\' + @year + @month + '\' + @year + @month + @day + '\' 
      SET @baseFileName = @year + @month + @day 

      PRINT @rootDir 

      PRINT @year 
      PRINT @month 
      PRINT @day 

      PRINT @yearDir 
      PRINT @monthDir 
      PRINT @dayDir 

      PRINT @baseFileName 

      INSERT INTO @dirsTable 
        EXEC master.dbo.xp_subdirs 
         @rootDir 

      IF NOT EXISTS (SELECT directory 
          FROM @dirsTable 
          WHERE directory = @year) 
       EXEC master.sys.xp_create_subdir 
        @yearDir 

      DELETE FROM @dirsTable  

      INSERT INTO @dirsTable 
        EXEC master.dbo.xp_subdirs 
         @yearDir 

      IF NOT EXISTS (SELECT directory 
          FROM @dirsTable 
          WHERE directory = @month) 
       EXEC master.sys.xp_create_subdir 
        @monthDir 
      DELETE FROM @dirsTable 

      INSERT INTO @dirsTable 
        EXEC master.dbo.xp_subdirs 
         @monthDir 

      IF NOT EXISTS (SELECT directory 
          FROM @dirsTable 
          WHERE directory = @day) 
       EXEC master.sys.xp_create_subdir 
        @dayDir 
      DELETE FROM @dirsTable 

      SET @detailFile = @dayDir + @baseFileName + ' Detail.txt' 
      SET @detailNydFile = @dayDir + @baseFileName + ' Detail_NYD.txt' 
      SET @summaryFile = @dayDir + @baseFileName + ' Summary.txt' 
      SET @summaryNydFile = @dayDir + @baseFileName + ' Summary_NYD.txt' 

      PRINT @detailFile 
      PRINT @detailNydFile 
      PRINT @summaryFile 
      PRINT @summaryNydFile 

      IF @errorCode = 0 
       BEGIN  
        PRINT 'Error Code: ' + CAST(@errorCode AS CHAR(1)) 
        SET @cmdQueryout = 'bcp "EXEC DB_NAME.dbo.spGetDetailRecords" queryout "' + @detailFile + '" -c -Uusername -Ppassword' 
        PRINT @cmdQueryout 

        EXEC master..xp_cmdshell 
         @cmdQueryout 

        SET @cmdQueryout = 'bcp "EXEC DB_NAME.dbo.spGetDetailNYDRecords" queryout "' + @detailNydFile + '" -c -Uusername -Ppassword' 
        PRINT @cmdQueryout 

        EXEC master..xp_cmdshell 
         @cmdQueryout  

        SET @cmdQueryout = 'bcp "EXEC DB_NAME.dbo.spGetSummaryRecords" queryout "' + @summaryFile + '" -c -Uusername -Ppassword' 
        PRINT @cmdQueryout 

        EXEC master..xp_cmdshell 
         @cmdQueryout   

        SET @cmdQueryout = 'bcp "EXEC DB_NAME.dbo.spGetSummaryNYDRecords" queryout "' + @summaryNydFile + '" -c -Uusername -Ppassword' 
        PRINT @cmdQueryout 

        EXEC master..xp_cmdshell 
         @cmdQueryout 
       END 
      ELSE 
       BEGIN 
        SET @cmdQueryout = 'bcp "SELECT NULL" queryout "' + @detailFile + '" -c -Uusername -Ppassword' 
        EXEC master..xp_cmdshell 
         @cmdQueryout 

        SET @cmdQueryout = 'bcp "SELECT NULL" queryout "' + @detailNydFile + '" -c -Uusername -Ppassword'     
        EXEC master..xp_cmdshell 
         @cmdQueryout  

        SET @cmdQueryout = 'bcp "SELECT NULL" queryout "' + @summaryFile + '" -c -Uusername -Ppassword' 
        EXEC master..xp_cmdshell 
         @cmdQueryout   

        SET @cmdQueryout = 'bcp "SELECT NULL" queryout "' + @summaryNydFile + '" -c -Uusername -Ppassword'  
        EXEC master..xp_cmdshell 
         @cmdQueryout 
       END  
     END TRY 
     BEGIN CATCH 
      EXEC dbo.spGetDatabaseErrorInfo 
     END CATCH 
    END 

回答

3

我明白了这个问题。

在事务处于打开状态时,我无法使用BCP实用程序。我必须承诺交易,然后使用BCP。

相关问题