update each row in a procedure - sql

I have the following data in a table TABLE1
DOCUMENT ------ FIELD1
12345
23456
34567
45678
98765
i have the following data in a view VIEW1
DOCUMENT ---- BUS
12345 ------------ 5
23456 ------------ 6
34567 ------------ 8
45678 ------------ 12
98765 ------------ 14
What i would like to do is update each row
if (table1.document = view1.document)
then
table1.field1 = view1.bus
Any insight will help.
Thank you.

That can be done using plain SQL, no procedures required:
UPDATE table1 SET field1 = (SELECT bus FROM view1 WHERE table1.document = view1.document)
Or, if your database allows it:
UPDATE (select table1.field1, view1.bus FROM table1 JOIN view1 ON table1.document = view1.document) SET table1.field1 = view1.bus

As Dan said, but in MS SQL Server I find this styling easier to read:
UPDATE U
SET U.field1 = V.bus
FROM table1 AS U
JOIN view1 AS V
ON V.document = U.document
Note that if VIEW1 could have multiple rows for a given TABLE1 row [DOCUMENT] value then the [BUS] value choosen to update TABLE1 will be random, within the matching set. (If this is the case the query could be modified to choose MAX / MIN / etc.)
I would refine this query to NOT update any rows that already matched the BUS value, which will make it faster if it is rerun and thus some values already exist in TABLE1
UPDATE U
SET U.field1 = V.bus
FROM table1 AS U
JOIN view1 AS V
ON V.document = U.document
WHERE U.field1 = V.bus
OR (U.field1 IS NOT NULL AND V.bus IS NULL)
OR (U.field1 IS NULL AND V.bus IS NOT NULL)
you can leave out the NULL / NOT NULL tests if the field is defined as not allowing NULLs.

Related

Update a range of rows in a table where the condition is in another table

example:
table 1
invoice line article price
1 1 pen 10
1 2 mouse 11
1 3 paper 15
2 1 ... 25
2 2 ... 80
2 3 ...
2 4 ....
table 2
invoice date
1 2014-01-03 00:00:00.0000
2 2014-05-12 00:00:00.0000
3 2014-06-17 00:00:00.0000
how can I update the price only on the rows where the invoice date is for example the month of november
I know that i must use a join but i'm already joining this table for doing other stuff in the same query:
UPDATE invoicelines
SET invoicelines.netprice = ART.price
FROM invoicelines IL INNER JOIN items ITM
ON IL.item = ITM.item
i want to update the invoicelines with a specified date, but this date is in another table, and the situation is similar to the first example
update table1
set price = price + 10
from table1 t1 join table2 t2 on t1.invoice = t2.invoice
where month(t2.date) = 11
As to "I know that i must use a join": No, you don't have to join. When a condition is in another table you would usually use EXISTS or IN.
update table1
set price = ...
where invoice in
(
select invoice
from table2
where month(`date`) = 11
);
Or:
update table1
set price = ...
where exists
(
select *
from table2
where month(table2.`date`) = 11
and table2.invoice = table1.invoice
);
You forgot to name your dbms. Date functions are mostly dbms specific, so a MONTH function may be available or not.
I Solved With This
UPDATE Invoicelines
SET Invoicelines.NetPrice = ART.Price
FROM Invoicelines BR INNER JOIN Items ART join InvoiceHead bt on month(bt.docdate)=11
ON BR.Item = ART.Item
where BR.sheetNumber=bt.sheetNumber

sql update table set - The multi-part identifier could not be bound

I have 2 tables:
Table1 = names of gas stations (in pairs)
Table2 = has co-ordinate information (longitude and latitude amongst other things)
Example of Table1:
StationID1 StationID2 Name1 Name2 Lattitude1 Longitude1 Lattitude2 Longitude2 Distance
------------------------------------------------------------------------------------------------
93353477 52452 FOO BAR NULL NULL NULL NULL NULL
93353527 52452 HENRY BENNY NULL NULL NULL NULL NULL
93353551 52452 GALE SAM NULL NULL NULL NULL NULL
Example of Table2:
IDInfo Name Lattitude Longitude
-------------------------------------------
93353477 BAR 37.929654 -87.029622
I want to update this table with the coordinate information which resides in tableA. I tried to do the following as per SQL Server 2005: The multi-part identifier … could not be bound
update table1
set t1.[Lattitude1] = t2.[Lattitude]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
I get the following error message:
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "t1.Lattitude1" could not be bound.
However, if I do the following it works which I can then store into another table.
SELECT t1.[StationID1]
,t1.[StationID2]
,t1.[Name1]
,t1.[Name2]
,t2.[Lattitude] AS [Lattitude1]
,t2.[Longitude] AS [Longitude1]
,t3.[Lattitude] AS [Lattitude2]
,t3.[Longitude] AS [Longitude2]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
left join table2 t3
on (t1.StationID2 = t2.IDInfo)
I am very new to SQL and am having a difficult time understanding why some things work and others don't. Based on the link I posted above my initial query should have worked - no? Perhaps I'm not thinking straight as I have spent many hours trying this and I finally got help from a co-worker (she suggested the approach I mention above).
I think you can modify your UPDATE statement to reference the table alias in the UPDATE line.
update t1
set t1.[Lattitude1] = t2.[Lattitude]
from table1 t1
left join table2 t2
on (t1.StationID1 = t2.IDInfo)
You need to change the inner table and give a different allias to the columns that are similar. This should work.
update table1
set [Lattitude1] = x.[lat]
from
(
SELECT IDInfo [id], Lattitude [lat] FROM
table2
) x
WHERE
StationID1 = x.[id]
In your particular case its not necessary to rename Lattitude to lat, but if you end up updating a table with itself and force yourself into giving the columns different names, it will save you headaches down the road.

modify value on a table with some conditions

