How to write a sql update query for below condition? - sql

I have 2 tables. First is employee table and has (id,emp_num,name,address_id,text) columns.
Second is address table with (adress_id,emp_num,adress,state) columns.
Now when I am inserting/updating a new record into employee table, I also want to update TEXT column for all other entries for that emp_num who are from the same state.
EMPLOYEE table:
enter image description here
Address table:
enter image description here
I am trying to do a update like below:
update employee set id_text='ABCABC' where id = 'ID1';
some other update query
after that employee table should look like:
enter image description here
(note: text field is updated for ID1 and ID4)
Thanks

To me, it looks as if you need a row-level trigger which fires whenever you insert or update a row in employee table; using :new.address_id, fetch state from address table and put it into employee's text column:
create or replace trigger trg_biu_emp
before insert or update on employee
for each row
begin
select a.state
into :new.text
from address a
where a.address_id = :new.address_id;
end;
/

UPDATE employee e
SET e.text = (SELECT a.state FROM address a WHERE a.address_id = e.address_id)
WHERE e.emp_num IN (SELECT a.emp_num FROM address a WHERE a.state = (SELECT a.state FROM address a WHERE a.address_id = e.address_id));

Related

Fetch Parent Record anyway even if child condition doesn't meet without using ON

I have two tables Employee and Address.
One Employee can have multiple Address.
Here I want to fetch 'active employee details' and 'active address details' of a particular emp_id. I can achieve this by below query :
Table Structure:
Employee(emp_id,name,is_active)
Address(add_id,emp_id,address,is_active)
Query:
SELECT * FROM EMPLOYEE e
LEFT OUTER JOIN ADDRESS a
ON e.emp_id=a.emp_id
WHERE e.is_active='A'
AND a.is_active='A';
Using above query it does not return any employee details if no active address. I want to return active employee details anyways even if it does not have any active address.
Note: as I am using Hibernate looking for a query without using ON . Only Where clause can be used here.
Kindly suggest.
You need to put a.is_active='A' in ON clause not in WHERE clause
SELECT * FROM EMPLOYEE e
LEFT OUTER JOIN ADDRESS a
ON e.emp_id=a.emp_id AND a.is_active='A'
WHERE e.is_active='A';
Since you have restrictions on using condition in on clause you can try below approach. It will return rows where address is active or address is not available (assuming that is_active column is never null in address table).
Schema and insert statements:
create table EMPLOYEES(emp_id int, name varchar(20), is_active varchar(10));
create table Address(add_id int ,emp_id int ,address varchar(50),is_active varchar(10));
insert into EMPLOYEES values (1,'NAME1','A');
insert into Address values(1,1,'addr1','N');
insert into Address values(2,1,'addr1','N');
Query:
SELECT * FROM EMPLOYEES e
LEFT OUTER JOIN (select * from Address where is_active='A') a
ON e.emp_id=a.emp_id
WHERE e.is_active='A'
AND (a.is_active='A' or a.is_active is null);
Output:
EMP_ID
NAME
IS_ACTIVE
ADD_ID
EMP_ID
ADDRESS
IS_ACTIVE
1
NAME1
A
null
null
null
null
db<>fiddle here

Insert into newly created column

