SQL 2012 Data importing and merging data - sql

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.

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

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

SQL question regarding the insertion of empty tuples to prepare for update statements

I am making a table that will be borrowing a couple of columns from another table, but not all of them. Right now my new table doesn't have anything in it. I want to add X number of empty tuples to my table so that I can then begin adding data from my old table to my new table via update statements.
Is that the best way to do it? What is the syntax for adding empty rows? Thanks
Instead of inserting nulls and then updating them, cant you just insert data from the other table directly. using something like this -
INSERT INTO Table1 (col1, col2, col3)
SELECT column1, column2, column3
FROM Table2
WHERE <some condition>
If you still want to insert empty records, then you will have to make sure that all your columns allow nulls. Then you can use something like this -
Table1
PrimaryKey_Col | col1 | col2 | col3
Insert INTO Table1 (PrimaryKey_col) values (<some value>)
This will make sure your a new row is inserted with a primary key and the rest of the columns are nulls. And these records can be updated later.
No, this is not even a good way, let alone the best way.
If you look at it conceptually adding empty rows serves no purpose.
In databases each row of a table corresponds to a true statement (fact). Adding a row with all NULLs (even if possible) records nothing and represents inconsistent data on its own. Especially having multiple empty records.
Also, if you are even able to add a row with all NULLs to a table that's an indication that you have no data integrity rules for the row so and that's mostly what databases are about - integrity rules and quality of data. A well designed database should not accept contradictory or meaningless data (and empty row is meaningless)
As Pavanred answered inserting real data is a single command, so there is no benefit in making it two or more commands.

Access - Merge two databases with identical structure

I would like to write a query that merges two Access 2000 databases into one. Each has 35 tables with identical fields and mostly unique data. There are some rows which will have the same "primary key" in which case the row from database A should always take precedence over database B. I use quotes around "primary key" because the databases are generated without any keys or relationships. For example:
Database A, table1
col1 col2
Frank red
Debbie blue
Database B, table1
col1 col2
Harry orange
Debbie pink
And the results I would like:
col1 col2
Frank red
Harry orange
Debbie blue
These databases are generated and downloaded by non-sql-savvy users, so I would like to just give them a query to copy and paste. They will obviously have to start by importing or linking one DB [in]to another.
I'm guessing I will have to make a third table with the combined results query and then delete the other two. Ideally, though, it would just add database A's rows to database B's (overriding where necessary).
I'm of course not looking for a complete answer, just hoping for some advice on where to start. I have some mySQL experience and understand the basics of joins. Is it possible to do this all in one query, or will I have to have a separate one for each table?
THANKS!!
How about:
SELECT t.ID, t.Field1, t.Field2 INTO NewTable FROM
(SELECT a.ID, a.Field1, a.Field2
FROM Table1 A
UNION
SELECT x.ID, x.Field1, x.Field2
FROM Table1 x IN 'C:\docs\db2.mdb'
WHERE x.ID NOT IN (SELECT ID From Table1)) t
This isn't an SQL solution, but may work just as well as telling your non-sql savvy users to cut and paste SQL statements.
I suggest defining a unique Key on the table that takes precedence (col1).
Then copy all the data from Database B into the 'master' table.
This will fail for all duplicates, but insert any 'new' records.
Remove the unique key constraint after you're done if necessary.
From your question it looks like you need the resulting table in database B. So you may want to have your users copy the table into database B before you start or after you're done.