I need some help with mssql. I dont know anything from mssql query, but I know some programing.
I have 2 tables, TableA and TableB each one has 2 columns
ColumnA1, ColumnA2, ColumnB1 and ColumnB2
Something like this
create table DB.dbo.TableA
(ColumnA1 varchar(10),
ColumnA2 int)
create table DB.dbo.TableB
(ColumnB1 varchar(10),
ColumnB2 int)
And I need to check if exist a row in TableA where ColumnA2>0
if so, then,
IF any of those possible rows also exists in ColumnB1,
update ColumnB2=ColumnB2+ColumnA2 and set ColumnA2=0
ELSE
insert a new row in TableB with ColumnB1=ColumnA1 and ColumnB2=ColumnA1 and set ColumnA2=0
I don't even know where to start, I tryed to explain it the best way I could.
EDIT:
Before the script
TableA:
ColumnA1 ColumnA2
John 0
Sam 1
Mark 1
TableB:
ColumnB1 ColumnB2
Sam 5
After the script should be something like this:
TableA:
ColumnA1 ColumnA2
John 0
Sam 0
Mark 0
TableB:
ColumnB1 ColumnB2
Sam 6
Mark 1
You should use the MERGE syntax
merge TableB
using
(select * from tablea where ColumnA2>0) source
ON source.ColumnA1 = tableb.ColumnB1
when matched then
update
set tableb.ColumnB2 = tableb.ColumnB2 + source.ColumnA2
when not matched by target Then
insert(ColumnB1,ColumnB2)
values (source.ColumnA1,source.ColumnA2)
I couldn't get your requirement properly but the below code using Merge will atleast help you in the right direction .
MERGE TableB B
USING TableA A
ON A.ColumnA1 = B.ColumnB1 //The columns which related to both the tables
WHEN MATCHED AND
A.ColumnA2>0 THEN
UPDATE
SET B.ColumnB2 = B.ColumnB2 + A.ColumnA2
when not matched by target Then
Insert(ColumnB1,ColumnB2)
values (A.ColumnA1,A.ColumnB2)
Output A.ColumnA1 into #t;
Update TableA
Set ColumnA2=0
WHERE ColumnA1 in (SELECT ColumnA1
FROM #t);

Update query for a specfic columns in all rows

I'm searching for check a columns value in all the rows and update with a where clause as a condition. My case is as follows:
SubscriptionID ChannelURI StudentID
1 XXXX 4
2 yyyy 4
3 XXXX 3
4 XXXX 4
5 XXXX 2
I want to check the column channel uri value for a specfic student and for all matched results to set it to null.
So in this case row 3 and 5 should be set to null.
I've tried this, but it set all channeluri of other rows than studnetid = 4 to null
UPDATE SubscriptionCourse
Set ChannelURI = 1
, DeviceId = null
FROM SubscriptionCourse as t1
INNER JOIN SubscriptionCourse as t2
on t1.ChannelURI = t2.ChannelURI
WHERE StudentId! = 4
Reference the table to be updated by it's alias given in the FROM clause, rather than by name (since the same table name is referenced twice. Also qualify the reference on StudentId in the WHERE clause with the table alias as well.
UPDATE t1
SET t1.ChannelURI = 1
, t1.DeviceId = NULL
FROM SubscriptionCourse t1
JOIN SubscriptionCourse t2
ON t1.ChannelURI = t2.ChannelURI
WHERE t1.StudentId != 4
You say you want to set ChannelURI to NULL, but your statement is setting to a literal value of 1. I've left the assignment as you specified in your statement, but qualified the columns with the table alias.
I don't think this is your problem, but I never include a space in the "not equals" comparison operator symbol (!=). I've just never seen that before. I prefer to use the <> symbol for the "not equals" comparison operator.
From your description of the problem and your example, it's not at all clear why you need to join the table to itself.
I recommend you FIRST write a SELECT statement that returns the rows you want to update, by replacing the UPDATE and SET clauses with a SELECT <expression_list> clause, with the expression_list including the value of the primary key column(s), the column you want to update, and any other columns you want to check. Once that SELECT is returning the rows you want to update, then convert it into an UPDATE statement.
update [table_name] set channelURI = "null" where SubscriptionID = 3 or SubscriptionID =5
in this case,SubscriptionID must be a primary key.
Update table set channeluri = null where studentid <> 4
That what you want? Or you want to find all the ones that have the same uri as student 4 and null those?
Update table set channeluri = null from table inner join table t2 on table.channeluri =t2.channeluri where table.studentid <> 4 and t2.studentid =4
Something like that, im on my phone, the wife has stolen the pc

Tricky SQLite query, could use some assistance

I have a rather confusing SQLite query that I can't seem to quite wrap my brain around.
I have the following four tables:
Table "S"
sID (string/guid) | sNum (integer)
-----------------------------------
aaa-aaa 1
bbb-bbb 2
ccc-ccc 3
ddd-ddd 4
eee-eee 5
fff-fff 6
ggg-ggg 7
Table "T"
tID (string/guid) | ... other stuff
-----------------------------------
000
www
xxx
yyy
zzz
Table "S2TMap"
sID | tID
-------------------
aaa-aaa 000
bbb-bbb 000
ccc-ccc xxx
ddd-ddd yyy
eee-eee www
fff-fff 000
ggg-ggg 000
Table "temp"
oldID (string/guid) | newID (string/guid)
------------------------------------------
dont care fff-fff
dont care ggg-ggg
dont care zzz
What I need is to be able to get the MAX() sNum that exists in a specified "t" if the sID doesn't exist in the temp.NewID table.
For example, given the T '000', '000' has S 'aaa-aaa', 'bbb-bbb', 'fff-fff', and 'ggg-ggg' mapped to it. However, both 'fff-fff' and 'ggg-ggg' exist in the TEMP table, which means I need to only look at 'aaa-aaa' and 'bbb-bbb'. Thus, the statement would return "2".
How would I go about doing this?
I was thinking something along the lines of the following for selecting s that don't exist in the "temp" table, but I'm not sure how to get the max of the seat and only do it based on a specific 't'
SELECT s.sID, s.sNum FROM s WHERE NOT EXISTS ( SELECT newID from temp where tmp.newID = s.sID)
Thanks!
Give this a try:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and not exists (
select * from temp
where temp.newId = st.sId)
Here is the fiddle to play with.
Another option, probably less efficient would be:
select max(s.sNum) result from s2tmap st
join s on st.sId = s.sId
where st.tId = '000' and st.sId not in (
select newId from temp)
The following query should give you a list of Ts and their max sNums (as long as all exist in S and S2TMap):
SELECT t.tID, MAX(sNum)
FROM S s
JOIN S2TMap map on s.sID=map.sID
JOIN T t on map.tId=t.tID
LEFT JOIN temp tmp on s.sID=tmp.newID
WHERE tmp.newID IS NULL
You were close, you just had to join on S2TMap and then to T in order to restrict the result set to a given T.
SELECT MAX(s.sNum)
FROM s
INNER JOIN S2TMap m on m.sID = s.sID
INNER JOIN t on t.tID = m.tID
WHERE t.tID = '000'
AND NOT EXISTS (
SELECT newID FROM temp WHERE temp.newID = s.sID
)