SQL to update a table - sql

So I have a table that has columns such as:
RequestID Owner Name Owner Email Date GUID Comments (etc)
1 Joe Soap joe.soap#gmail.com 1/2/19 asdfasdfasdf
2 Sally Soap ssoap#hotmail.com 9/7/15 asf2f23f23f2
3 Joe Schmo jschmo#yahoo.com 1/1/19 af2f2f2f2f2f
4 Adam ABC abc_adam#adam.com 3/2/18 f89282822828
What I've done is exported the table to an excel file and made changes in the excel file. I've made changes to the 'Owner Email' and 'Owner Name' columns for specific RequestID.
What I'm trying to do is import the three columns to a temp table ("RequestID", "Owner Name", "Owner Email"). I then want to update the main table based on the Request ID. So it would update the owner name and owner emails such as below:
RequestID Owner Name Owner Email
3 Joe Schmo Jr joesnewemailaddr#gmail.com
1 Joe Replacement newjoe#yahoo.com
The script I THINK I need is this:
UPDATE
MainTable
SET
MainTable.OwnerName = New.OwnerName
MainTable.OwnerEmail = New.OwnerEmail
FROM
MainTable
INNER JOIN
UpdateTable
ON
MainTable.RequestID = UpdateTable.RequestID
This should do it, right?

I am assuming that the "UpdateTable" is the temp table? Your SQL isn't right, but you don't specify a DBMS either, so here it goes:
UPDATE
MainTable
SET
MainTable.OwnerName = New.OwnerName,
MainTable.OwnerEmail = New.OwnerEmail
FROM
MainTable
INNER JOIN
UpdateTable AS New
ON
MainTable.RequestID = New.RequestID
Notice how I alias the UpdateTable as New. However you could have just omitted the New alias and used UpdateTable in your SET.
UPDATE
MainTable
SET
MainTable.OwnerName = UpdateTable.OwnerName,
MainTable.OwnerEmail = UpdateTable.OwnerEmail
FROM
MainTable
INNER JOIN
UpdateTable
ON
MainTable.RequestID = UpdateTable.RequestID

Related

SQL - How to substitute a result which was retrieved by a SELECT clause

Suppose you have 2 tables,
Citizen
Admin
An Admin is inherently a Citizen, so Admin has a FK into Citizen's PK.
Next when a Citizen entry is created, the entry records which Admin entered it.
Citizen includes details like the citizen's name, whereas admin includes other details not related to a citizen's details.
So then I have a query,
SELECT ctzName, ctzEnteredByID
FROM Citizen
INNER JOIN Admin
ON ctzPK = admPK
WHERE ctzPK = 2
This query returns the name of the Citizen and the ID of the Admin who created the Citizen entry. In this case that ID would be admPK/ctzPK of the Admin. As an example, let's say the Citizen table has 2 entries,
ctzPK = 1, ctzName = Chris, ctzEnteredByID = 0
ctzPK = 2, ctzName = John, ctzEnteredByID = 1
then this query returns,
John, 1
However, I want to print the name of the Admin who entered it as well. So I want my result to be
John, 1, Chris
I've tried SELECTing this value as a condition in WHERE, but to no avail. How do you do this type of substitution?
Just add another two INNER JOIN (and alias the extant table references) to get the data for the subjectCitizen.ctzEnteredByID.
Like so:
SELECT
subjectCitizen.ctzName AS "Subject Citizen Name",
subjectCitizen.ctzEnteredByID AS "Subject Citizen's data entered by (ID)",
makerCitizen.ctzName AS "Subject Citizen's data entered by (Name)"
FROM
-- Subject data:
dbo.Citizen AS subjectCitizen
INNER JOIN dbo.Admin AS subjectAdmin ON
subjectCitizen.ctzPK = subjectAdmin.admPK
-- Subject's creator:
INNER JOIN dbo.Admin AS maker ON
maker.admPK = subjectCitizen.ctzEnteredByID
INNER JOIN dbo.Citizen AS makerCitizen ON
maker.admPK = makerCitizen.ctzPK
WHERE
subjectCitizen.ctzPK = 2
You should have a foreign key on citizen table like "AdminId" , then your select query changes to this :
SELECT ctz.Name, ctz.AdminId, ad.Name
FROM Citizen ctz
INNER JOIN Admin ad
ON ctz.AdminId = ad.Id
WHERE ctz.Id = 2

update a column in a sql table with a value from another table based on a relationship

