sql query refrence data on a alternate table to change specific values to false

csePixelated
csePixelated used Ask the Experts™
on
i have 2 tables, the primary table [dbo.subscriber data] has several columns. the columns i need to reference on the primary table are 'AccountID' and 'AcctLineCode'
the other table is [dbo.subscriber Status], it also has the 'AccountID' column as well as the column 'IsRecurring'
i need to change all instances of 'IsRecurring' on [dbo.subscriber Status] to False if it has an 'AccountID' that matches the 'AccountID' from [dbo.subscriber data] that has an 'AcctLineCode' of 'HR' or 'HRS'

I assume i need to use inner join, i have no idea the syntax
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
awking00Information Technology Specialist

Commented:
update [dbo.subscriber Status] s
set IsRecurring = False
where exists
(select 1 from [dbo_subscriber Data] d
 where d.AccountID = s.AccountID
 and d.AcctLineCode like 'HR_');

Author

Commented:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.

Author

Commented:
removed the s from line one and the d from line 4, checked sql passed, ran it and got...
Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.subscriber Status'.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
update [dbo.subscriber Status]
set IsRecurring = False
where exists
(select 1 from [dbo_subscriber Data] d
 where d.AccountID = s.AccountID
 and d.AcctLineCode like 'HR_');

ran the above
still got
Msg 208, Level 16, State 1, Line 1
Invalid object name 'dbo.subscriber Status'.
awking00Information Technology Specialist

Commented:
What are the actual case sensitive names of your tables

Author

Commented:
[dbo.Subscriber Data] & [dbo.Subscriber Status] are the actual case sensitive names of the tables.
i have now tryed with these corected...

update [dbo.Subscriber Status]s
set IsRecurring = False
where exists
(select 1 from [dbo_Subscriber Data] d
 where d.AccountID = s.AccountID
 and d.AcctLineCode like 'HR_');

still getting...
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.

Author

Commented:
noticed a lack of space...
update [dbo.Subscriber Status] s
set IsRecurring = False
where exists
(select 1 from [dbo_Subscriber Data] d
 where d.AccountID = s.AccountID
 and d.AcctLineCode like 'HR_');
still
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.
awking00Information Technology Specialist
Commented:
Might need AS keyword?
update [dbo.Subscriber Status] AS  s
 set IsRecurring = False
 where exists
 (select 1 from [dbo_Subscriber Data] AS d
  where d.AccountID = s.AccountID
  and d.AcctLineCode like 'HR_');

Author

Commented:
update [dbo.Subscriber Status] AS "s"
 set IsRecurring = False
 where exists
 (select 1 from [dbo_Subscriber Data] AS "d"
  where d.AccountID = s.AccountID
  and d.AcctLineCode like 'HR_');

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.

update [dbo.Subscriber Status] AS s
 set IsRecurring = False
 where exists
 (select 1 from [dbo_Subscriber Data] AS d
  where d.AccountID = s.AccountID
  and d.AcctLineCode like 'HR_');

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'AS'.

Author

Commented:
i was curious as to weather i needed the In clause, did some playing around

update [Subscriber Status]
 set IsRecurring = 'False'
 where exists
 (select 1 from [Subscriber Data] AS d
  where d.AccountID = AccountID
  and d.AcctLineCode like 'HR_');

this ran however too many rows were affected

Author

Commented:
yea it changed every IsRecurring to 'false'
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
UPDATE ss
SET IsRecurring = 0 /*'F'*/
FROM [dbo.subscriber Status] ss
INNER JOIN (
    SELECT AccountID
    FROM [dbo.subscriber data]
    WHERE AcctLineCode IN ('HR', 'HRS')
    GROUP BY AccountID
) AS sd ON sd.AccountID = ss.AccountID

Author

Commented:
aparently it did not like the "dbo."

UPDATE ss
SET IsRecurring = 0 /*'F'*/
FROM [Subscriber Status] ss
INNER JOIN (
    SELECT AccountID
    FROM [Subscriber Data]
    WHERE AcctLineCode IN ('HR', 'HRS')
    GROUP BY AccountID
) AS sd ON sd.AccountID = ss.AccountID

the above worked perfectly..,. TY Scott P and Awking00..!!
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
D'OH, sorry, the "dbo." should have been separate, before the brackets.

UPDATE ss
SET IsRecurring = 0
FROM dbo.[subscriber Status] ss
INNER JOIN (
    SELECT AccountID
    FROM dbo.[subscriber data]
    WHERE AcctLineCode IN ('HR', 'HRS')
    GROUP BY AccountID
) AS sd ON sd.AccountID = ss.AccountID

Btw, ALWAYS use an alias when UPDATEing a table using a JOIN.  Otherwise, it's too easy to UPDATE all rows and/or the wrong rows in the table being updated.  Like above:

UPDATE ss --<< alias name (not [subscriber Status] directly)
SET IsRecurring = 0
FROM dbo.[subscriber Status] ss --<< assign alias name for use in UPDATE

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial