T-SQL动态地从测试转移到生产(第2部分)

时间:2013-03-14 21:40:22

标签: sql sql-server tsql

好吧,自从我之前post以来,我就取得了进步!但是,我又遇到了另一个问题:

目标:传入两个参数(任务和主键)以生成表列表。获取列表,然后动态构造insert语句,目的是将数据从生产环境复制到测试环境。换句话说,以编程方式执行'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

1 个答案:

答案 0 :(得分: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