0

Here is my base SQL query in SQL Server 2008:

SELECT userID
FROM coreUsers 
INNER JOIN pluginSales ON coreUser.userID = pluginSales.userID
WHERE (coreUser.CurrentYear = '2014') 
  AND (pluginSales.PaidShipping = 'Yes') 
   OR (coreUser.IsContract = 'Yes')

Just tested on 12/19/2014 again, this returns 61 results.

I need to be able to update the pluginSales table column ShipppingDate for all the records that result from the above statement.

coreUser.userID is my unique key that auto increments.

pluginSales has a column ShippingDate that is just a standard varchar that I need update to '12/16/2014' if the record was set to 2014 (coreUser.CurrentYear) and they PaidShipping (pluginSales.PaidShipping) OR if they are a contracted customer (coreUser.IsContract)

EDIT #1

I tried the following...

UPDATE       pluginSales
SET                ShippingDate = ''
FROM            coreUsers AS cu INNER JOIN
                         pluginSales AS pis ON cu.userID = pis.userID CROSS JOIN
                         pluginSales
WHERE (cu.CurrentYear = '2014') AND (pis.PaidShipping = 'Yes' OR pis.IsContract = 'Yes')

Just tested again on 12/19/2014 this modified 554 records (my whole pluginSales database)

This affected ALLLLLL of my rows and not just the specific ones. Please advise.

3
  • 2
    Bad habits to kick : choosing the wrong data type - you should always use the most appropriate data type. WHY are you storing a DATE (ShippingDate) as a varchar - use a DATE or DATETIME2(n) instead! That's what these types are there for!
    – marc_s
    Dec 17, 2014 at 6:01
  • The reason why this is like that is because i don't have a client side validation for a specific "date". I know its a bad idea, but for the current system I have running its what I need it to do and is working as intended.
    – eqiz
    Dec 19, 2014 at 20:28
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Dec 20, 2014 at 0:26

2 Answers 2

3

You can do this with an update and join:

UPDATE cu
    SET ShippingDate '12/16/2014'
FROM coreUsers cu INNER JOIN
     pluginSales pis
     ON cu.userID = pis.userID
WHERE (cu.CurrentYear = '2014' AND (pis.PaidShipping = 'Yes') OR (pis.IsContract = 'Yes');

You may actually want this where clause:

WHERE (cu.CurrentYear = '2014') AND (pis.PaidShipping = 'Yes' OR pis.IsContract = 'Yes');

Note that you shouldn't be storing dates in strings. Instead, you should be using the native date or datetime types. If you have to store dates as strings, you should use YYYY-MM-DD format, so operations such as order by and comparisons will work on the values.

4
  • This is the EXACT same as amit_g.. it doesn't work it freaking effected my whole database and NOT just the records it was suppose to. The original select statement I gave above only affects 61 returns while yours just affected my whole database
    – eqiz
    Dec 19, 2014 at 21:22
  • @eqiz . . . I would check the alternative where clause. Dec 19, 2014 at 21:23
  • 1
    @eqiz . . . Then the version with your original where clause would appear to have the correct logic. Dec 19, 2014 at 21:28
  • MS SQL Server 2008 didn't like your statement so it reformated it to what I have above in my Edit #1. It has to be the statement itself thats causing the issue. I didn't change anything all i did was copy 'n paste
    – eqiz
    Dec 19, 2014 at 21:44
1

First run the select

SELECT *
FROM pluginSales PS
INNER JOIN coreUsers CU ON CU.userID = PS.userID
WHERE (CU.CurrentYear = '2014')
    AND (PS.PaidShipping = 'Yes')
    OR (CU.IsContract = 'Yes')

then change only first line to UPDATE ...

UPDATE pluginSales SET ShippingDate = '12/16/2014'
FROM pluginSales PS
INNER JOIN coreUsers CU ON CU.userID = PS.userID
WHERE (CU.CurrentYear = '2014')
    AND (PS.PaidShipping = 'Yes')
    OR (CU.IsContract = 'Yes')

Your Where clause has an OR. You may want to explicitly group using parenthesis ().

3
  • This doesn't work it actually freaking effected 552 rows when the original SELECT statement only returns 61 items. This obviously doesn't work
    – eqiz
    Dec 19, 2014 at 21:21
  • @eqiz, I added a select which is syntactically identical to the one you have but slightly modified to ensure the comparability to update. If your where clause is correct, both those select and update should effect identical number of rows.
    – amit_g
    Dec 19, 2014 at 23:08
  • Your fix worked perfectly thanks for that change/update!
    – eqiz
    Dec 27, 2014 at 21:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.