update email selected from another table using the foreign key relation - sql

I have 3 tables called Contact, APP_User, Customer Table. here I want to select email from my Contact table based on Contact_ID as Foreign key in App_User table and I want to Update the selected email into my Customer table based on App_User_Id as Foreign key in Customer table and primary key in APP_User Table.
Clearly:- I want to fetch email where Contact.contact_ID = APP_User.Contact_ID and Update the fetched email to Customer.emial where APP_User.App_User_Id = Customer.App_User_Id
UPDATE CRM.CRM_CUSTOMER_USER
SET EMAIL = (SELECT EMAIL
FROM QA29.ST_CONTACT
INNER JOIN QA29.ST_APP_USER ON QA29.ST_CONTACT.CONTACT_ID = 129)
WHERE APP_USER_ID = 120;
But it results in:
ORA-01427: single-row subquery returns more than one row

Join condition is missing in the inner query.
There must be multiple rows returned by the following query which is the cause of the issue:
SELECT
EMAIL
FROM
QA29.ST_CONTACT
INNER JOIN QA29.ST_APP_USER ON QA29.ST_CONTACT.CONTACT_ID = 129;
You must restrict this to single record using valid join condition, something like the following:
UPDATE CRM.CRM_CUSTOMER_USER
SET
EMAIL = (
SELECT
EMAIL
FROM
QA29.ST_CONTACT
INNER JOIN QA29.ST_APP_USER
ON QA29.ST_CONTACT.APP_USER_ID = QA29.ST_APP_USER.APP_USER_ID -- OR SOMETHING LIKE THIS
AND QA29.ST_CONTACT.CONTACT_ID = 129
)
WHERE
APP_USER_ID = 120;
Cheers!!

Related

SQL - Delete from Multiple Tables (with FK)

I'm trying to delete data from three tables.
I have an AccountId which I will set, then I want to delete the associated data.
The Id in Appointment.Id and AppointmentExtension.Id is the same value (GUID)
Table:Appointment
---------------------
- Id
- Name
- AccountId
Table: AppointmentExtension
----------------------
- Id
- Settings
Table:SettingItems
---------------------
- Id
- AppointmentExtensionId(FK)
So essentially I want to set the parameter #accountId. = "ABCD"
and delete all the records.
Note that I have a FK on SettingItems.AppointmentExtensionId = AppointmentExtension.Id,
Assume I need to delete all the items in table SettingItems before deleting the other two due to the FK?
An example of how to do this would be great?
you need tot start from the child in order as below:
delete from SettingItems
where AppointmentExtensionId in ( select Id from AppointmentExtension)
delete from AppointmentExtension where id in ( select id from Appointment where #AccountId = accountId
delete from Appointment where #AccountId = accountId
Using delete from join
delete si
from SettingItems si
join Appointment a on si.AppointmentExtensionId = a.id
and a.AccountId = #accountId;
delete ae
from AppointmentExtension ae
join Appointment a on ae.id = a.id
and a.AccountId = #accountId;

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)

Updating item based on linking data in three tables

Trying to update a field while needing to link three tables, and matching three fields in two of the tables.
I need to update TableB Options by linking the ID to the ID in TableA and matching the list from TableC by matching the Fname,Lname,address fields from TableA.
TableA: TableB: TableC:
ID ID
Fname Options Fname
Lname Lname
address address
I've tried joins and selects but can't figure out how to join on matching three fields between two tables. Considered adding an ID field to TableC and first updating the ID field from TableA and then updating TableB, but still can't figure out how to match the three fields. I don't get all the results returned.
select TableB.options, TableA.fname, TableA.lname from TableB inner join TableA on TableB.id = TableA.id inner join TableC on TableA.address = TableC.address where TableA.firstname=TableC.fname and TableA.lname=TableC.lname
It did not return the correct number of people (68 instead of 128).
Also tried this which returned the wrong list of people (using a zip field also):
Select * from TableB where ID in(Select ID from TableA where Address in(Select Address from TableC) and zip in (Select Zipcode from TableC))
Upon further investigation some records will not show because the data will not match, but for those that should match I'd like to update a new ID field in TableC. Tried the following but get an error message that I need an EXISTS:
update atblundeliverables set personid= (select id, firstname, lastname from tblpeople where firstname in (select firstname from atblundeliverables where street in (select addressline1 from atblundeliverables)))
This update should work. Try it as Select, with the Where to eliminate Nulls
--Update dbo.[Link3_TableB]
-- Set Options = 'doesMATCH'
Select Options
,dbo.[Link3_TableB].*
,dbo.[Link3_TableA].*
,dbo.[Link3_TableC].*
From dbo.[Link3_TableB]
Left Join dbo.[Link3_TableA]
On dbo.[Link3_TableB].ID = dbo.[Link3_TableA].ID
Left Join dbo.[Link3_TableC]
On dbo.[Link3_TableA].Fname = dbo.[Link3_TableC].Fname
and dbo.[Link3_TableA].Lname = dbo.[Link3_TableC].Lname
and dbo.[Link3_TableA].address = dbo.[Link3_TableC].address
Where Not dbo.[Link3_TableA].ID Is Null
And Not dbo.[Link3_TableC].Fname Is Null

Update table column based on value of key in other column POSTGRES

My table name is companies having is_nse,a boolean column and exchanges column type json having value like "[{"exchange":"NSE","ticker":"ABC"},{"exchange":"BSE","ticker":"ABC"}]"
Now i have this query
update companies set is_nse=0 from (SELECT is_nse, obj.value->>'exchange' As exch FROM (SELECT * FROM companies WHERE sector is not null) u JOIN LATERAL json_array_elements(exchanges) obj(value) ON obj.value->>'exchange' = 'BSE')y
But it is updating all the rows in companies table rather than rows of the subquery.Can anybody tell me where am I going wrong?
You need to connect the companies in the update to the from clause. Assuming you have an id, you can do:
update companies
set is_nse = 0
from (select c.*
from companies u join lateral
json_array_elements(exchanges) obj(value)
on obj.value->>'exchange' = 'BSE'
where u.sector is not null
) y
where y.companyid = companies.companyid ;

sql selecting from different tables based on boolean

I have a simple stored procedure like so:
ALTER PROCEDURE [dbo].[spList_Report]
#id INT
AS
SET NOCOUNT ON
SELECT *
FROM
tblProducts as products
WHERE
product.intID = #id
I have 2 user tables: MainUser and SubUser
Both tables have a foreign key column productID which is related to the primary key intID of the product table intID. They both also have a column emailAddress. The product table also has a bit column isMainUser.
How can I update my stored procedure to return the column emailAddress based on the isMainUser value? So if the value is true it selects the email address from the MainUser table and if its false then it selects the emailAddress from the SubUser table.
E.g what I want is only one emailAddress column:
ALTER PROCEDURE [dbo].[spList_Report]
#id INT
AS
SET NOCOUNT ON
SELECT
products.*
, myUser.emailAddress
FROM
tblProducts as products
WHERE
product.intID = #id
You have left out some important information. I am assuming that
Products can be joined with MainUser on a UserProductID
Products can be joined with SubUser on a UserProductID
There is 1 Main user for each product. (most likely not but that will need to be adressed when you have given us more information)
There is 1 Sub user for each product.
SQL Statement
SELECT products.*
, CASE WHEN isMainUser=1
THEN MainUser.emailAddress
ELSE SubUser.emailAddress
END
FROM tblProducts as products
LEFT OUTER JOIN MainUser mu ON mu.UserProductID = products.UserProductID
LEFT OUTER JOIN SubUser su ON su.UserProductID = products.UserProductID
WHERE product.intID = #id
Note that it is considered bad practice to use a SELECT *. SELECT * should never be present in production code.
Create a view on the two tables and join to that in your proc