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

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

Related

Joining Different Database Tables

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.
You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you

SSRS 2008 R2 Data Region Embedded in Another Data Region

I have two unrelated tables (Table A and Table B) that I would like to join to create a unique list of pairings of the two. So, each row in Table A will pair with each row in Table B creating a list of unique pairings between the two tables.
My ideas of what can be done:
I can either do this in the query (SQL) by creating one dataset and having two fields outputted (each row equaling a unique pairing).
Or by creating two different datasets (one for each table) and have a data region embedded within a different data region; each data region pulling from a different dataset (of the two created for each table).
I have tried implementing the second method but it would not allow me to select a different dataset for the embedded data region from the parent data region.
The first method I have not tried but do not understand how or even if it is possible through the SQL language.
Any help or guidance in this matter would be greatly appreciated!
The first is called a cross join:
select t1.*, t2.*
from t1 cross join
t2;
Whether you should do this in the application or in the database is open to question. It depends on the size of the tables and the bandwidth to the database -- there is an overhead to pulling rows from a database.
If each table has 2 rows, this is a non-issue. If each table has 100 rows, then you would be pulling 10,000 rows from the database and it might be faster to pull 2*100 rows and do the looping in the application.

Oracle SQL merge tables without specifying columns

I have a table people with less than 100,000 records and I have taken a backup of this table using the following:
create table people_backup as select * from people
I add some new records to my people table over time, but eventually I want to merge the records from my backup table into people. Unfortunately I cannot simply DROP my table as my new records will be lost!
So I want to update the records in my people table using the records from people_backup, based on their primary key id and I have found 2 ways to do this:
MERGE the tables together
use some sort of fancy correlated update
Great! However, both of these methods use SET and make me specify what columns I want to update. Unfortunately I am lazy and the structure of people may change over time and while my CTAS statement doesn't need to be updated, my update/merge script will need changes, which feels like unnecessary work for me.
Is there a way merge entire rows without having to specify columns? I see here that not specifying columns during an INSERT will direct SQL to insert values by order, can the same methodology be applied here, is this safe?
NB: The structure of the table will not change between backups
Given that your table is small, you could simply
DELETE FROM table t
WHERE EXISTS( SELECT 1
FROM backup b
WHERE t.key = b.key );
INSERT INTO table
SELECT *
FROM backup;
That is slow and not particularly elegant (particularly if most of the data from the backup hasn't changed) but assuming the columns in the two tables match, it does allow you to not list out the columns. Personally, I'd much prefer writing out the column names (presumably those don't change all that often) so that I could do an update.

SQL 2012 Data importing and merging data

I am doing a project on database programming using SQL Server 2012 and also visual studio. I have created some tables and I have a excel file with lots of data. The specification at this stage is to merge two excel sheets of data that has 11 columns each (same columns for both files with different data) into a separate table that is then used later on in the project for paging etc.
My original vision was to create two tables, one table that had one excel tab of data and another table exactly the same except for the name to house the second data set, and then using a union join to merge the two tables into one. However importing straight to the tables is impossible (possibly due to the existence of a composite key in column 1 of the data) so I then created two new tables altogether that now does contain the data from the excel sheets however this doesn't meet the specification of merging the files into the table (as the data is still in two tables not one, and also it has to be in a certain table that was created by DDL earlier). Also it doesn't solve the problem as there doesn't seem to be a way to query those tables into an existing table (or is there?)
Anyway, thanks for reading, hopefully I have included enough information, if it seems I've missed something, feel free to ask. I think the ideal solution for this would involve joins of some description such as union but there doesn't seem to be anyway to then relate that join back to the existing table.
Basically, you will want to do something similar this:
SET IDENTITY_INSERT MainTable ON
INSERT INTO MainTable (col1, col2, col3 ... col11)
SELECT col1, col2, col3 ... col11
FROM Table1
UNION
SELECT col1, col2, col3 ... col11
FROM Table2
SET IDENTITY_INSERT MainTable OFF
You haven't specifically mentioned you have an IDENTITY field for your Primary Keys but I'm assuming you are, hence including the SET IDENTITY_INSERT commands.

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.