1

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')
2
  • 1
    shouldn't you select just a single column instead of everything (*) Commented May 8, 2015 at 12:45
  • 1
    Can you have multiple ID's with the same code? In that case it should be IN (Select...) if not just do WHERE code = 'SOUND_WRONG_GARMENT' Commented May 8, 2015 at 12:48

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

And if you ever do need an if statement for something like this, I find that using an exists statement often cleaner than comparing the result of a select statement to a value.
2

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.

Comments

2

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.

4 Comments

Except checking for the value is completely superfluous; if you run the update statement, it will either update or it won't; no need to check for the record's existence before updating it. Additionally, it should be WHERE ID IN, in case the subquery returns more than one result.
@LittleBobbyTables, you didn't read my answer properly did you?
well, when I commented, your last two paragraphs weren't there, and your SQL statement still had = instead of IN. Since you keep editing your answer, no need to be snippy about it.
@LittleBobbyTables, no snippyness here. I did pre-text my answer with acknowledging that a simple UPDATE is the correct solution though. I was simply pointing out why his code wasn't playing out the expected logical flow, there's more to this question than the way he should be doing it, he can learn about IF at the same time, resulting in two lessons in one :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.