I am working on a SQL query to update a column values with an Id from a different table
Example
Organization Table
Id Name
1 AA
2 BB
Events Table
Id Name OrgId
1 AA NULL
2 AA NULL
3 BB NULL
Now, I would like to update OrgId of Events table with its respective Id from Organization table
I did try the below query but I had explicitly do it for each organization
UPDATE Event SET OrId=
(SELECT DISTINCT O.ID FROM Organization O WHERE O.Name='AA') WHERE Name='AA'
May I know a better way to do it automatically?
Use a join:
update e
set orid = o.id
from event e join
organization o
on o.name = e.tenant;
You can perform a Merge using
MERGE Event AS e
USING Organization AS o
ON (e.Name= o.name)
WHEN MATCHED THEN
UPDATE SET e.OrgId = o.id
OUTPUT $action, inserted.*;
The output clause is optional and will print out the Ids that are inserted into the Event table.
The Merge is quite powerful as it has other clauses that can be used for cases when data is only in one table and not the other. Here's a nice post which explains things clearly. https://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/

I'm trying to update a table with information based on another table

I have a table with a list of SSNs (not real ones) connected to houseIDs.
I need to update a specific SSN's houseid based on a given address.
I have another table with houseIDs connected to HouseAddresses.
Here is what I have so far:
update persons
set houseid = houses.houseid
from houses
where houses.houseaddress = 'Alma Street'
and persons.SSN = 675849512;
While this works, it doesn't update anything, and I know it should update one row.
I'm afraid I don't know where to go from here.
Try to use this
update persons
set persons.houseid = houses.houseid
from houses
where houses.houseaddress = 'Alma Street' and
persons.SSN = 675849512;
OR
update persons
set houseid = i.houseid
from (
select houseid,houseaddress
from houses) i
where i.houseaddress = 'Alma Street' and
persons.SSN = 675849512;
if you want to update your houseid from other table connected by address you should join tables by addresses
Look this
update persons
set houseid = (Select Top(1) houses.houseid from
from houses
where houses.houseaddress = persons.address
.....
);
TRY IN THIS WAY: Use JOIN for effective solution in the following way but in the below code I am taking SSN as the common column but you can replace it with one if different:
UPDATE p SET houseid = h.houseid
FROM persons p
INNER JOIN houses h ON h.SSN = p.SSN
AND h.houseaddress = 'Alma Street'
AND ----------
--------
You can add more conditions according to you requirement in AND or add WHERE clause if required.

Update statement is only inserting one value

I have data in one table (OWNER) i'm trying to use to update the data in another table (TAX_BILL_INFO). I've written an update statement to take the value from one column (owner.ownername) and insert that value into another column (tax_bill_info.mailername) where they have the same ID (taxbillid). below is my statement that I used:
UPDATE TAX_BILL_INFO
SET Mailername = OWNER.Ownername
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
where taxyear = '2013' and (mailername = '' or mailername = ' ' or mailername is null) and (purchasername = '' or purchasername = ' ' or purchasername is null)
The columns are updating, it is only putting the FIRST value from OWNER into EVERY column in tax_bill_info. (ex. john smith, john smith, john smith). Are my where criteria throwing this off? Or could it be something else?
EDIT
If I use this select query:
select owner.OwnerName
FROM owner INNER JOIN
TAX_BILL_INFO ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
the names pull as they should. If i do a select query from the table I need the data entered into, it gives me the same dupliacte name for every row:
SELECT MAILERNAME
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID

Read Rows But search first

I want to make an import system that will look into one Datasource and copy new records into another DataSource.
Monthly I want to copy some tables data from one datasource to another datasource
SourceTableName : srcTable
DestinationTableName : destTable
Suppose first month in source table I have:
Id Name 1 john
3 Rahul 5 Andrew
All three rows Will be copy into desTable
Suppose Second Month in Source Table I have
Id Name 1 John
3 Rahul 5 Andrew
6 Vikas 7 Sonam
8 Divya
Firstly Sql Should get the last Row of desTable
and match that row into srcTable
and extract all new records from scrTable and copied into desTable
.....
Please let me know how I can write query for fulfill above purpose. If there is shorter approach, that would be helpful too.
Since you only care about adding new records, and don't need to handle updates or deletes... You can simply add the record from the source table if it doesn't exist in the destination table:
INSERT INTO destTable (ID, Name)
SELECT s.ID, s.Name
FROM
srcTable s
LEFT OUTER JOIN destTable d ON d.ID = s.ID
WHERE
d.ID IS NULL
You can write a stored procedure for do this action and execute that every time you want.
for this action you can from bellow query:
(Part 1 for insert new data, Part 2 for update change data)
Insert Into DestinationTable(ID, Name)
Select ID, Name
From SoiurceTable
Where Not Exists
(Select *
From TDestinationTablest
Where DestinationTable.ID = SoiurceTable.ID)
Go
Update DestinationTable
Set DestinationTable.Name = SoiurceTable.Name
From DestinationTable, SoiurceTable
Where DestinationTable.ID = SoiurceTable.ID
I hope it's helpful.