Pass Column value to parameter in Dataflow task SSIS - sql-server

I have a SQL table TABLE1 which has columns ID and LastModifiedDate.Now I have one oracle query SELECT * From Table where NEWID =? where parameter will be the value from ID column from TABLE1 and I need to insert these records into a destination table on a SQL Server.Please advise the best approach for this.I am using SQL 2008.

Where #Id will be parameter
INSERT INTO DESTINATIONTABLE
SELECT *
FROM TABLE
WHERE NEWID IN(SELECT ID From Table1 WHERE ID=#ID)
or if you want to specify the columns you could use
INSERT INTO DESTINATIONTABLE(Col1,Col2)
SELECT Col1,Col2
FROM TABLE
WHERE NEWID IN(SELECT ID From Table1 WHERE ID=#ID)

Related

Create new tables from old tables [duplicate]

This question already has answers here:
How to create table using select query in SQL Server?
(4 answers)
Closed 9 years ago.
I want to create a table from select query result in SQL Server, I tried
create table temp AS select.....
but I got an error
Incorrect syntax near the keyword 'AS'
Use following syntax to create new table from old table in SQL server 2008
Select * into new_table from old_table
use SELECT...INTO
The SELECT INTO statement creates a new table and populates it with
the result set of the SELECT statement. SELECT INTO can be used to
combine data from several tables or views into one table. It can also
be used to create a new table that contains data selected from a
linked server.
Example,
SELECT col1, col2 INTO #a -- <<== creates temporary table
FROM tablename
Inserting Rows by Using SELECT INTO
Standard Syntax,
SELECT col1, ....., col# -- <<== select as many columns as you want
INTO [New tableName]
FROM [Source Table Name]
Please be careful,
MSSQL: "SELECT * INTO NewTable FROM OldTable"
is not always the same as
MYSQL: "create table temp AS select.."
I think that there are occasions when this (in MSSQL)
does not guarantee that all the fields in the new table are of the same type as the old.
For example :
create table oldTable (field1 varchar(10), field2 integer, field3 float)
insert into oldTable (field1,field2,field3) values ('1', 1, 1)
select top 1 * into newTable from oldTable
does not always yield:
create table newTable (field1 varchar(10), field2 integer, field3 float)
but may be:
create table newTable (field1 varchar(10), field2 integer, field3 integer)
Please try:
SELECT * INTO NewTable FROM OldTable
Try using SELECT INTO....
SELECT ....
INTO TABLE_NAME(table you want to create)
FROM source_table
Select [Column Name] into [New Table] from [Source Table]

How to get ID from one table and associate with record in another table in SQL Server

I've tried searching for the answer to this one to no avail. There is no good logic behind the way this was setup. The guy does not know what he's doing, but it's what I have to work with (long story).
I'm using SQL Server 2008R2 I need to take records from one table and transfer the data to 4 separate tables all with a one to one relationship (I know - not smart). I need to get the value from the Identity field in the first table the data is inserted into, then populate the other 3 tables with the same ID and disperse the data accordingly. for example:
OldTable: Field1, Field2, Field3, Field4
NewTable1: Identity field, Field1
NewTable2: ID, Field2
NewTable3: ID, Field3
NewTable4: ID, Field4
I'd like to handle this in a stored procedure. I'd like to do a loop, but I read that loops in SQL are inadvisable.
Loop moving through each record in OldTable... (??)
INSERT INTO NewTable1
(Field1)
Select Field1 from OldTable
INSERT INTO NewTable2
(ID, Field2)
Select SCOPE_IDENTITY?, Field2 From OldTable Where OldTable.ID = ??
etc for other 2 tables
Loop to next record in OldTable
I am not sure how to use SCOPE_IDENTITY, but I have a feeling this will be involved in how I accomplish this.
Also, I'm probably going to need to setup a trigger for whenever a new record is created in NewTable1. I know, it's insanity, but I can't do anything about it, just have to work around it.
So, I need to know
1: the best way to initially populate the tables
2: how to make triggers for new records
The solution to 1 might involve 2.
Please help!
You can use the output clause of the merge statement to get a mapping between the existing primary key in OldTable and the newly generated identity ID in NewTable1.
-- Temp table to hold the mapping between OldID and ID
create table #ID
(
OldID int primary key,
ID int
);
-- Add rows to NewTable1 and capture the ID's in #ID
merge NewTable1 as T
using OldTable as S
on 1 = 0
when not matched by target then
insert(Field1) values(S.Field1)
output S.ID, inserted.ID into #ID(OldID, ID);
-- Add rows to NewTable2 using #ID to get the correct value for each row
insert into NewTable2(ID, Field2)
select I.ID, O.Field2
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable3(ID, Field3)
select I.ID, O.Field3
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
insert into NewTable4(ID, Field4)
select I.ID, O.Field4
from #ID as I
inner join OldTable as O
on I.OldID = O.ID
drop table #ID;
SQL Fiddle
See also Using merge..output to get mapping between source.id and target.id
How about using the OUTPUT clause of the insert statement? Assuming that Field1 is a unique key on the OldTable...
Declare #IDinserted table(ID int, Field1 varchar(255));
Insert Into NewTable1(Field1)
Output inserted.ID, inserted.Field1 into #IDinserted
Select OldID, Field1 from OldTable;
Insert Into NewTable2(RowID, Field2)
Select i.ID, o.#Field2
from #IDinserted i Inner Join OldTable o
on i.Field1=o.Field1;

SQL Server : openquery insert linked server

How do I insert data into a linked server (oracle) with a condition that a row does not exist?
I want to insert into employee table if employeecode does not exist yet in that table
INSERT INTO OPENQUERY(ORACLEX,
'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') -- I want a where clause here
Select EID, ENAME FROM EMPDATA
You might actually have to read from the table twice
INSERT INTO OPENQUERY(ORACLEX,
'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') -- I want a where clause here
Select D.EID, D.ENAME
FROM EMPDATA D
LEFT JOIN OPENQUERY(ORACLEX,
'SELECT EMPCODE, EMPNAME FROM AX.EMPLOYEE') OQ ON OQ.EMPCODE = D.EID
WHERE QQ.EMPCODE IS NULL;

SELECT INSERT INTO

Is it possible to insert data from select statement into a a dynamically created table? I will not know how many columns are there in select statement until I run the query. It has to create the appropriate number of columns at run time.
Thank you,
Smith
Just use the SELECT INTO syntax:
SELECT *
INTO NewTable
FROM MyTable
WHERE ...
The new table NewTable will be created with the same columns as your select statement.
You can use a CTAS pattern for this
USE AdventureWorks
GO
----Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO
Have a look at This Article on CTAS
-- This creates temporary table
select *
into #newtable
from YourTable
select * from #newtable
-- This creates physical table in the DB
select *
into newtable
from YourTable
select * from newtable

SQL Server 2008 Union Select 'creating' non-existent records

The results of a union select statement in SQL Server 2008 is including records that are not found in either source table. Example:
theid is an integer ID value within each source table. Neither table includes theid value of 277741.
Select *
From DataTable1
WHere theid = 277741
-- 0 records returned
Select *
From DataTable2
Where theid = 277741
-- 0 records returned
However, when you run the Union Select statement below, a record is generated for theid.
Select *
Into ConjoinedData
From DataTable1
Union
Select *
From DataTable2
Where theid Not In (Select theid From DataTable1)
Select
From ConjoinedData
Where theid = 27741
-- 1 record returned
The theid field is not an identity field in either source table. Ultimately, the data for DataTable1 and DataTable2 came from the same parent, whose content includes an unrelated record with ID 277741. However, there are no foreign key relationships to it or any other table on either of the source tables. I have tried changing to a Union All query with no success. I have created an index on theid in both source tables and the 'created' record appears. I have dropped and recreated the source tables numerous times to no avail. Why is SQL Server getting the unrelated record from the disconnected parent table (source talbes were both cereated from the same parent using Select..Into statements and no foreign key relationship to either table back to the parent)?
277741 is different from 27741. The following script reproduces exactly what you describe:
create table Table1 (id int);
create table Table2 (id int);
go
insert into Table1(id) values (277742);
insert into Table2(id) values (27741);
go
Select * From Table1 WHere id = 277741;
Select * From Table2 Where id = 277741;
go
Select *
Into ConjoinedData
From Table1
Union
Select *
From Table2
Where id Not In (Select id From Table1);
Select *
From ConjoinedData
Where id = 27741;

Resources