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
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'.
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'.
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'.
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_');
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
set IsRecurring = False
where exists
(select 1 from [dbo_subscriber Data] d
where d.AccountID = s.AccountID
and d.AcctLineCode like 'HR_');