2013-02-14 60 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。

相關問題