![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have the following code which will give me all the tables, but I will need to dynamically generate the select list and explicitly name the fields - here is the code I received in another post. I would be grateful if somebody could give me the information of how I could create the list of fields in the source table (OldDB) to dynamically insert into the destination table (NewDB) - I know that there may be fields in the NewDB that are not in the OldDB, but not vice versa. And I know defaults will handle any fields that do not make it into the select list. How can this be done? By the way I have to have this scripted onto the production machines that I do not have direct access to. declare @sql varchar(8000) declare @table_name varchar(256) SELECT name FROM sysobjects where xtype = 'u' and name 'dtproperties' DECLARE table_list CURSOR FOR SELECT name FROM sysobjects where xtype = 'u' and name 'dtproperties' OPEN table_list FETCH NEXT FROM table_list INTO @table_name WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = 'Insert into NewDB..' + @table_name + ' ' + '(Select * From OldDatabase..' + @table_name + ' ) ' --print @sql --EXEC (@SQL) FETCH NEXT FROM table_list INTO @table_name END DEALLOCATE table_list |
#3
| |||
| |||
|
|
A couple of questions and a few comments. 1. How big is your database? Would a restore/robocopy/litespeed or even DTS solution not be better seen as code is inherently going to invite error. 2. Why do you have to dynamically instantiate the field names. Are the table structures not the same in oldDB? 3. You could try a nested loop using your table name variable and iterating through the following, you will still have to determine the size, collations and defaults for the table. Its messy but with a bit of work you could do it. create table tblMaster_Data_Types( intData_Type_Id int, vcData_Type_Desc varchar(50), chActive char(1)) insert into tblMaster_Data_Types values(1, 'TINYINT', 'Y') insert into tblMaster_Data_Types values(2, 'FLOAT', 'Y') insert into tblMaster_Data_Types values(3, 'SMALLINT', 'Y') insert into tblMaster_Data_Types values(4, 'INT', 'Y') insert into tblMaster_Data_Types values(5, 'BIGINT', 'Y') insert into tblMaster_Data_Types values(6, 'DECIMAL', 'Y') Select SYS_OBJ.NAME,SYS_COL.NAME--, DATA_TYP.vcData_Type_Desc, SYS_USR.NAME From OldDB..sysobjects SYS_OBJ, OldDB..sysUsers SYS_USR, OldDB..SYSCOLUMNS SYS_COL, OldDB..SYSTYPES SYS_TYP--, OldDB..tblMaster_Data_Types DATA_TYP Where SYS_OBJ.type = 'U' AND SYS_OBJ.UID = SYS_USR.UID AND SYS_OBJ.ID = SYS_COL.ID AND SYS_TYP.TYPE = SYS_COL.TYPE AND DATA_TYP.vcData_Type_Desc Collate QL_LATIN1_GENERAL_CP1_CI_AS = SYS_TYP.NAME collate SQL_LATIN1_GENERAL_CP1_CI_AS AND DATA_TYP.chActive = 'Y' ORDER BY SYS_OBJ.NAME 4. Get access to the relevant database. "Derek Hart" wrote: I have the following code which will give me all the tables, but I will need to dynamically generate the select list and explicitly name the fields - here is the code I received in another post. I would be grateful if somebody could give me the information of how I could create the list of fields in the source table (OldDB) to dynamically insert into the destination table (NewDB) - I know that there may be fields in the NewDB that are not in the OldDB, but not vice versa. And I know defaults will handle any fields that do not make it into the select list. How can this be done? By the way I have to have this scripted onto the production machines that I do not have direct access to. declare @sql varchar(8000) declare @table_name varchar(256) SELECT name FROM sysobjects where xtype = 'u' and name 'dtproperties' DECLARE table_list CURSOR FOR SELECT name FROM sysobjects where xtype = 'u' and name 'dtproperties' OPEN table_list FETCH NEXT FROM table_list INTO @table_name WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = 'Insert into NewDB..' + @table_name + ' ' + '(Select * From OldDatabase..' + @table_name + ' ) ' --print @sql --EXEC (@SQL) FETCH NEXT FROM table_list INTO @table_name END DEALLOCATE table_list |
#4
| |||
| |||
|
#5
| |||
| |||
|
#6
| |||
| |||
|
|
I am going to build these sql statements dynamically using VB.NET. Is there a way to read a system table to determine if a specific table has an identity column? I want to run SET IDENTITY_INSERT myTable ON but this statement errors if the table does not have an Identity field. |
#7
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |