Update query with backup option - sql

I want to create an update that will add "xxx" to the beginning of a Product Number, which is found in
table.ProductNo
Every now and then, there might already be a code that has had xxx added to the beginning though (because sometimes product numbers get recycled- bad system I know, but it's how things go).
How can I create a query that will just add "xx" to the beginning if "xxx[table.productNo]" already exists?
The statement I'm currently using is
UPDATE table SET
table.ProductNo = "xxx" & [table.productNo],
table.Description = "xxx" & [table.Description],
table.OnInternet = 0,
table.OnInternetfs = 0,
table.OnFlyer = 0,
table.OnFSFlyer = 0
WHERE (((table.ProductNo)=[Enter Product#]));

You want to add xxx to the start of ProductNo unless it already starts with xxx. In that case you want to add xx instead.
If that is correct, you can use an IIf expression to decide whether to add 2 or 3 x characters.
UPDATE table AS t
SET
t.ProductNo = IIf(Left(t.ProductNo, 3) = 'xxx', 'xx', 'xxx')
& [t.productNo],
t.Description = "xxx" & [t.Description],
t.OnInternet = 0,
t.OnInternetfs = 0,
t.OnFlyer = 0,
t.OnFSFlyer = 0
WHERE (((t.ProductNo)=[Enter Product#]));

Try using a CASE statement to get this working.
UPDATE t
SET productNo = CASE WHEN productNo LIKE 'xxx%' THEN 'xx' + productNo ELSE 'xxx' + productNo END
FROM table t
Update
Access SWITCH statement included:
UPDATE table
SET product_id = SWITCH(
productNo LIKE 'xxx*', 'xx' + productNo,
TRUE, 'xxx' + productNo --Default
)

Related

Using Sequel Pro: How do I update a column that contains a query?

So basically I have a column called 'query' which contains a query. I need to update that query but don't know how.
I tried a simple
UPDATE TABLE
SET QUERY = 'New Query'
WHERE ID = 1
but it's getting thrown out because of the pre-existing ' ' that I have in the query.
Way out of my league on this one. Trying to learn on the fly for a position that's going to open up.
You'll have to escape the ' in the query, possibly by doubling them up or using an escape char before them:
UPDATE t
SET query = 'DELETE FROM x WHERE y = ''z'' '
WHERE ID = 1
UPDATE t
SET query = 'DELETE FROM x WHERE y = \'z\' ' ESCAPE '\'
WHERE ID = 1
You can also make life easier/neater with REPLACE, like:
UPDATE t
SET query = REPLACE('DELETE FROM x WHERE y = "z" AND a = "b" AND c = "d" ', '"', CHAR(39))
This way you can use some char all through that doesn't occur naturally in the query, and replace it with ' at the end
I can't say which variant works for your DB because you didn't say what your DB was..
WHERE ID = 1

SQL AND and OR logic error

Using MS Access 2016
Very stuck on a query error. It only is using either the "Milestone" criteria or the "HierarchyLevel" criteria - depending on which control was updated last. The Status criteria works in all cases.
Can anyone see my error? The query should meet the criteria of: chosen Status, Milestone Name search string, chosen level or Null level (ie all Levels)
Thanks Kindly
SELECT
qry_Milestones_WithHierarchy.ID,
qry_Milestones_WithHierarchy.HierarchyLevel,
qry_Milestones_WithHierarchy.Milestone
FROM qry_Milestones_WithHierarchy
WHERE
( qry_Milestones_WithHierarchy.Milestone Like "*" & [Forms]![frm_SelectMilestone]![txt_SearchTerm].[Text] & "*" )
AND ( qry_Milestones_WithHierarchy.IDStatus = [Forms]![frm_SelectMilestone]![cbo_Status] )
AND
(
qry_Milestones_WithHierarchy.HierarchyLevel = [Forms]![frm_SelectMilestone]![cbo_HierarchyLevel]
OR
Len( [Forms]![frm_SelectMilestone]![cbo_HierarchyLevel].[Text] & "" ) = 0
)
;
UPDATE
Example Date
HierarchyLevel Milestone
4 NameTest1
4 NameDemo1
3 NameTest2
3 NameDemo2
When I only use the Milestone or the Level filter I get the correct results
Controls
TextBox(Milestone) = ""
CombboBox(Status) = 1
CombboBox(HierarchyLevel) = 4
Results
HierarchyLevel Milestone
4 NameTest1
4 NameDemo1
Level 3 records are wrongly being included in the result
Controls
TextBox(Milestone) = "Test"
CombboBox(Status) = 1
CombboBox(HierarchyLevel) = 4
Results
HierarchyLevel Milestone
4 NameTest1
3 NameTest2
Try using a IIf to switch the last statement. Note that the use of wildcards at the beginning of a search renders indexing on the field redundant so large datasets will make this very slow.
SELECT
qry_Milestones_WithHierarchy.ID,
qry_Milestones_WithHierarchy.HierarchyLevel,
qry_Milestones_WithHierarchy.Milestone
FROM qry_Milestones_WithHierarchy
WHERE ( qry_Milestones_WithHierarchy.Milestone Like "*" & [Forms]![frm_SelectMilestone]![txt_SearchTerm] & "*" )
AND ( qry_Milestones_WithHierarchy.IDStatus = [Forms]![frm_SelectMilestone]![cbo_Status] )
AND ( qry_Milestones_WithHierarchy.HierarchyLevel " & IIF ([Forms]![frm_SelectMilestone]![cbo_HierarchyLevel] is null ,"Like *", " = " & [Forms]![frm_SelectMilestone]![cbo_HierarchyLevel] ) & ") ;

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?

VBA / Visual Basic. Simple String SQL

I'm creating a Voting System. so it's like this every time A Button1 is pressed + 1 or it will increase the vote in the Access database. I can't find anything in Google.
trx = "update [Table1] SET [Vote] = Vote + 1, (WHERE ID = 1)"
Don't include a comma before the WHERE clause. Also you don't need to put the WHERE clause inside parentheses.
Test this as a new query in the Access query designer.
update [Table1] SET [Vote] = Vote + 1 WHERE ID = 1
Fine tune as needed. And once you have it working in the query designer, adapt your VBA code to use that working statement.
Remove comma before Where and brackets around where clause
trx = "update [Table1] SET [Vote] = Vote + 1 WHERE ID = 1"

Replace multiple fields with one query

Instead of running two separate queries to replace the innerSku and outerSku fields in my parts table, is there a way to do this in one query without affecting performance?
UPDATE
parts
SET
innerSku = #newSku
WHERE
innerSku = #oldSku;
UPDATE
parts
SET
outerSku = #newSku
WHERE
outerSku = #oldSku;
The case statement becomes your where condition determining what value to set, only updating the column that meets the condition, otherwise it will just set the current value back.
The where clause makes it so you don't run the statement on your whole table.
SET parts.innerSku = (CASE WHEN innerSku = #oldSku THEN #newSku ELSE innerSku END),
parts.outerSku = (CASE WHEN outerSku = #oldSku THEN #newSku ELSE outerSku END)
WHERE parts.innerSku = #oldSku or parts.outerSku = #oldSku
I would stick to two separate statements, since using following will update some rows which doesnt need to be updated, it will update the rows to itself, which will not change the data but unnecessary updates operations will take place, I would say stick to two updates. anyway following is how you can update this table with one update statement.
UPDATE Parts
SET innerSku = CASE
WHEN innerSku = #oldSku
THEN #newSku ELSE innerSku
END
,outerSku = CASE
WHEN outerSku = #oldSku
THEN #newSku ELSE outerSku
END
WHERE innerSku = #oldSku OR outerSku = #oldSku