0

我正在使用 SSIS 遍历文件夹并将所有 txt 文件的内容读入数据库。首先,我正在检查文件是否已使用存储过程进行处理

ALTER PROC [dbo].[CheckForDuplicateFileEntry]
(
  @TaskID INT,
  @Filename VARCHAR(50),
  @FileAlreadyExists BIT OUTPUT
)AS 
BEGIN

    DECLARE @TaskTypeID INT
    SET @FileAlreadyExists = 0

    SELECT @TaskTypeID = TaskTypeID FROM Tasks WHERE TaskID = @TaskID

    IF EXISTS ( SELECT  TaskID
                FROM    TaskSteps
                WHERE   @Filename IN (
                        SELECT TOP ( 30 )
                                TaskSteps.Filename
                        FROM    TaskSteps
                                INNER JOIN Tasks ON TaskSteps.TaskID = Tasks.TaskID
                        WHERE   ( Tasks.TaskTypeID = @TaskTypeID
                                  AND [Filename] IS NOT NULL
                                )
                                AND IsValid = 1
                                AND ProcessStatusID = 2 ) ) 
        BEGIN              
            INSERT  INTO TaskSteps ( TaskID, StepDesc )
            VALUES  (
                      @TaskID,
                      'Duplicate filename. (' + @Filename + ') Already exists.' 
                    )                
            SET @FileAlreadyExists = 1                  
        END        
END

我也试过

IF EXISTS (SELECT TOP 30 Filename
       FROM  TaskSteps  INNER JOIN Tasks ON TaskSteps.TaskID = Tasks.TaskID
       WHERE (SUBSTRING(TaskSteps.Filename,18,13) = SUBSTRING(@Filename,18,13))
             AND IsValid = 1                            
             AND ProcessStatusID = 2)

但是当它遍历文件时,它处理的第一个声明下一个是重复的,处理第三个并声明第四个重复的文件等等文件名非常相似,即 Songs_120501_175535.txt 文件名保持不变,日期和时间部分是唯一改变的部分,可能只改变一位数,即 Songs_120502_175535.txt

4

2 回答 2

1

这就像我能得到的一样简单。我会检查 SSIS 进程或触发器等中的其他内容是否正在删除或更新 TaskSteps/Task 表,这导致它有时工作,有时不工作。还要检查IsValidProcessStatusID列,因为它们在查询中使用。尝试在 Management Studio 中使用相同的参数运行您的查询三、四、五次 - 并@FileAlreadyExists在每次之后打印出值。如果只是调用 proc 正常工作,那么问题出在 SSIS 的其他地方。

ALTER PROC [dbo].[CheckForDuplicateFileEntry]
(
  @TaskID INT,
  @Filename VARCHAR(50),
  @FileAlreadyExists BIT OUTPUT
)AS 
BEGIN

SET @FileAlreadyExists = 0

IF EXISTS( 
           SELECT     * 
           FROM       TaskSteps
           INNER JOIN Tasks 
           ON         TaskSteps.TaskID = Tasks.TaskID
           WHERE      Tasks.TaskTypeID = TaskSteps.TaskTypeID
           AND        Tasks.TaskID = TaskSteps.TaskID
           AND        IsValid = 1
           AND        ProcessStatusID = 2
           AND        TaskSteps.Filename = @FileName
         )
    BEGIN              
        INSERT  INTO TaskSteps ( TaskID, StepDesc )
        VALUES  (
                  @TaskID,
                  'Duplicate filename. (' + @Filename + ') Already exists.' 
                )                
        SET @FileAlreadyExists = 1                  
    END        
END
于 2012-06-26T10:33:46.817 回答
1

尝试这个

使用字符索引函数

IF EXISTS (SELECT TOP 30 Filename
           FROM  TaskSteps  INNER JOIN Tasks ON TaskSteps.TaskID = Tasks.TaskID
           WHERE (SUBSTRING(TaskSteps.Filename,CHARINDEX('_',TaskSteps.Filename),13) =    
                  SUBSTRING(@Filename,CHARINDEX('_',@Filename),13) ) 
                 AND IsValid = 1                            
                 AND ProcessStatusID = 2)
于 2012-06-26T10:35:01.993 回答