How to insert mutiple select statements into table uisng SSIS - sql

For example I have got 2 tables Employee and Department.
With the queries like
Select EmpID,Employee From dbo.Employee
Select DepID,DeptName From dbo.Department
which I have put 2 EXECUTE SQL tasks.
and want to insert those select statements into some Destination table.
Output :
ID SQL_Stmt
1 Select EmpID,Employee From dbo.Employee
2 Select DepID,DeptName From dbo.Department
where as my structure looks like this :

It's kinda tough to say what you're doing here, but judging by the SELECT statements and the destination table layout it seems to me that it would be simpler to merge the two SELECT statements into one SELECT statement that joins on an ID column which appears to be in the destination table, so I'll assume ID is in the source tables too.
IMPORTANT: If there isn't a relationship (such as ID) between [dbo].[Employee] and [dbo].[Department] then they SHOULD NOT be inserted into the same destination table. Your table will not be normalized, which is bad.
Ideally, your source query would be similar to
SELECT Emp.EmpID
,Emp.Employee
,Dept.DeptName
,Dept.Department
FROM [dbo].[Employee] Emp
INNER JOIN [dbo].[Department] Dept
ON Emp.ID=Dept.ID
Here ends the SQL and begins the SSIS:
You'll need to create a Data Flow task in SSIS.
Within the Data Flow task you'll need an OLE DB Source and OLE DB Destination tasks.
Open the OLE DB Source task and select the "Data Access Mode" dropdown menu and change it to the "SQL Command" option.
Now you can write your SQL Query as the source you'll be inserting into your destination table.
Connect the OLE DB Source and OLE DB Destination and select the destination table from the "Name of the table or view:" dropdown menu.
Columns with the same name should be automatically mapped together, but you may need to manually map columns which are not alike.
You should be able to resolve any other errors that arise on your own. Good luck!
TL;DR: You're trying to achieve something on the Control Flow level of SSIS which should actually be done on the Data Flow level.

Related

How do you copy Sql table from one database to another database with differ field names

I have a database name "EmpOld" with a table name "Employee" and a database name "EmpNew" with a table name "Employee".
The table structures are identical on both database tables except for the names in the table.
Here is a definition of the tables:
Database "EmpOld" table name "Employee" has following field names:
int e_id
char(20) e_fname
char(25) e_lname
Database "EmpNew" table "Employee" has following field names:
int id
char(20) fname
char(25) lname
Notice, the only difference in the tables is the "e_" prefix for field names are removed from the EmpNew Employee table.
How do I move data from EmpOld database to EmpNew database?
Is there a code that maps these field respectively.
Thanks community,
Nick
Well, you could just name them manually:
INSERT dbo.EmpNew(fname, lname) SELECT e_fname, e_lname FROM dbo.EmpOld;
If you want to do this without manually typing out the column names, there is magic in SSMS - just drag the Columns folder from each table into the appropriate spot in your query window (and then manually remove identity columns, timestamp, computed columns etc. if relevant).
There is no automatic way of mapping fields, unless with some code.
This can be done in two ways:
Using the SQL Import & Export Wizard
This is the most easy way to do this and here is an article that gives step by step to do this. The key is to change the mapping between the source and destination fields while importing the data.
Writing an SQL
This method requires both the databases to be accessible. Using a simple insert statement as follows this can be achieved
insert into EmpNew.dbo.Employee(id, fname, lname)
select
e_id, e_fname, e_lname
from
EmpOld.dbo.Employee
If they are on same sql server then the above will work good as is. If they are different sql server you may have to add a link server connection and prefix the table commands with that.
Is there a code that maps these field respectively.
No - you'll need to provide the mapping. If you're using an ETL tool like SSIS there may be a way to programatically map columns based on some criteria, but nothing built into SQL.
Maybe you can generate code with help from the tables sys.columns and other system tables so that you can make the copy-process run automatically.
I think you can't use:
insert into (...) (...)
because you have two databases. So just generate insert statements like:
insert into table (...) VALUES (...)
Please correct me if i misunderstood the question.
There are 2 ways you can do without the data loss.
1) you can use Insert statement
`
Insert into EmpNew (ID,fname,lname)
Select e_id, e_fname, e_lastname
from EmpOld
`
2) You can simple use Import-Export Wizard
Go to Start Menu > SQL Server 2008/2008R2/2012 > ImportandExport>
This will take you the wizard box
Select Source :- DataSource(ServerName) and Database where you are
extracting data from
Select Destination : DataSource(ServerName) and Database where you are extracting data to
Map the table
BE AWARE of PK/FK/Identity
you are good to go

How to copy data and structure of table from one table to another?

In my first database named 'sshopping' has two tables IndiaStates and IndiaCity. I want to copy these both table in new database 'jaijinendera' .
IndiaStates has columns StateID (Primary Key) and StateName
IndiaCity has columns CityID(Primary Key) , StateID (Foreign Key),CityName
I used the query something like this
Insert int jaijinendera..IndiaStates select * from sshopping..IndiaStates
Insert int jaijinendera..IndiaCity select * from sshopping..IndiaCity
this has copied the data but not the keys (structure). What query should i made to copy proper structure and data from sshopping to jaijinendera
Try using the SQL Server Generate script.
Right click on DB where you want to export table structure and data. Select Task and generate script.
follow the wizard. Select the object or entire database in the select object you want to generate script. Click on the Advance to select schema only (for structure)/ data only for data and schema and data for both.
generate the script to file or clipboard.

