0

好吧,自从我上一篇文章以来,我能够取得进步!但是,我遇到了另一个问题......再次:

目标:传入两个参数(一个任务和一个主键)以生成表列表。获取列表,然后动态构造插入语句,目的是将数据从生产环境复制到测试环境。换句话说,以编程方式执行 'EDIT TOP 200' 所做的......但要快得多。

问题(更新):@tmpInserVars 不会随着每次迭代而更新。它在第一次输入代码时被设置,并且永远不会刷新。

迄今:

USE MAINDB
DECLARE @PK int = 1000,
 @TaskName nvarchar(50) = 'TASK', 
 @curTable nvarchar(75),
 @curRow nvarchar(75),
 @tmpStatement nvarchar(500),
 @tmpInsert nvarchar(500)

RAISERROR('Retrieving Tables',0,1) WITH NOWAIT
 DECLARE TableCursor CURSOR LOCAL FOR 

    SELECT DISTINCT TOP 2 PRMPTTBL.tTable as PromptTable
       FROM THING1 TK INNER JOIN THING2 SC ON TK.tkNo=SC.tkNo
              INNER JOIN Component EL on EL.scNo=SC.scNo             
              LEFT OUTER JOIN Field FLD1 on FLD1.cfNo=EL.cfNoPrompt1            
              LEFT OUTER JOIN MyTableTable MTTTBL on MTTTBL.tbNo=FLD1.tbNo

       WHERE EL.PK=@PK
              AND (MTTTBL.tTable is not NULL AND MTTTBL.tTable not in('OneTableIDontWant'))
              AND MTTTBL.tTable not like '%[_]d%' --eliminate any tables that are actually views
              AND EL.cfNo > 0  
              AND TK.Description like @TaskName

RAISERROR('Table',0,1) WITH NOWAIT
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @curTable
WHILE @@FETCH_STATUS = 0
BEGIN
   SET @tmpStatement = 'SELECT TOP 5 * FROM [MYCONN].TEST_MYDB.dbo.' + @curTable + ' where PK=' + Cast(@PK as nvarchar(10))
   EXEC (@tmpStatement)
   RAISERROR(N'Table (outside): %s',0,1,@curTable) WITH NOWAIT  

   IF @@ROWCOUNT = 0 
   BEGIN
        --@tmpInsertVars isn't updating!!!
        RAISERROR(N'Initial Select: %s',0,1,@tmpStatement) WITH NOWAIT
        SELECT @tmpInsertVars = COALESCE(@tmpInsertVars + ',','') + COLUMN_NAME 
        FROM PRODDB.INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = @curTable

        SET @tmpInsertStatement = 'INSERT INTO [MYCONN].TEST_MYDB.dbo.' + @curTable + ' (' + @tmpInsertVars + ')' +
                                  ' SELECT TOP 500 ' + @tmpInsertVars +
                                  ' FROM TEST_MYDB.' + @curTable +
                                  ' WHERE PK=' + Cast(@PK as nvarchar(10))

        RAISERROR(N'Insert Statement: %s',0,1,@tmpInsertStatement) WITH NOWAIT
   END


   FETCH NEXT FROM TableCursor INTO @curTable
END

CLOSE TableCursor
DEALLOCATE TableCursor
4

1 回答 1

1
  1. 声明您的 @tmpInsertStatement 和 @tmpInsertVars 变量。
  2. 在填充之前将 @tmpInsertVars 重置为 NULL

    USE MAINDB
    DECLARE @PK int = 1000,
     @TaskName nvarchar(50) = 'TASK', 
     @curTable nvarchar(75),
     @curRow nvarchar(75),
     @tmpStatement nvarchar(500),
     @tmpInsert nvarchar(500),
     @tmpInsertStatement nvarchar(500),
     @tmpInsertVars nvarchar(500)
    
    RAISERROR('Retrieving Tables',0,1) WITH NOWAIT
     DECLARE TableCursor CURSOR LOCAL FOR 
    
        SELECT DISTINCT TOP 2 PRMPTTBL.tTable as PromptTable
           FROM THING1 TK INNER JOIN THING2 SC ON TK.tkNo=SC.tkNo
                  INNER JOIN Component EL on EL.scNo=SC.scNo             
                  LEFT OUTER JOIN Field FLD1 on FLD1.cfNo=EL.cfNoPrompt1            
                  LEFT OUTER JOIN MyTableTable MTTTBL on MTTTBL.tbNo=FLD1.tbNo
    
           WHERE EL.PK=@PK
                  AND (MTTTBL.tTable is not NULL AND MTTTBL.tTable not in('OneTableIDontWant'))
                  AND MTTTBL.tTable not like '%[_]d%' --eliminate any tables that are actually views
                  AND EL.cfNo > 0  
                  AND TK.Description like @TaskName
    
    RAISERROR('Table',0,1) WITH NOWAIT
    OPEN TableCursor
    FETCH NEXT FROM TableCursor INTO @curTable
    WHILE @@FETCH_STATUS = 0
    BEGIN
       SET @tmpStatement = 'SELECT TOP 5 * FROM [MYCONN].TEST_MYDB.dbo.' + @curTable + ' where PK=' + Cast(@PK as nvarchar(10))
       EXEC (@tmpStatement)
       RAISERROR(N'Table (outside): %s',0,1,@curTable) WITH NOWAIT  
    
       IF @@ROWCOUNT = 0 
       BEGIN
            --@tmpInsertVars isn't updating!!!
            SELECT @tmpInsertVars = NULL; -- RESET @tmpInsertVars
            RAISERROR(N'Initial Select: %s',0,1,@tmpStatement) WITH NOWAIT
            SELECT @tmpInsertVars = COALESCE(@tmpInsertVars + ',','') + COLUMN_NAME 
            FROM PRODDB.INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_NAME = @curTable
    
            SET @tmpInsertStatement = 'INSERT INTO [MYCONN].TEST_MYDB.dbo.' + @curTable + ' (' + @tmpInsertVars + ')' +
                                      ' SELECT TOP 500 ' + @tmpInsertVars +
                                      ' FROM TEST_MYDB.' + @curTable +
                                      ' WHERE PK=' + Cast(@PK as nvarchar(10))
    
            RAISERROR(N'Insert Statement: %s',0,1,@tmpInsertStatement) WITH NOWAIT
       END
    
    
       FETCH NEXT FROM TableCursor INTO @curTable
    END
    
    CLOSE TableCursor
    DEALLOCATE TableCursor
    
于 2013-03-14T22:46:03.740 回答