Data to be inserted by SSIS - sql

I have a table known as Customer(DATABASE AAA) containing 3 fields
Cust_id Cust_name Cust_salary
1 A 2000
2 B 3000
3 C NULL
I want to put data of these 3 columns in Employee(DATABASE BBB) which has the same structure as of Customer.
I want to transfer records of only those customer in which Cust_salary part is not null.
This work is to be done in SSIS only. MY values for Cust_id is auto generated & before putting values to Employee_id,the Employee table should be deleted.The auto generated identity should be preserved.

You could create a SQL Execute Task in SSIS and run the following:
INSERT INTO Employee
(EmployeeId, EmployeeName, EmployeeSalary)
SELECT Cust_id, Cust_name, Cust_salary
FROM Customer
WHERE Cust_salary IS NOT NULL

Darren Davies answer seems correct, but if for some obscure reason you have an EmployeeID is also an identity column and needs to match Cust_ID, and assuming any entries already in the Employee table correspond with the correct customer you can use an Execute SQL Task in SSIS with a connection open to Database BBB to run the following:
SET IDENTITY_INSERT Employee ON
INSERT INTO Employee (EmployeeID, EmployeeName, EmployeeSalary)
SELECT Cust_ID, Cust_Name, Cust_Salary
FROM AAA..Customer
WHERE Cust_Salary IS NOT NULL
AND NOT EXISTS
( SELECT 1
FROM Employee
WHERE EmployeeID = Cust_ID
)
SET IDENTITY_INSERT Employee OFF
This will maintain the integrity of the Identity fields in each table, and only insert new Customers to the Employee table.

what have you tried?
You will need two connections, one for each DB and one data flow component which will have a OleDBSource and an OleDBDestination component inside.
On the OleDBSource you can select your connection and write your query and then you drag the green arrow to the OleDBDestination. Double click the OleDBDestination select destination connection and table and click on mapping.
Should be it

Related

What is the output of the query if the query try to fetch information form same table multiple time

Consider the following relational data table, employee. Now find the output for the following SQL statement?
SELECT COUNT(*)
FROM employee, employee, employee
Employee table
gid
name
Three
E101
John
HRM
E102
Lucy
Marketing
E103
Rick
Management
This will produce error since you didn't use unique aliases. You need to assign unique name (alias) to all the tables in from clause. But you should run it first. Here I am sharing a fiddle link please go there and run the query.
Schema (MySQL v5.7)
create table employee (gid varchar(20), name varchar(20),Three varchar(20));
insert into employee values('E101','John','HRM');
insert into employee values('E102','Lucy','Marketing');
insert into employee values('E103','Rick','Management');
Query #1
SELECT count(*) From employee a, employee b, employee c;
count(*)
27
View on DB Fiddle

SQL On Delete Trigger

I am trying to create a trigger that when a row is deleted from a table it gets inserted in another table:
1 Create Or Replace Trigger cancel
2 After Delete
3 On OrderTable
4 For EACH ROW
5 Begin
6 Insert Into CancelledOrders Values (:old.acctNum, age, phone)
7 From OrderTable Natural Join Customer
8 Where acctid = :old.acctNum AND menuid = :old.menuNum;
9 End;
10 /
11 Show Errors;
I want to grab the acctNum, age, and phone. The acctNum is from the Order table but the age and phone is from the Customer table. Therefore I join the two tables (on the acctid key). So the joined result will look like this:
acctNum Age Phone
I get this error when I try to compile the Trigger:
2/2 PL/SQL: SQL Statement ignored
3/2 PL/SQL: ORA-00933: SQL command not properly ended
Does anyone know the problem?
EDIT:
Table Structure:
OrderTable: AcctNum MenuNum startOrder endOrder
Customer Table: AcctNum age phone
You're mixing the values and select (subquery) syntax, which are for different things. You can insert from a query that uses a value from the :old pseudorecord and values from the customer table:
Insert Into CancelledOrders -- (acctNum, age, phone)
Select :old.acctNum, age, phone
From Customer
Where acctNum = :old.acctNum;
It's better to specify the columns in the target table as part of the insert clause (I've left that commented out in case the names are different). You also don't want (or need) to requery the table the trigger is against; you already have the data you need, and it will get a mutating-table error in some circumstances. So no join is needed.
Your insert statement is incorrect. You need to specify the columns you want to select in the tables.
Add the select clause to your insert statement. Also, removed the values keyword and specify column names:
Insert Into CancelledOrders (acctNum, age, phone)
Select :old.acctNum, age, phone
From OrderTable Natural Join Customer
Where acctid = :old.acctNum AND menuid = :old.menuNum;

SQL Return Specific Value for Duplicate rows

