How to compare table columns and match them in SQL Sever 2012 - sql

This is a bit of a HW assignment at work.
What I'm doing at work right now is database archiving. I take a source DB and move all (or specific portion) of data into a new archive DB using a stored procedure.
Problem is, not all of the columns in the source DB match the destination DB.
How can I compare the tables in the DBs for missing columns and then match them? So if source DB has Table 1 that has 4 columns and destination DB has Table 1 that only has 2 columns, how can I compare both Table 1's and then have it add/delete columns to match each other?
SQL Server 2012/SQL Management Studio

You can compare the table Columns by querying Information_Schema.Columns or sys.Columns.
The below query will provide number of missing columns
SELECT c2.table_name,c2.COLUMN_NAME
FROM archdb.[INFORMATION_SCHEMA].[COLUMNS] c2
WHERE table_name='archtable' and c2.COLUMN_NAME
not in (SELECT column_name
FROM orgdb.[INFORMATION_SCHEMA].[COLUMNS]
WHERE table_name='orgtable')
This link will provide information about number of ways and tools available to compare sql server tables

Related

How to check if all data entries in a table is in an SQL database?

I have table full of data that I want to match up with a table from Oracle SQL Database. The table I have does not have all of the columns in the Database and has a smaller set of data. I just want a fairly simple way to check if the data can be matched up with a row in the Database.

How to determine if a column exists in SQL server DB in other tables with a different name

I need to fetch specific hardware data from source tables. The hardware information is present in a table Server_Data with columns as follows,
Server_ID, Server Property, Property_Value
65 Model Cisco 123
65 Name Cisco abc
I need to link this table with System table that has columns as follows,
System_ID, System_IP
1 10.20.30.40
I searched all tables in database but Server_ID column is present only in Server _Data table. Also, I searched all tables if there exists a table that links System_ID with Server_ID, but there is no such table.
I need to find if the Server_ID column is present in any other table with some other name (say Server_Key or just Key). Any help would be appreciated.
Only using SQL, there will not be a way to find an identical column in another table, especially if it has a different name. I think you would need to manually compare every column in every other table to that column to find a match.
If I were you I would start by running the following SP:
EXEC sp_fkeys 'Server_Data'
That will tell you if the Server_ID column is referencing any other table in the DB or not.

Exporting from SQL Server 2008

I have a SQL Server database from a POS System. I need to export data to a new POS system. All I need is Products, Prices and Barcodes.
My problem is the barcodes are stored in a different table. I need to export multiple tables and merge them together if this is possible. I have no problem exporting each table and then importing but I am missing the barcodes as they are in a different table.
Can this be done with query builder or scripting?
You can write cross database queries by specifying 3 part names.
If you have a table in DB1 (dbo.Barcodes) as source and a table with the same structure in DB2 (dbo_NewBarcodes), the following query skeleton could be used:
INSERT INTO DB2.dbo.NewBarcodes (
col1, col2, col3
)
SELECT col1, col2, col3 FROM DB1.dbo.Barcodes
If the structure of the two tables are different, construct your select query to transform the columns from the source table to match with the columns in the destination table.
Please note, that the order and count of the columns are important.
EDIT
If the source and destination databases are on different servers, you can either build the database on the source server, than create a backup and restore it on the destination server, or you can use cross server queries (see OPENROWSET, OPENQUERY and Linked Servers)
If there are some data in the destination table and there could be conflicts with the data in the source table, please check the MERGE INTO statement.

Comparing the data of two tables in the same database in sqlserver

I need to compare the two table data with in one database.match the data using some columns form table.
Stored this extra rows data into another table called "relationaldata".
while I am searched ,found some solutin.
But it's not working to me
http://weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx
can any one help how to do this.
How compare two table data with in one database using redgate(Tool)?
Red Gate SQL Data Compare lets you map together two tables in the same database, provided the columns are compatible datatypes. You just put the same database in the source and target, then go to the Object Mapping tab, unmap the two tables, and map them together.
Data Compare used to use UNION ALL, but it was filling up tempdb, which is what will happen if the table has a high row count. It does all the "joins" on local hard disk now using a data cache.
I think you can use Except clause in sql server
INSERT INTO tableC
(
Col1
, col2
, col3
)
select Col1,col2,col3from tableA
Except
select Col1,col2,col3 from tableB
Please refer for more information
http://blog.sqlauthority.com/2008/08/07/sql-server-except-clause-in-sql-server-is-similar-to-minus-clause-in-oracle/
Hope this helps

Compare Oracle table columns in SQL

Is it possible through SQL in oracle to compare two tables and list the columns that exist in one but not the other. I have two tables, one (table A) that receives the data from an authoritative source with a specific code and the second is the rest of the data from that import without that specific code (Table B). I was hoping there would be a fast way in SQL to compare the two tables and tell me what columns exist specifically in Table A and not in Table B? Thanks.
Use:
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='A' AND OWNER='YourSchema'
minus
SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME='B' AND OWNER='YourSchema'