Update statement is only inserting one value - sql

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

Related

SQL to update a table

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

How Could I Update a table with conditions from other table

I Have the Table "Person" and the table "Fisica" which is the extention of "Person". Both tables are related by the field name Id, and I want to update the tables based on conditions that include both tables.
For example:
Tables:
Persona(Id, Name, Money)
Fisica(Id, LastName, Year)
With data:
Persona(1, X, 5)
Persona(2, A, 10)
Fisica(1, Y, 1990)
Fisica(2, B, 2000)
I want to set Persona.Name=some_Value, Fisica.LastName=other_Value and Fisica.Year=number when Persona.Name='X', so it results
Persona(1, some_Value, 5)
Persona(2, A, 10)
Fisica(1, other_Value, number)
Fisica(2, B, 2000)
I am working in Oracle
You can't update two tables with one statement. So you need two UPDATE statements.
So you have to update one, then the other. The question is, which one first?
It doesn't really matter, but if you update Persona first and change the name, then you have to use the new name when updating Fisica. Like this:
update Persona
set name = 'some_Value'
where name = 'current_Name'
;
update Fisica
set lastname = 'other_Value',
year = number
where id = (
select id
from Persona
where name = 'some_Value'
)
;
If you update Fisica first, you use the old value of name both times, like this:
update Fisica
set lastname = 'other_Value',
year = number
where id = (
select id
from Persona
where name = 'current_Value'
)
;
update Persona
set name = 'some_Value'
where name = 'current_Value'
;
note number has to be replaced with an actual numeric value.
Good coding practice would be to put both statements in the same transaction and commit only if they are both successful.
Though Oracle don't provide such functionality as compare to mysql where we can write query like this:
Update table1 t1, table2 t2 set t2.field2="ABC" where t1.id=t2.id
But you can update multiple tables using single query in Oracle for Views.
So create the view for the above two tables Persona, Fisica and write query as follows:
CREATE OR REPLACE FORCE VIEW "Persona_Fisica" ("PersonaID", "Name", "Money", "FisicaID", "Lastname", "Year") AS
select P.Id as PersonaID,
Name as Name,
Money as Money,
F.Id as FisicaID,
LastName as Lastname,
year as Year
from Persona P,
Fisica F
where P.ID=F.ID
/
Update Persona_Fisica set Name=some_Value, LastName=other_Value,Year=number were Name='X',

SQL - Match String and Update Row, using Excel list

I have a DB Table [List1], 2 columns, Name, Number
SQLFiddle
I have an excel spreadsheet with 2 columns,names and numbers.
I want to match the Names in Excel to the Names column in SQL and If a match is found insert the relevant number in the second column.
Something tells me I will need to build an array / or csv and run some Tsql to achieve this.
I originally used the Task> Import data to build the DB Table.
Will importing the data again just overwrite the existing data?
What is the most efficient way to import the info, but update existing numbers? [EDIT, I have made some progress, read on]
I have managed to Create an conditional insert:
SET #PersonName = 'Andy
insert into People (Name, Number)
select
#PersonName
where not exists (
select * from People where Name = #PersonName
);
How do I pump the name list into the #PersonName variable and loop through the command in SQL?
Update:
I want to update the Datasets based on a dual column First/Last name.
Will this Work?
Update : Yes it worked, final code below.
update p
set p.number = s.numbers
from People p
join dbo.[spreadsheet] s on p.Firstname = s.Firstname AND p.lastname = s.lastname
If I understood you correctly and you want to match relatively small amount of data (up to 2k-5k rows) between excel and database table you may perform the next sequence of actions:
In SSMS execute: create table dbo.[spreadsheet] (firstname nvarchar(100), lastname nvarchar(100), numbers int);
In Excel spreadsheet copy to buffer data from firstname, lastname and numbers columns (without headers)
In SSMS Object Explorer: Tables->Right click->Refresh
Select dbo.spreadsheet table->Right click->Edit top 200 rows
In the designer select last row->Right click the on row header->Paste
And finally execute following update statement (see below)
update p
set p.number = s.numbers
from People p
join dbo.[spreadsheet] s on s.firstname = p.firstname and s.lastname = p.lastname

coldfusion Hide/remove a name from a menu/list

I have an insert form that I did in adobe coldfusion. This insert form inserts information from the men on my team into a databases table called squadreport. After every insert into my squadreport table, I would like to remove the name of the person that I just inserted from the menu list of available names of those on my team who have not been inserted yet. This way I won’t accidently reinsert their names. The menu list are generated from my NAMES tables. The name.foiid field from my NAMES table matches my squadreport.squfoiid field from my SQUADREPORT table.
Example: If the squadreport.squfoiid is already recorded with the start date of ‘2012-11-18’ and a end date of ‘2012-11-24’, then I want to prevent the reinsertion of same person with the same dates etc.
My current syntax will generate a blank list.
Here’s is what my current syntax look like:
<cfquery name="brother" datasource="master">
SELECT name.foiid, squadlt, squadlt.ltid, CONCAT(name.fname,' ',name.lname) AS teammember
FROM name
LEFT JOIN name ON name.foiid = squadreport.squfoiid
LEFT JOIN squadlt ON ltid = squadreport.sqult
WHERE name.foiid is null
AND squweekbegin =’2012-11-18’
AND squweekend = ‘2012-11-24’
AND squadlt = '3'
AND ltid = '3'
AND CITY = 'sandiego'
AND STATUS <> 'd'
AND STATUS <> 'T'
AND Form4444Complete = 'yes'
ORDER BY teammember
</cfquery>
Assuming you're inserting the rows into SquadReport with matching foiid columns, you'd just need to exclude the foiid values which exist in the SquadReport table. If there are other conditions like the squad member has to have been activated, or deployed or something, just add it to the exclusion clause.
<cfquery name="brother" datasource="master">
SELECT name.foiid, squadlt, squadlt.ltid, CONCAT(name.fname,' ',name.lname) AS teammember
FROM name
LEFT JOIN name ON name.foiid = squadreport.squfoiid
LEFT JOIN squadlt ON ltid = squadreport.sqult
WHERE name.foiid is null
AND squweekbegin =’2012-11-18’
AND squweekend = ‘2012-11-24’
AND squadlt = '3'
AND ltid = '3'
AND CITY = 'sandiego'
AND STATUS <> 'd'
AND STATUS <> 'T'
AND Form4444Complete = 'yes'
<!--- new code here --->
AND name.foiid NOT IN (SELECT foiid FROM SquadReport)
<!--- end new code --->
ORDER BY teammember
</cfquery>
So what you basically want to do is to display people not in the table squadreport? Something like this should work:
select name.foiid
, CONCAT(name.fname,' ',name.lname) AS teammember
from name
where not exists
(select 1
from squadreport
where squfoiid = name.foiid
and other constraints
)
and other constraints
I assume the code you posted is early development code. Once you go to production you will wanting to use date objects instead of strings and query parameters for any variables.

Updating a table by referencing another table

I have a table CustPurchase (name, purchase) and another table CustID (id, name).
I altered the CustPurchase table to have an id field. Now, I want to populate this newly created field by referencing the customer ids from the CustID table, using:
UPDATE CustPurchase
SET CustPurchase.id = CustID.id
WHERE CustPurchase.name = CustID.name;
I keep getting syntax errors!
I believe you are after the useful UPDATE FROM syntax.
UPDATE CustPurchase SET id = CI.id
FROM
CustPurchase CP
inner join CustID CI on (CI.name = CP.name)
This might have to be the following:
UPDATE CustPurchase SET id = CI.id
FROM
CustID CI
WHERE
CI.name = CustPurchase.name
Sorry, I'm away from my Postgres machine; however, based upon the reference, it looks like this is allowable. The trouble is whether or not to include the source table in the from_list.
Joining by name is not an ideal choice, but this should work:
UPDATE custpurchase
SET id = (SELECT c.id
FROM CUSTID c
WHERE c.name = custpurchase.name)
The caveat is that if there's no match, the value attempting to be inserted would be NULL. Assuming the id column won't allow NULL but will allow duplicate values:
UPDATE custpurchase
SET id = (SELECT COALESCE(c.id, -99)
FROM CUSTID c
WHERE c.name = custpurchase.name)
COALESCE will return the first non-NULL value. Making this a value outside of what you'd normally expect will make it easier to isolate such records & deal with appropriately.
Otherwise, you'll have to do the updating "by hand", on a name by name basis, to correct instances that SQL could not.