简体   繁体   English

T-SQL动态地从测试转移到生产

[英]T-SQL Move From Test To Production Dynamically

GOAL: Pass in two parameters (a task and a primary key) to generate a list of tables. 目标:传入两个参数(任务和主键)以生成表列表。 Take the list, and then dynamically construct insert statements with the aim to copy data from a production environment to a test environment. 获取列表,然后动态构造insert语句,目的是将数据从生产环境复制到测试环境。 In other words, do programmatically what 'EDIT TOP 200' does...but a lot faster. 换句话说,以编程方式执行'EDIT TOP 200'的功能......但速度要快得多。

PROBLEM: The query spins and runs indefinitely. 问题:查询无限期地旋转和运行。 There should only be about 20-30 tables that the query will need to construct insert statements for...so I let it go for about 2 minutes before concluding that I probably have an infinite loop somewhere. 应该只有大约20-30个表,查询将需要构建插入语句...所以我让它走了大约2分钟,然后得出结论我可能在某个地方有一个无限循环。 Note that I'm not even inserting anything into the test database at this point. 请注意,此时我甚至没有在测试数据库中插入任何内容。

At the moment I'm just trying to display the VALUES portion of the insert statements using the RAISERROR call. 目前我只是尝试使用RAISERROR调用显示插入语句的VALUES部分。 While the endgame isn't implemented, I'm hoping someone can help me figure out the problem. 虽然最终没有实施,但我希望有人可以帮我解决问题。

Thus far: 迄今:

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.CustNo=@Custno
              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)

   IF @@ROWCOUNT = 0 
   BEGIN
        DECLARE RowCursor CURSOR LOCAL FOR
        SELECT COLUMN_NAME
        FROM REALDB.INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = @curTable

        RAISERROR('Row',0,1) WITH NOWAIT
        OPEN RowCursor
        FETCH NEXT FROM RowCursor INTO @curRow
        WHILE @@FETCH_STATUS = 0
        BEGIN
          SET @tmpInsert = @tmpInsert + ',' + @curRow
        END

        IF RIGHT(@tmpInsert,1) = ',' SET @tmpInsert = LEFT(@tmpInsert,LEN(@tmpInsert) -1)
        RAISERROR(@tmpInsert,0,1) WITH NOWAIT

        CLOSE RowCursor
        DEALLOCATE RowCursor
        SET @tmpInsert = ''
        FETCH NEXT FROM RowCursor INTO @curRow
   END


   FETCH NEXT FROM TableCursor INTO @curTable
END

CLOSE TableCursor
DEALLOCATE TableCursor
    WHILE @@FETCH_STATUS = 0
    BEGIN
      SET @tmpInsert = @tmpInsert + ',' + @curRow
    END

is an infinite loop, because you don't FETCH NEXT inside it. 是一个无限循环,因为你内部没有FETCH NEXT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM