0

I'm trying to run a query akin to the following. I'm basically trying to change the format of the updated text conditionally. I need some help as I've tried this a few different ways without success. Thanks in advance.

UPDATE bL SET 
bL.[QUANTITY] = bL.[QUANTITY] + 2,
bL.[SERIAL_NUMBERS] = 
    (
      CASE WHEN bL.[SERIAL_NUMBERS] IS NOT NULL 
      THEN ISNULL(bL.[SERIAL_NUMBERS], '') + 'EOSB7U310007; EOSB7U310022' 
      ELSE bL.[SERIAL_NUMBERS] + '; EOSB7U310007; EOSB7U310022'
    ) 
 FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES_LINES] AS bL 
 LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES] AS bO 
 ON bL.[AOF_BOXES_ID] = bO.[ID] 
 WHERE bO.[SELECTED] = 'True'
addohm
  • 2,248
  • 3
  • 14
  • 40

2 Answers2

2

So, putting all the comments I found correct together in a single answer, your code should probably look like this:

UPDATE bL SET 
bL.[QUANTITY] = bL.[QUANTITY] + 2,
bL.[SERIAL_NUMBERS] = 
    (
      CASE WHEN bL.[SERIAL_NUMBERS] IS NULL 
      THEN 'EOSB7U310007; EOSB7U310022' 
      ELSE bL.[SERIAL_NUMBERS] + '; EOSB7U310007; EOSB7U310022'
      END
    ) 
 FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES_LINES] AS bL 
 LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES] AS bO 
 ON bL.[AOF_BOXES_ID] = bO.[ID] 
 AND bO.[SELECTED] = 'True'

However it can be shorten - like this:

UPDATE bL SET 
bL.[QUANTITY] = bL.[QUANTITY] + 2,
bL.[SERIAL_NUMBERS] = ISNULL(bL.[SERIAL_NUMBERS] + '; ', '') + 'EOSB7U310007; EOSB7U310022'
 FROM [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES_LINES] AS bL 
 LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES] AS bO 
 ON bL.[AOF_BOXES_ID] = bO.[ID] 
 AND bO.[SELECTED] = 'True'

This is because it the value of bL.[SERIAL_NUMBERS] is null then the concatenation of it with ; will also be null.

Notes:

  • I've moved bO.[SELECTED] = 'True' from the WHERE clause to the ON clause. This is because when using a left join, all the conditions on the right table should be on the ON clause, otherwise the join is turned into an inner join.

  • Seems like you are using the bL.[SERIAL_NUMBERS] column to store multiple values. This is a big mistake in 99.9999% of the cases.
    Read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why.

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Noted. The customer designed their system this way, I am just adapting and integrating with them :) – addohm Apr 18 '17 at 19:26
1

You don't need the SELECT and you have to END your CASE statement.

UPDATE  bL
SET     bL.[QUANTITY] = bL.[QUANTITY] + 2
      , bL.[SERIAL_NUMBERS] = ( CASE WHEN bL.[SERIAL_NUMBERS] IS NOT NULL 
                                     THEN ISNULL(bL.[SERIAL_NUMBERS] , '') + 'EOSB7U310007; EOSB7U310022'
                                     ELSE bL.[SERIAL_NUMBERS] + '; EOSB7U310007; EOSB7U310022'
                                END )
FROM    [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES_LINES] AS bL
LEFT JOIN [ROBOTICS_OPTICS_MECHUAT].[dbo].[AOF_BOXES] AS bO ON bL.[AOF_BOXES_ID] = bO.[ID]
WHERE   bO.[SELECTED] = 'True';
Dave.Gugg
  • 6,561
  • 3
  • 24
  • 43