Avatar of csePixelated
csePixelated
Flag for United States of America asked on

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

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
awking00

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_');
csePixelated

ASKER
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 's'.
csePixelated

ASKER
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'.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
csePixelated

ASKER
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'.
awking00

What are the actual case sensitive names of your tables
csePixelated

ASKER
[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'.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
csePixelated

ASKER
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'.
SOLUTION
awking00

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
csePixelated

ASKER
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'.
csePixelated

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
csePixelated

ASKER
yea it changed every IsRecurring to 'false'
ASKER CERTIFIED SOLUTION
Scott Pletcher

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
csePixelated

ASKER
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 Pletcher

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.