I have a Students table with 2 col as Rollno and Marks with 12 records
I added another column in students table as Name. How do I fill Names with 12 records from another table in SQL Server?
I tried this:
SELECT * FROM [SampleDB].[dbo].[Student_SQL]
insert into Student_SQL(name)
select name
FROM [School].[dbo].[StudentMaster]
update [Student_SQL]
set name = 'David'
where RollNo = 4`
I think you want the upate/join syntax. Assuming that the two tables relate through column rollno:
update ss
set ss.name = sm.name
from student_sql ss
inner join student_master sm on sm.rollno = ss.rollno
The upside of this approach is that it filters the rows and only update those that match in the master table.
in order to do this, you need to have column in common for both tables
update [Student_SQL] s
set name = (select name from other_table o where o.roll_no = s.roll_no)

How to fetch records from a Oracle table using SQL based on some condition

Please refer attached ER diagram.
I need to write a SQL query to fetch records from 2 main tables (Table A and Table B) based on a condition.
Condition is for a given Application (APP_ID) :
if the Table A Case_Status is True (if Case_Status = '00') then fetch all the Table A details (like First_name, Last_name) along with details from Table B (i.e. Person_name, comments from Table B) corresponding to that application
otherwise just fetch application details from Table A only (First_name, Last_name etc)
Table C is just a master table that stores the status code as 'True' or 'False' in its description Column (Status_Id->00, means True and Status_Id->01 means False).
Please suggest me on this. I want to join both columns and bring data but I can't write the condition correctly.
Thank you
This is a bit of a hack...
select a.app_id
, a.case_status
, a.first_name
, a.last_name
, b.description
, b.person_name
, b.comments
from tableA a
left join tableB b
on a.app_id = b.app_id
and a.case_status = '00'
... but it will return all the values in tableA, plus matching values from tableB when the status is correct.

Trigger Update another table After Insert

I have 2 tables Employee and Employee_Backup
Employee has 3 columns
IDEmployee
Name
Status
and Employee_Backup also has 3 columns:
IDEmployee
Detail
Status
For every row inserted into or updated in table Employee, I want to set the Status in table Employee_Backup using this criteria
WHERE employee_backup.IDEmployee = employee.IDEmployee (inserted / updated)
Something like that??
CREATE TRIGGER tr_Employee_Insert
ON dbo.Employee
FOR INSERT
AS
UPDATE b
SET Status = 'Inserted'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
CREATE TRIGGER tr_Employee_Update
ON dbo.Employee
FOR UPDATE
AS
UPDATE b
SET Status = 'Updated'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
You basically need to join the Inserted pseudo table which contains all rows that have been inserted (or updated) from the base table (dbo.Employee) and the Employee_Backup table - and then use that result set from the JOIN as the basis for your UPDATE statement.
Note: this will NOT insert any new rows into Employee_Backup when you add new rows to dbo.Employee - is that what you want? If not, you'd have to change the FOR INSERT trigger a bit ....

how to update table from an external table

I want to update a employee table based on my external table, but I got ORA-01427 error,
single-row subquery returns more than one row
employee(emp_id, emp_name, job_history, city_code)
ext_table(emp_name, job_history, city_name)
city(city_code, city_name)
data in my ext_table as following:
Sandy, waitress, los angeles
Sandy, restaurant manager, los angeles
John, store manager, phoenix
update employee em
set (em.emp_name, em.job_history, em.city_code) =
(select t.emp_name, t.job_history, t.city_code
from (select distinct(emp_name), job_history, c.city_code from
ext_table e, city c where e.city_name=c.city_name) t)
where em.emp_name=t.emp_name;
I am greatly appreciate for any help
This is what MERGE is for:
merge into employee
using
(
select e.emp_name, e.job_history, c.city_code
from ext_table e
join city c on e.city_name=c.city_name
) t on (t.emp_name = employee.emp_name)
when matched then update
set job_history = t.job_history,
city_code = t.city_code;
Note that it's useless to update emp_name as that is the column you use to join between ext_table and employee.
The above assumes that emp_name is unique in ext_table (and employee). If this is not the case you will need to find some "key" that uniquely identifies the employee in the external table.
Also: distinct is NOT a function.
select distinct (foo), bar is absolutely identical to select distinct foo, bar. It always operates on all columns. The difference between the two is the same as the difference between select (foo),bar and select foo, bar.
Another possible option is to convert external table to regular table and update. To convert external to regular table use SQL Developer. Steps: 1.Create empty table with the same structure as your external table. 2.Export data from external to reg. table. The process is similar to exporting data from file to Excel.
you can do it by using update query with join . check following code
UPDATE employee SET job_history = B.job_history , city_code =
C.city_code FROM EMPLOYEE as A INNER JOIN ext_table AS B ON
A.emp_name = B.emp_name INNER JOIN city AS C ON B.city_name =
C.city_name