sql server if statement not working - sql

Can anyone advise me as to what is wrong with the following SQL server update statement:
IF (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT') = ''
GO
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID = (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')

You don't need an if statement - you can just run the update statement, and if the subquery returns no rows, no rows will be updated. The if won't really save anything - you're performing two queries instead of one.

You either want
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID In (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
if there are multiple ID's with that code OR use
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE code = 'SOUND_WRONG_GARMENT'
either way and lose the IF statement as #Mureinik said.

Although Mureinik's answer is the logical solution to this, I will answer why this isn't actually working. Your condition is wrong, and this approach will work instead using IF EXISTS:
IF EXISTS (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT')
BEGIN
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID IN (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
END
As a side note, you're using an = sign instead of IN, which means you'll be matching to an arbitrary singular ID and only update 1 row based on this. To use a set based operation, use the IN clause.
You could actually 'golf' this by doing away with the derived query altogether, and using a simple WHERE code='SOUND_WRONG_GARMENT' on the table you're updating on.

Related

Update table only if value does not exist in a specific column

I want to update a column (variableA) in a table (myTable) only when
There is no dataset with this #variableA in the variableA column
There is already a dataset with #variableB in the variableB column and with 'DUMMY' in the variableA column
FYI: Another interface inserts the 'DUMMY' datasets before and I later need to update them with the real variables/numbers.
The code below is already working fine but I am wondering if there is a more "elegant" solution to do this. I want to avoid/change the last line ("SELECT COUNT(*)" etc.)
DECLARE #variableA nvarchar(10) = '12345'
DECLARE #variableB nvarchar(10) = '67890'
UPDATE TOP (1) myTable
SET variableA = #variableA,
timestamp = GETDATE()
WHERE variableB = #variableB
AND variableA = 'DUMMY'
AND (SELECT COUNT(*) FROM myTable WHERE variableA = #variableA) = 0
Can you please help me to find a smarter solution instead of this last line?
you can use not exists operator like this
not exists (SELECT 1 FROM myTable WHERE variableA = #variableA)
and if it again slow you can set index I_my_Table_variableA by your variableA column and it will be more faster(you can set index by variable because it almost unique and it will be good index)
Well, I would write it like this:
UPDATE myTable
SET variableA = #variableA,
timestamp = GETDATE()
WHERE variableB = #variableB
AND variableA = 'DUMMY'
AND NOT EXISTS (
SELECT 1
FROM myTable
WHERE variableA = #variableA
)
First, Using TOP without specifing ORDER BY is a mistake, since database tables are unsorted by nature, this actually means that you might get unexpected results.
Second, changing the (select count) > 0 to exists(select...) might improve performance (unless the optimizer is smart enough to use the same execution plan for both cases)
Also, for your future questions - Please avoid using images to show us sample data and desired results. Use DDL+DML to show sample data, and text to show desired results. If you do that, we can copy your sample data to a test environment and actually test the answers before posting them.

Find multiple SQL columns and update based on defined data listed in query

I have an update query in which I am trying to locate data in a column from a single table. All while taking other defined data listed in the query to update another column in the same table once a match has been found with that original search. Below is an example of my update statement. My end goal is to find '003447710' then update AltId to '540112'
UPDATE Site
SET AltId = ('540112'
'540129'
'540142'
'540143')
WHERE CCMFStatus in ('003447710',
'002754540',
'003564370',
'005942870')
I am sure there may already be something like this out there but I am really having trouble on an easy method on how to do this quickly and accurately.
Try this
update site
set altid = a.altid
from
(select altid,CCMFstatus from site) as a
where site.CCMFstatus = a.CCMFstatus
The best way might be multiple update statements:
UPDATE Site
SET AltId = '540112'
WHERE CCMFStatus = '003447710';
And so on.
If not, you can do this with a giant case statement or a join:
WITH values as (
SELECT '003447710' as oldstatus, '540112' as newaltid UNION ALL
SELECT '002754540', '540129' UNION ALL
SELECT '003564370', '540142' UNION ALL
SELECT '005942870', '540143'
)
UPDATE s
SET AltId = va.newaltid
FROM site s JOIN
values v
ON s.CCMFStatus = v.oldstatus;
If you already have the values in a table, then you don't need the WITH statement. You can just use the table.
Have you tried using CASE statement?
UPDATE SITE SET AltID = (CASE
WHEN CCMFStatus = '003447710' THEN '540112'
WHEN CCMFStatus = '002754540' THEN '540129'
END)
WHERE
CCMFStatus in ('003447710', '002754540', '003564370', '005942870');
BR,

Alternative update SQL query

I am looking for an alternative for this query. I know that such query will end up with invalid identifier in Oracle. So please give me the same query for updating one filed from another field in another table.
update RBT_CMP_RECOM_9304
set FIRST_RECOM_NAME=(select rbt_cmp_base_code.RBT_NAME
from rbt_cmp_base_code
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM)
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM;
FYI:
RBT_CMP_RECOM_9304=(firt_recom,first_recom_name)
RBT_CMP_BASE_CODE = (rbt_code, rbt_name)
I get this error when I try it:
ORA-00904: RBT_CMP_BASE_CODE.RBT_CODE: invalid identifier
Regards.
One way is to repeat the subquery:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select bc.RBT_NAME
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
EDIT:
If you are getting an error that more than one row is returned, then you have to decide which value. Nothing in your code suggests that this might be an issue (hint: sample data and desired results always help a question).
The easiest solution is to use and aggregation function:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select max(bc.RBT_NAME)
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
But you might want to fix the rbt_cmp_base_code table so it doesn't have duplicates. From the table name, it sounds like there should be one row per code.

Oracle SQL update

I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;

Updating a table within a select statement

Is there any way to update a table within the select_expr part of a mysql select query. Here is an example of what I am trying to achieve:
SELECT id, name, (UPDATE tbl2 SET currname = tbl.name WHERE tbl2.id = tbl.id) FROM tbl;
This gives me an error in mysql, but I dont see why this shouldn't be possible as long as I am not changing tbl.
Edit:
I will clarify why I cant use an ordinary construct for this.
Here is the more complex example of the problem which I am working on:
SELECT id, (SELECT #var = col1 FROM tbl2), #var := #var+1,
(UPDATE tbl2 SET col1 = #var) FROM tbl WHERE ...
So I am basically in a situation where I am incrementing a variable during the select statement and want to reflect this change as I am selecting the rows as I am using the value of this variable during the execution. The example given here can probably be implemented with other means, but the real example, which I wont post here due to there being too much unnecessary code, needs this functionality.
If your goal is to update tbl2 every time you query tbl1, then the best way to do that is to create a stored procedure to do it and wrap it in a transaction, possibly changing isolation levels if atomicity is needed.
You can't nest updates in selects.
What results do you want? The results of the select, or of the update.
If you want to update based on the results of a query you can do it like this:
update table1 set value1 = x.value1 from (select value1, id from table2 where value1 = something) as x where id = x.id
START TRANSACTION;
-- Let's get the current value
SELECT value FROM counters WHERE id = 1 FOR UPDATE;
-- Increment the counter
UPDATE counters SET value = value + 1 WHERE id = 1;
COMMIT;