Import data from one DB to another Database with condition - sql

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

Related

Want to bring data from Oracle warehouse to SQL Server

I want to bring data from Oracle Datawarehouse to SQL Server based on condition.
I have 3 segment columns(seg_1,seg_2,seg_3) in table_A in Datawarehouse, want bring them into SQL but I want make them as one column based on column present in SQL server table_B. I will run stored procedure to do it.
SELECT ID,
CASE WHEN seg_1 in (select seg from [Servername].[dbname].[schemaname].[table_B]) then seg_1
WHEN seg_2 in (select seg from [Servername].[dbname].[schemaname].[table_B]) then seg_2
WHEN seg_3 in (select seg from [Servername].[dbname].[schemaname].[table_B]) then seg_3
End
from table_A
When I'm trying run above code in Stored Procedure it is giving error as table_B is invalid table name. The table is present in that server and database. Please help me to do it.

SQL Command to copy data from 1 column in table and 1 column in another table into a new table?

I had to make a new table to get the Include statement working in Entity Framework since EF was looking for a table called be_PostTagbe_Posts. I was using EF Code First from DB. But now the question is about SQL. I added one row of data and now the include works. But what I am looking for is a SQL command that can copy data from 1 column in 1 table and 1 column in another into the new be_PostTagbe_Posts table. In the be_Posts table I need the data in PostRowID to go into be_Posts_PostRowID and PostTagId to go into be_PostTag_PostTagID. Both be_PostTag_PostTagID and be_Posts_PostRowID are in the new be_PostTagbe_Posts table. I am not very good with SQL so not sure how to do this.
Edit: Thanks for the answers. I tried 2 separate queries but only data was inserted into the the be_PostTag_PostTagID while be_PostTag_PostRowID remained null.
And I tried this query which returned The multi-part identifier "be_PostTag.PostID" could not be bound.
INSERT INTO be_PostTagbe_Posts(be_PostTag_PostTagID, be_Posts_PostRowID)
SELECT be_PostTag.PostTagID, be_Posts.PostRowID
WHERE be_PostTag.PostID = be_Posts.PostID
EDIT:
This only inserted half the data - even 2 inserts leave one column null
INSERT INTO be_PostTagbe_Posts (be_Posts_PostRowID)
SELECT PostRowID FROM be_Posts;
INSERT INTO be_PostTagbe_Posts (be_PostTag_PostTagID)
SELECT PostTagID FROM be_PostTag;
And yet management studio tells me the query executed successfully but one column is still null. Weird.
Here are screenshots of the tables:
SELECT PostTagID AS be_PostTag_PostTagID, PostRowID AS be_Posts_PostRowID
INTO be_PostTagbe_Posts
FROM be_PostTag
Inner JOIN be_Posts
ON be_PostTag.PostID=be_Posts.PostID
That command created the new table with the 2 columns populated.
If i understand you ,you want to Copy Table Z's Column A to Table X And Table Z's Column B to Table Y.
If it is so, According to your question it is not clear about Table Structure of TableX and TableY
Assuming TableX And TableY to single ColumnTable [Apart from IdentityColumn] our query will be
INSERT INTO TableX
SELECT ColumnA FROM TableZ
INSERT INTO TableY
SELECT ColumnB FROM TableZ
Rest put your Entire Structure of Table To Get More Help Because These query are on Assumptions
There's not enough information in your question to give you a working example, but this would be the general syntax for INSERTing into a different table using a query SELECTing from two other tables.
INSERT INTO destination_table(wanted_value_1, wanted_value_2)
SELECT table_1.source_field_1, table_2.source_field_1
WHERE table_1.matching_field = table_2.matching_field
There has to be some sort of relationship between the two tables for the WHERE clause to work in that statement. I'm guessing based the little information you provided that there is a PostRowID field somewhere in the table that contains the tags such that your data would look similar to this in the tag table:
PostRowID PostTagID
--------- ---------
1 1
1 2
1 3
1 4
2 1
2 2
3 3
4 4
It sounds like you should use two sql statements:
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`)
select `PostTagID` from POSTTAGIDTABLE
and
Insert into `be_PostTagbe_Posts` (`be_Posts_PostRowID`)
select `PostRowID` from POSTTAGIDTABLE
unless the items have some sort of relationship, then if you have a select statement that will select the merged data in two columns you can just do
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`,`be_Posts_PostRowID`)
(select statement that selects the two items)

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

Validate Data in SQL Server Table

I am trying to validate the data present in SQL Server table using a stored procedure.
In one of the validation rules, i have to check whether the value of a particular column is present in another table.
Suppose i have a staging table with following columns Cat_ID, Amount, SRC_CDE
I have a 'maintable' with following columns CatID , Cat_Name
I have to validate whether the Cat_ID present in staging table exists in the 'maintable' for each row
I am using the following statement to validate
if((Select count(*) from maintable where CatID= #Cat_id) >0 )
-- Do something if data present
I want to know if there is any better way of doing the above thing other than using a select query for every row.
Can i use some sort of an array where i can fetch all the CatID from maintable and the check instead of using a select query.
Thanks
Using a left join to list all the invalid rows.
select
staging.*
from
staging
left join maintable
on staging.catid=maintable.catid
where maintable.catid is null

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