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.
Related
I need a query to import data of a table into a different table and different database. Then using the new table I need to update another table in same database
As you tagged SSMS, I'm assuming you're using SQL Server. This answer may apply to other databases, but you'd need to check.
How to refer to other databases
Assuming your database is on the same server, and the table is in the 'dbo' schema, then you can refer to the other database/table as follows
SELECT *
FROM [db1].[dbo].[t1]
If it's on a different server (but has been set up as a linked server) you can do something similar
SELECT *
FROM [servername].[db1].[dbo].[t1]
Making a copy of the data
To save it as a new table within your current database, you could either create a copy of the table's structure in the current database and use an INSERT INTO command, or instead let SQL Server make the table for you by using the 'INTO' clause in the SELECT e.g.,
SELECT *
INTO [db2].[dbo].[t2] -- Or just simply [t2] if you're running this from that database and are happy with [dbo]
FROM [db1].[dbo].[t1]
The above copies the data from the t1 table in db1 to a new table t2 in db2.
You can then use the data in t2 as you normally would use a table.
If you're running this from within d1, you will always need to refer to the tables in d2 properly (and/or alias them) as below.
UPDATE t3
SET xfield = t2.zfield
FROM [db2].[dbo].[t3] AS t3
INNER JOIN [db2].[dbo].[t2] AS t2 ON t3.id = t2.id
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 the following SQL (SSMS) statement that returns invalid records for endstrands not found in the fiberstrands tabl:
SELECT * FROM FIBERSPLICE fs
WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)
Every record returned here needs to be rectified and placed back into the fiberstrand table based off a backup of the fiberstrand table.
Now, I have a backup table, FiberStrandHAS, that has all of the fiberstrand records that are missing (plus more) as indicated by the above statement. My goal is to insert the specified records from the above statement using the FiberStranHAS backup table into the fiberstrand table. Any ideas on how I could accomplish this task?
Figured out how to get what I needed...
select *
from fiberstrandhas fsh
inner join (SELECT * FROM FIBERSPLICE fs WHERE ENDSTRAND NOT IN (SELECT ID FROM FIBERSTRAND ft)) es
on (fsh.id = es.endstrand)
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
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?