Here is my issue
I am using bulk import to import a CSV file into a table (InputTable). This table needs to be separated into 3 different tables with distinct values.
Tables are Client, Child Company, Contacts
Client has a one to many relationship to child company as well as to contacts.
The client Table has has two fields (name,status)
it is easy enough to pull in a distinct name from dbo.InputTable using this query...
insert into Client
(
Name
)
select distinct Name
from InputTable
this query will insert data like this
Name
One Company
Two Company
Three Company
However when i try this code
insert into Client
(
Name
,Status
)
select distinct Name
from InputTable
group by Name
,Status
I get this result
Name | Status
One Company | Active
Two Company | Active
Two Company | Terminated
Three Company | Active
Here is the kicker, If client is showing active in one row no matter how many rows then i need to show active for that Name record on the Client Table
if they are showin all terminated then i would need to copy in the terminated status to the client table for that row.
any ideas on how to accomplish this?
Thank you in advance
insert into Client (Name, status)
select name,
min(status) as status
from InputTable
group by name

row in generation in ssis

I want to transfer data of table A,column emp_name to Table B with column name EMP_NAME.
I want that EMP_ID column of Table B to be equal to row_id.How this can be done in sql
or ssis..?
what have you tried?
You will need two connections, one for each DB and one data flow component which will have a OleDBSource and an OleDBDestination component inside.
On the OleDBSource you can select your connection and write your query and then you drag the green arrow to the OleDBDestination. Double click the OleDBDestination select destination connection and table and click on mapping.
Should be it
SQL:
SELECT ROW_NUMBER() OVER(ORDER BY emp_name DESC) AS 'emp_id on table B', emp_name
FROM table
The SQL is:
INSERT INTO
TableB (EMP_ID, EMP_NAME)
SELECT
A.row_id
A.emp_name
FROM
TableA AS A
but for this to work EMP_ID on TableB must not be defined as an IDENTITY column (assuming you are in SQL Server).

SQL Server 2008 - Help writing simple INSERT Trigger

This is with Microsoft SQL Server 2008.
I've got 2 tables, Employee and EmployeeResult and I'm trying to write a simple INSERT trigger on EmployeeResult that does this - each time an INSERT is done into EmployeeResult such as:
(Jack, 200, Sales)
(Jane, 300, Marketing)
(John, 400, Engineering)
It should look up for the Name, Department entry pairs, such as
(Jack, Sales),
(Jane, Marketing),
(John, Engineering)
within the Employee table, and if such an employee does not exist, should insert that into the Employee table.
What I have is this with unknowns on how to fix the "???"s:
CREATE TRIGGER trig_Update_Employee
ON [EmployeeResult]
FOR INSERT
AS
IF EXISTS (SELECT COUNT(*) FROM Employee WHERE ???)
BEGIN
INSERT INTO [Employee] (Name, Department) VALUES (???, ???)
END
Schema:
Employee
--------
Name, varchar(50)
Department, varchar (50)
EmployeeResult
--------------
Name, varchar(50)
Salary, int
Department, varchar (50)
You want to take advantage of the inserted logical table that is available in the context of a trigger. It matches the schema for the table that is being inserted to and includes the row(s) that will be inserted (in an update trigger you have access to the inserted and deleted logical tables which represent the the new and original data respectively.)
So to insert Employee / Department pairs that do not currently exist you might try something like the following.
CREATE TRIGGER trig_Update_Employee
ON [EmployeeResult]
FOR INSERT
AS
Begin
Insert into Employee (Name, Department)
Select Distinct i.Name, i.Department
from Inserted i
Left Join Employee e
on i.Name = e.Name and i.Department = e.Department
where e.Name is null
End
cmsjr had the right solution. I just wanted to point out a couple of things for your future trigger development. If you are using the values statement in an insert in a trigger, there is a stong possibility that you are doing the wrong thing. Triggers fire once for each batch of records inserted, deleted, or updated. So if ten records were inserted in one batch, then the trigger fires once. If you are refering to the data in the inserted or deleted and using variables and the values clause then you are only going to get the data for one of those records. This causes data integrity problems. You can fix this by using a set-based insert as cmsjr shows above or by using a cursor. Don't ever choose the cursor path. A cursor in a trigger is a problem waiting to happen as they are slow and may well lock up your table for hours. I removed a cursor from a trigger once and improved an import process from 40 minutes to 45 seconds.
You may think nobody is ever going to add multiple records, but it happens more frequently than most non-database people realize. Don't write a trigger that will not work under all the possible insert, update, delete conditions. Nobody is going to use the one record at a time method when they have to import 1,000,000 sales target records from a new customer or update all the prices by 10% or delete all the records from a vendor whose products you don't sell anymore.
check this code:
CREATE TRIGGER trig_Update_Employee ON [EmployeeResult] FOR INSERT AS Begin
Insert into Employee (Name, Department)
Select Distinct i.Name, i.Department
from Inserted i
Left Join Employee e on i.Name = e.Name and i.Department = e.Department
where e.Name is null
End