UPDATE query - multiple criteria for same field in MS Access - sql

I am new to scripting in MS Access and would like to do multiple queries in one instance. I need this query to UPDATE records based off the SET criteria. Below is what I have so far.
UPDATE Inspections SET Inspections.Surveyed_By = "TDG/DT"
WHERE Inspections.Surveyed_By="TDG/DavidT";
OR
UPDATE Inspections SET Inspections.Surveyed_By = "TDG/RW"
WHERE Inspections.Surveyed_By="TDG/RobbieW"

Is this what you are looking for?
UPDATE Inspections
SET Surveyed_By = IIF(Surveyed_By = 'TDG/DavidT', 'TDG/DT', 'TDG/RW')
WHERE Surveyed_By IN ('TDG/DavidT', 'TDG/RobbieW');

You can use Switch
UPDATE Inspections
SET Surveyed_By = Switch(
Surveyed_By = "TDG/DavidT", "TDG/DT",
Surveyed_By = "TDG/RobbieW" , "TDG/RW" );
WHERE
Surveyed_By = "TDG/DavidT"
OR Surveyed_By = "TDG/RobbieW"

Related

Set hive variables based on condition

I want to set hive variables based on condition. I am not sure if that's possible in HQL or not and if it is possible then how to achieve that.
I want to do something like this,
if ${hiveconf:work}==1 then do
set q1 = 4;
set yr2 = eval(${hiveconf:year}-1);
set q3= 3;
end
else if ${hiveconf:work}==2 then do
set q2 = 4;
set yr1 = eval(${hiveconf:year}-2);
set q4= 2;
end
Any help and knowledge will be appreciated.

SQL Update statement not working - SQL Server

I am trying to run the below sql statement (SQL Server), however getting the error
"FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins."
update fp
set fp.totalcapacity = hc.totalcapacity,
fp.sellablecapacity = hc.sellablecapacity
from [fact].[FinalPosition] fp
join fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey
and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18'
I can't seem to understand why I am getting this error. Any idea?
I think the syntax you want is:
update fp
set totalcapacity = hc.totalcapacity,
sellablecapacity = hc.sellablecapacity
from fp join
fact.[HotelCapacity] hc
on fp.hotelkey = hc.hotelkey and fp.staydate = hc.staydate
where fp.staydate = '2016-06-18';
If you want fp to refer to an actual table, include that in the from clause and make the fp the alias for the table.

UPDATE same column multiple times in query

Is there a specified behavior for updating the same column 2+ times in the same UPDATE query, as follows?
UPDATE tbl SET a = 5, b = 'something', a = 6 WHERE c = 'whatever';
Is there a standardized behavior for this, or might it vary between flavors of SQL (e.g. it is "undefined behavior")? A cursory test with sqlite seems to indicate they are executed left-to-right, so the last column value will be the resulting one, but that doesn't imply that will always be the case.
Edit: The reason I'm trying to do this is I'm testing some SQL injection for a class project. One of the fields in an UPDATE is unsafely injected, and I'm trying to use it to overwrite previously SET fields from the same query.
This isn't exactly the answer you're looking for but assuming that the text "something" is a field you are passing in and it isn't parameterized or escaped you may be able to do this. This all depends on how the query is being built and what database it is being run against.
UPDATE tbl SET a = 5, b = 'something'; UPDATE tbl set a = 6;--' WHERE c = 'whatever';
by entering the following in the user input
something'; UPDATE tbl set a = 6;--
This assumes that the query is built dynamically something like this
var query = "UPDATE tbl set a = 5, b = '" + userInput + "' WHERE c = 'whatever'";
Here is a relevant question: How does the SQL injection from the "Bobby Tables" XKCD comic work?

SQL update syntax with subquery

I have this update statement:
UPDATE quotedetailextensionbase qd
SET qd.new_capvehicleid = (SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype)
I get an error back but I'm not sure why
Incorrect syntax near 'qd'
Seems like it would be easier to achieve this result with an update-join statement:
UPDATE qd
SET new_capvehicleid = cv.new_capvehicleid
FROM quotedetailextensionbase qd
JOIN (SELECT new_capvehicleid
FROM vwCapidLookup) cv ON cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype
SQL Server lets you do joined updates:
UPDATE
qd
SET
new_capvehicleid = cv.new_capvehicleid
FROM
quotedetailextensionbase qd
join vwCapidLookup cv on
cv.new_capid = qd.new_capid
AND cv.new_captype = qd.new_captype
;
Does that make sense?
Please try the below SQL:
UPDATE quotedetailextensionbase
SET new_capvehicleid =
(SELECT cv.new_capvehicleid
FROM vwCapidLookup cv
WHERE cv.new_capid = qd.new_capid AND
cv.new_captype = qd.new_captype)
Use MERGE: https://msdn.microsoft.com/en-us/library/bb510625.aspx. That is correct when you would update from another table.

Updating Table with System Date

My question is very simple:
I have a column named "DateProcessed". Whenever User Clicks a Button, the column should be updated for each row with the current System.Date.
Here is my code:
update dbo.JobStatus SET DateShipTransmitProcessed = ???? WHERE JobTableId = #JobTableId
What should go in ????. Thanks for your help!
The ANSI standard would be to use current_timestamp, which should work for MySql, SQL Server, and any other ANSI compliant RDBMS.
update dbo.JobStatus SET DateShipTransmitProcessed = current_timestamp WHERE JobTableId = #JobTableId
If you are on SQL server..
update dbo.JobStatus SET DateShipTransmitProcessed = GetDate() WHERE JobTableId = #JobTableId