Compare Oracle table columns in SQL - 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'

Related

Get a list of all schemas that contain a certain table name

I've looked at these questions:
postgres query to list all table names
Psql list all tables
But neither of them quite answer my question. I'm trying to search an entire PostgreSQL database and list the name of every schema in it that contains a "groups" table.
I'm thinking something like:
SELECT * FROM information_schema.tables WHERE table_name='groups';
But that's still missing how to get the containing schemas.
SELECT DISTINCT table_schema
FROM information_schema.tables
WHERE table_name='yourtablename';

How to check all column of a table in a database to same table exist in other databases in sql server

I have a table in a database and I have same table to other databases.I add some columns of the table of first database.Now I want to check that all off database's table are same or not.If its not then add those columns on those tables.Please give any kind of writing sp or another idea to do that
You can use INFORMATION_SCHEMA.COLUMNS to Check all the column names of all tables present in a database.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
Of course this may not be a complete answer for your question but thought this will help you in some way.

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

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

How to select all fields in SQL joins without getting duplicate columns names?

Suppose I have one table A, with 10 fields. And Table B, with 5 fields.
B links to A via a column named "key", that exists both in A, and in B, with the same name ("key").
I am generating a generic piece of SQL, that queries from a main table A, and receives a table name parameter to join to, and select all A fields + B.
In this case, I will get all the 15 fields I want, or more precisely - 16, because I get "key" twice, once from A and once from B.
What I want is to get only 15 fields (all fields from the main table + the ones existing in the generic table), without getting "key" twice.
Of course I can explicit the fields I want in the SELECT itself, but that thwarts my very objective of building a generic SQL.
It really depends on which RDBMS you're using it against, and how you're assembling your dynamic SQL. For instance, if you're using Oracle and it's a PL/SQL procedure putting together your SQL, you're presumably querying USER_TAB_COLS or something like that. In that case, you could get your final list of columns names like
SELECT DISTINCT(column_name)
FROM user_tab_cols
WHERE table_name IN ('tableA', 'tableB');
but basically, we're going to need to know a lot more about how you're building your dynamic SQL.
Re-thinking about what I asked makes me conclude that this is not plausible. Selecting columns in a SELECT statement picks the columns we are interested in from the list of tables provided. In cases where the same column name exists in more than one of the tables involved, which are the cases my question is addressing, it would, ideally, be nice if the DB engine could return a unique list of fields - BUT - for that it would have to decide itself which column (and from which table) to choose, from all the matches - which is something that the DB cannot do, because it is solely dependent in the user's choice.

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