SSIS Update a particular column

I have a table in SQL Server with many fields. Two of which are IDNumber and Resolved(Date) Resolved is Null in SQL Server. Then I have data with IDNumber and and Resolved(Date) that I want to import and update the table. I want it to find the matching IDNumber and put in the updated resolved date. How can I do this?
Thanks!
When doing updates SSIS will update a row at a time, which can be quite costly. I would advise to use an Execute SQL task to create a staging table:
IF (OBJECT_ID(N'dbo.tmpIDNumberStaging') IS NOT NULL
DROP TABLE dbo.tmpIDNumberStaging;
CREATE TABLE dbo.tmpIDNumberStaging
( IDNumber INT NOT NULL,
Resoved DATE NOT NULL
);
Then use a Data Flow task to import your data to this table (not sure what your source is, but your destination would be OLE DB Destination. You may need to set "Delay Validation" on the Data Flow task to false and/or "Validate External Meta Data" to false on the destination because the destination is created at run time).
Finally use this staging table to update your main table (and drop the staging table to clean up)
UPDATE YourMainTable
SET Resolved = st.Resolved
FROM YourMainTable t
INNER JOIN dbo.tmpIDNumberStaging st
ON st.IDNumber = t.IDNumber
WHERE t.Resolved IS NULL;
-- CLEAN UP AND DROP STAGING TABLE
IF (OBJECT_ID(N'dbo.tmpIDNumberStaging') IS NOT NULL
DROP TABLE dbo.tmpIDNumberStaging;

Stored procedure import data from view

I'd like to do a sync job - from a view to different tables.
I'm having a DB view (from a linked server) and I'd like to make a stored procedure which splits the db view into 2 tables.
The db view looks like:
person_id
person_type (=> either 'employee' or 'customer')
lastname
firstname
email
The tables are 'employees' and 'customers'. Both of them have at least the attributes 'lastname', 'firstname' and 'email'.
If the data row already exists in the tables there will be an update otherwise an insert. The attribute 'person_type' is to decide witch table I have to use.
I use ms sql server 2008 r2.
Can anybody help me creating the stored procedure? Or give me a hint?
Thanks in advance
Create 2 merge statements, one to perform each task.
One MERGE statement will insert/update/delete into the Employee table and the other will do the same to the Customer Table.
Example:
MERGE YourCustomerTable TargetTable
USING (SELECT * FROM YourView WHERE Column = 'Customer') SourceTable ON TargetTable.CustomerID = SourceTable.CustomerID
WHEN MATCHED THEN UPDATE...........
WHEN NOT MATCHED THEN INSERT (ColumnA, ColumnB) Values (SourceTable.ColumnA, SourceTable.ColumnB)
From your description I understand that you need to create two tables for Customer and employee from DB view.
For making this fast; create a ssis package and use "Conditional Split" option to split the table based on the person_type column. and insert it ot corresponding table.
You can save this package and run it whenever u need or u can make it as a job

Using SQL Server DTS Package to Conditionally Insert / Update Rows in Destination Table

I want to create a DTS Package to pull data from an Oracle table into a SQL2K
table. How can I insert rows that are not already in the SQL2K table and
update rows that already exist in the SQL2K table?
I guess I could truncate and repopulate the entire table or create a
temporary table and then do updates/inserts from the temp table into the
destination table.
Is there any easier way using DTS?
Thanks,
Rokal
You can do that in a DTS package using two data driven query tasks: one for the inserts and one for the updates. The data driven query tasks are a bit of a pain to use, but they work. I've also done this (a "merge") in sql server 2000 with an AS/400 database using a dynamic t-sql. You'd write a t-sql script that outputs psql and runs it againt a linked server to the Oracle database.
UPDATE:
A DTS "data driven query task" will let you insert|update data from the sql server connection in DTS to an oracle server connection in DTS w/o a temp table or a linked server.
Update2; here's some more info on what I mean:
http://www.databasejournal.com/features/mssql/article.php/3315951
http://msdn.microsoft.com/en-us/library/aa933507(SQL.80).aspx
Are you keeping the same primary key values?
If you are you have a number of options, some versions of SQL support the MERGE statement which will update or insert just like you require.
Or you can write your own.
Something along the lines of loading all the rows into a staging table in your SQL database and row by row checking for the existence of your primary key in your main SQL table. If the key exists update the row and if not insert it.
Yes, the primary key values in the source and destination will match.
I was hoping to accomplish this task without the use of a temporary (staging) table.
Also, I am using sql server 2000 so the MERGE statement is not available.
Try:
DELETE FROM dbo.WhateverTable WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
It might be pretty slow, use join instead:
Delete a
from firstTable a join secondTable b on a.id = b.id
There's no way with TSQL to do a INSERT or UPDATE in the same statement, but you could very easily do it in two statements (as you mentioned above).
Statement 1:
DELETE FROM dbo.WhateverTable
WHERE WhateverTableID IN (SELECT WhateverTableID FROM MySource)
Statement 2:
INSERT INTO dbo.WhateverTable
SELECT * FROM MySource
Also, is there any reason why you don't want to use a temp table?