Monday, February 13, 2012

copy some fields of table1 to an existing table2

I have two tables (table1 and table2) in a database of sqlserver 2000. I
want to add those fields of table1 who doesn't exist in table2, to table2.
How can I do that?
Thanks,
HugoEduco Gent wrote:
> I have two tables (table1 and table2) in a database of sqlserver 2000. I
> want to add those fields of table1 who doesn't exist in table2, to table2.
You will have to do this by hand unless you purchase a diff tool that is
designed to work against database schema. SQLCompare is a pretty popular
one.
Otherwise you will have to do it by hand.
ALTER TABLE table2
ADD COLUMN [NewColumn] <datatype>
See Books Online for more information.
http://msdn.microsoft.com/library/e..._aa-az_4e5h.asp
Aaron Weiker
http://aaronweiker.com/
http://www.sqlprogrammer.org/|||Hi Educo
Check if following solution helps you.
I took two tables Table1 and Table2 (Formatting need to be done)
create table Table1(a int , b int , c int,d varchar(100),e nvarchar(100),f
text , g money)
create table Table2(a int , b int)
This proc will do the job.
Create Procedure Proc_AddCols
@.Tab1 Varchar(1000),
@.Tab2 Varchar(1000)
As
declare
@.sqlString Varchar(8000),
@.ColString Varchar(8000)
Begin
Set @.ColString = ''
Select @.ColString = @.ColString + 'Alter Table ' + IsNull(@.Tab2,'') + ' Add
' + IsNull(Column_Name,'') + ' ' + Case upper(IsNull(Data_Type,''))
When 'INT' then ' INT'
When 'VARCHAR' then ' VARCHAR(' + cast(IsNull(Character_Maximum_Length,'')
as Varchar) + ')'
When 'NVARCHAR' then ' NVARCHAR(' +
cast(IsNull(Character_Maximum_Length,'')
as Varchar) + ')'
Else ' ' + upper(IsNull(Data_Type,''))
end + ' ' + Case IsNull(IS_Nullable,'') when 'YES' then '' else ' NotNull'
end
From Information_Schema.Columns Where Table_Name Like @.Tab1 And Column_Name
not in
(Select Column_Name from Information_Schema.Columns Where Table_Name like
@.Tab2)
Exec(@.ColString)
End
Go
Exec Proc_AddCols 'Table1','Table2'
Go
Exten this stored procedure for your solution
Tell me if it solves your problem.
Vishal Khajuria
Sungard SCT India
"Educo Gent" wrote:

> I have two tables (table1 and table2) in a database of sqlserver 2000. I
> want to add those fields of table1 who doesn't exist in table2, to table2.
> How can I do that?
> Thanks,
> Hugo
>
>

No comments:

Post a Comment