Alright, since my previous post , I was able to make progress!! However, I've run into another problem...once again:
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. In other words, do programmatically what 'EDIT TOP 200' does...but a lot faster.
PROBLEM (UPDATED): @tmpInserVars isn't updating with each iteration. It gets set the first time the code is entered, and never refreshes.
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.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
Reset @tmpInsertVars to NULL just before you populate it
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.