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
Related
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
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.
I am working on a simple read write database with design form.
I used this query for counting all rows
SELECT COUNT (*) FROM tblData
Now I can't get to count the rows when I am adding a filter. I tried various queries, some instructions here, but I can't decode it and make it vba. I need to find all pending status of 338559
Dim db As Database
Dim rs As DAO.Recordset
Dim query As String
Dim i As Integer
query = "SELECT COUNT Status = 'Pending' FROM tblData WHERE UID = 338559"
Set db = CurrentDb
Set rs = db.OpenRecordset(query)
For i = 0 To recordCountOfTheItem
'code to display it to listbox
Next i
Thank you for some assistance coders :)
You can use that request :
SELECT COUNT(*)
FROM tblData
WHERE UID = 338559
AND Status = 'Pending';
You can achieve this by CASE statement, if the condition is satisfies set as 1 and if not satisfies set as NULL. This will work for MySQL
When Count it won't consider the null values, so you will get valid count for your filter
SELECT COUNT(CASE WHEN Status = 'Pending' THEN 1 ELSE NULL END)
FROM tblData WHERE UID = 338559
For VBA Access 2007
SELECT COUNT(IIF(Status = 'Pending', 1, NULL))
FROM tblData WHERE UID = 338559
You don't have to count, just loop:
query = "SELECT COUNT Status = 'Pending' FROM tblData WHERE UID = 338559"
Set db = CurrentDb
Set rs = db.OpenRecordset(query)
If rs.RecordCount > 0 Then
While rs.EOF = False
'code to display it to listbox
rs.MoveNext
Wend
End If
rs.Close
Set rs = Nothing
Set db = Nothing
For the record: To fill a listbox with data from a query, you need neither Count() nor VBA at all.
Just set the listbox RowSource to the query, e.g.
SELECT <whichever fields you want to display> FROM tblData
WHERE UID = '338559' AND Status = 'Pending';
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?
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
)