Help in SSIS task of adding Data - sql

I am trying to create a task of data import, my case is :
Taking ProductOrderID from ORDERS table, and searching it in MAINORDERS table and fetch all the records that match the POrderID and once the match is found, I insert the result set to ORDERDETAILS table.
and also in ORDERDETAILS table I like to add ORDERS's table OrderID as Foreign key.
Need a advice on how to perform this task.
Thanks

You would need to have a data source (OleDb or SqlServer probably, you don't state the DB being used), which selects the ProductOrderID from the Orders table, and the OrderID as well, since you'll need that later on. Then add a Lookup task to the MainOrders table, sending the matching rows to OrderDetails with an OleDb or SqlServer Destination task - the OrderID from the original source would be used to populate the FK. What do you need to do with non-matching rows?

Related

How To Update All Tables In SQL

I'm self learning SQL, using Microsoft Server management and have a question
Let say you have Customer table, Store table and Delivery table. The store needs to update their stocks count every time a Customer purchases a product or a Deliveryis made(INSERT INTO TABLE).
How to update your stock count in Store table whenever a data is Inserted into Customer table and Delivery table? And how do you Insert a New product from Delivery to your Store Table?
I think nested queries is the correct way to do both of these problems but just not sure how to do nested query for INSERT query
It is generally not possible to update multiple fields from multiple tables on a single update query (depends on your DBMS). You can add triggers, e.g. updating the Stock table after an insert on the Delivery or Customer table. If you want these updates to be done at the exact same time, use a transaction.

one-to-one tables relationship, linked by the autonumber in the main table

I have a main table that contain the customers details, I built another table that contain for example a yes/no fields about if the customer paid his taxes, the two tables is linked with autonumber from the main table.
I want always to keep them both with the same amount of records (that means every customer has a record in the second table even if the second table has empty record with data only in the primary key field)
I need that cause with missing records I cannot run update query to auto fill the second table and i got an error of validation rule violation.
I use this sql:
update clients LEFT JOIN MonthlyTbl ON clients.SerialNo = MonthlyTbl.serialno
set sReport04='ready';
I have almost 700 records in the main table and only 80 records in the second, and when I run the sql it updates only 80!!!!
Thanks for Help
Please use below query,
update clients set sReport04='ready' where SerialNo in
(select serialno from MonthlyTbl);
here is the right answer
first run the sql:
INSERT INTO monthlytbl ( serialno )
SELECT clients.serialno FROM clients
WHERE (((clients.[serialno]) Not In (select serialno from monthlytbl)));
and then:
select sreport04 from monthlytbl
set sReport04='ready';

Incremental load for Updates into Warehouse

I am planning for an incremental load into warehouse (especially for updates of source tables in RDBMS).
Capturing the updated rows in staging tables from RDBMS based the updates datetime. But how do I determine which column of a particular row needs to be updated in the target warehouse tables?
Or do I just delete a particular row in the warehouse table (based on the primary key of the row in staging table) and insert the new updated row?
Which is the best way to implement the incremental load between the RDBMS and Warehouse using PL/SQL and SQL coding?
In my opinion, the easiest way to accomplish this is as follows:
Create a stage table identical to your host table. When you do your incremental/net-change load, load all changed records into this table (based on whatever your "last updated" field is)
Delete the records from your actual table based on the primary key. For example, if your primary key is customer, part, the query might look like this:
delete from main_table m
where exists (
select null
from stage_table s
where
m.customer = s.customer and
m.part = s.part
);
Insert the records from the stage to the main table.
You could also do an update existing records / insert new records, but either way that's two steps. The advantage of the method I listed is that it will work even if your tables have partitions and the newly updated data violates one of the original partition rules, whereas an update would not accomplish that. Also, the syntax is much simpler as your update would have to list every single field, whereas the delete from / insert into allows you list only the primary key fields.
Oracle also has a merge clause that will update if it exists or insert if it does not. I honestly don't know how that would be impacted if you had partitions.
One major caveat. If your updates include deletes -- records that need to be deleted from the main table, none of these will resolve that and you will need some other way to handle that. It may not be necessary, depending on your circumstances, but it's something to consider.

SQL - Selecting a field from another table using a primary key in a trigger

I have two tables in my database, one is Transactions and the other is TransactionHistories. The latter is essentially an auditing table, whereby a trigger executes on insert, update and delete on Transactions to capture a screenshot of the data.
I am successfully retrieving all of the data stored in the Transactions table where the columns match, but the difficulty comes where I am trying to retrieve data from another table using a foreign key. For instance:
The transaction table has a field "TransactionType_TransactionTypeId", but in the audit table we wish to store its 'name' equivalent as "TransactionTypeName". This needs to be populated from the "TransactionTypes" table, which has the fields "TransactionTypeId" and "Name".
I am struggling to write a query to retrieve this as we wish. I am trying something similar to the following but having little success:
SELECT #TransactionTypeName=Name
FROM TransactionTypes
WHERE inserted.TransactionType_TransactionTypeId=TransactionTypes.TransactionTypeId;
I'm assuming that is a syntactic nightmare. If someone could point me in the right direction I would be extremely grateful!
well to get a name you should do the following
select #TransactionTypeName = TT.Name
from inserted as i
left outer join TransactionTypes as TT on TT.TransactionTypeId = i.TransactionType_TransactionTypeId
but you have to know that inserted table can have more than one row, and you are getting value for only one row.

easiest way to map ids during database refactoring

i have a number of tables with a column called OrderId. I have just done a refactoring and i want to get rid of the Order table and i have a new table called Transaction. I want all tables that have an OrderId column to now have a TransactionId column
This is complete. I now need to populate the transactionId column. I have a mapping today between orderId and transactionId so i wanted to see the quickest way i can go populate that new transactionId column (should i do this through code, through a SQL query, etc ??)
So i have the transationId column in the Order Table so i can do a join.
I want a query that says something like this (pseudo SQL)
update childTable CT
set transactionId = MapFromOrderId(CT.OrderId)
any suggestions?
I would do it in SQL code:
UPDATE MT
SET
transaction_id = MAP.transaction_id
FROM
My_Table MT
INNER JOIN My_Map MAP ON
MAP.order_id = MT.order_id
Then check to make sure that every row was mapped:
SELECT
*
FROM
My_Table
WHERE
transaction_id IS NULL
The process is usually:
Make sure the database is backed up
Addtransctionid to each child table.
Populate based on a join to the
mapping table (you did store the
mappings between orderid and
transactionid in a table?)
Make sure you have no blank values.
Then you create the FK for
transactions, drop the fk to the
Order table and then drop the orderid
column.
Then move to the next table and
repeat.
Test to make sure everything worked
properly
Definitely I'd do this in a script so it will be easy to port to prod after dev and QA testing.
On prod you need to do this while the database is in single user mode to prevent new orders from being added as the process transitions.