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
Related
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.
I have no clue how to Import data from one database to another with condition.
I have DB Name (Northwind) and the table Name Employee
I have following columns
ID
Name
I have another DB (Mater) and the table Name Employee.
I have the following columns
Emp.ID
Emp.Name
Now i want transfer all data from Northwind.Employee to Master.Employee table with Condition.
Condition is
IF ID=1 then Emp.ID=201 (this is a constant value no logic behind that)
Any idea or suggestion please
in order to select from diferent DB you can assist this question:
INSERT INTO from two different server database
the id issue is a simple case you can see example here: SQL Case Statement Syntax?
if the databases are on the same server, you can just reference the 2 tables from the different databases with SQL such as the following:
INSERT INTO Master.Employee
SELECT 201 as ID, e2.Name
FROM Northwind.Employee e2
WHERE e2.ID = 1
But if the databases are on different servers, you will have to use either a linked server or SSIS package to achieve this.
If I'm understanding your question correctly, you can use a case statement in your insert:
insert into master.schema.employee (id, name)
select case when id = 1 then 201 else id end, name
from northwind.schema.employee
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
I want to create a table in sql using the columns details (name, data type etc.) stored in anther table in the database.
Depending on the database you can use the information schema tables. They hold the information you are looking for. Look for the table that describes the columns.
Postgres: http://www.postgresql.org/docs/8.4/interactive/information-schema.html
MySQL: http://dev.mysql.com/doc/refman/5.0/en/information-schema.html
You can query these tables and use 'select into' to insert the results into your other table.
One opinion is to create CREATE TABLE query and execute it in ADO.NET like shown here this
Try out this code
CREATE TABLE new_table
AS
SELECT *
FROM old_table
WHERE 1 = 2;
I need a query to create a table which is the exact replica but with different table name and without any data from the source table using a sql query!
You can try this
SELECT * INTO Table_Copy
FROM Table
where 1=2
It will create a empty table with the same structure.
SQL Server Management Studio
Object Explorer
Connect -> Your server
Databases -> Choose Database
Tables
Right Click Your Table
Script Table as -> Create To -> New Query Editor Window
Jonathan has it (upvoted), and you should probably go with that because it's more portable. I normally use something similar:
SELECT TOP 0 * INTO [New_Table] FROM [Old_Table]
I think this better expresses what you're doing, but I like Jonathan's because 'TOP 0' is SQL Server specific, and so his is more portable.
For MySQL, you can call SHOW CREATE TABLE table_name;
It will display a CREATE TABLE query. Simply change the table name in that query and you're good to go.
http://dev.mysql.com/doc/refman/5.1/en/show-create-table.html
If you use Postgresql:
CREATE TABLE LIKE table_name
http://www.postgresql.org/docs/8.1/static/sql-createtable.html
SELECT * INTO Table_Copy
FROM Table
where 1=2
This worked very well, when i tried to create a replica of the table without any data's.
SELECT * INTO Table_Copy
FROM Table
This will create a replica with the data's too.
This can help you:
CREATE TABLE foo AS SELECT...
Read more here
select * into newtablename from sourcetablename
go
truncate newtablename
go
That will result in an exact copy but it also copies the data at first which you remove with the truncate statement.
create table <new table name> as select * from <old tale name from which you would like to extract data>
It will create a new table with a different name but will copy all existing data from the old table to new table.
in postgres you can use INHERITS or LIKE keyword to make replica of a table(only copies structure of the table)
CREATE TABLE client_new (LIKE client);
or
CREATE TABLE client_new () INHERITS (client)
Use of INHERITS creates a persistent relationship between the new child table and its parent table(s). Schema modifications to the parent(s) normally propagate to children as well, and by default the data of the child table is included in scans of the parent(s).
LIKE clause specifies a table from which the new table automatically copies all column names, their data types, and their not-null constraints.Unlike INHERITS, the new table and original table are completely decoupled after creation is complete. Changes to the original table will not be applied to the new table, and it is not possible to include data of the new table in scans of the original table.