Urgent: Get the missing column name

Sandiya
Sandiya used Ask the Experts™
on
Hi,

I've a table namesly "course" in database1 "db1" & another table "course" in database2 "db2". I want to compare two tables & check whether two table columns are equal. If not equal, I have to find out the extra column or missing column and datatype and add the extra/missing column at runtime using alter table command.

How to get data type & column name...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Are the fields unique in there respective columns?
Try this:

Edit
Sounds like you need a full outer join, in a function that returns a table wherethe full outer join finds nulls.  Maybe one for each table.  Then that could be the input for a transaction, or another stored procedure to uptadte the tables as needed.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Kamal KhaleefaInformation Security Specialist

Commented:
hi

i recommend the following

dim dt1 as new datattable=get data from course in db1
dim dt2 as new datattable=get data from course in db2

then write a for loop to compare the columns name
ex: for i as integer=0 to dt1. Columns.Count -1 ...
and then u can know what is the missing
and base on that create a sql statement to do the necessary modification and execute it

Author

Commented:
The problem is that i cannot do a sql query because only the target datatable is from sql and the source datatable is from salesforce.Iam doing an import of data from salesforce and export it to sqlserver.

I have done coding to import the data from salesforce and export it to sqlserver.But say if they have added a custom field in the salesforce  and so i have to a comparison between datatables and find the missing column and then add that column to the sql server table.

i need a logic to compare two datatables for column names and then check whether there are any columns missing and then add that column to the target table in sql server.
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try following

dt1 = salesforce
dt2 = sql server

Dim dtype as String
For i As Integer = 0 to dt1.Columns.Count - 1
   If dt2.Columns.Contains(dt1.Columns(i).ColumnName) = False Then
     dtype = dt1.Columns(i).DataType.ToString()
     'Code here to convert dtype to a sql type and then execute an alter table command
   End If
Next

Author

Commented:
Thanks Codecruiser the code worked like a charm.I am able to identify the missing column and will proceed to do the coding for alter table.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial