Solved

Select within update

Posted on 2006-11-06
11
215 Views
Last Modified: 2008-02-01
When am I missing in order to get the below working?

Update dbo.Name
Set CombineID = NULL
where
SELECT CombineID
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1
0
Comment
Question by:PeterErhard
  • 6
  • 5
11 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17887204
Hi PeterErhard,


Update dbo.Name
Set CombineID = NULL
where CombineID In (
SELECT CombineID
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1 )





Cheers!
0
 

Author Comment

by:PeterErhard
ID: 17887218
Thanks, but get this now:

Server: Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17887226
can i know the exact query you tried ?
0
 

Author Comment

by:PeterErhard
ID: 17887232
As above and below:

Update dbo.Name
Set CombineID = NULL
where CombineID In (
SELECT CombineID, count(CombineID) as test
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1 )
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17887233
remove that Count(CombineID) from the inner query

Update dbo.Name
Set CombineID = NULL
where CombineID In (
SELECT CombineID
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1 )
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:PeterErhard
ID: 17887238
I need that, it's critical to the update statement.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17887248
PeterErhard,
> I need that, it's critical to the update statement.
 Can I know what you are trying to do ?  Where this is used  ?
I assume that the 'Test' value is used to update some other columns

Update a
Set CombineID = NULL
--, SomeOtherField  = t.Test
FROM dbo.Name A
INNER JOIN (
SELECT CombineID, count(CombineID) as test
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1 )T
ON A.CombineID = T.CombineID
0
 

Author Comment

by:PeterErhard
ID: 17887255
There's a field within the Name table named CombineID. If there are less or equal to 1 CombineID the same, then I want to set those CombineIDs to NULL.

and Nope, that's the full query.
0
 

Author Comment

by:PeterErhard
ID: 17887277
As an example, you could have CombineIDs like this:

1
2
4
1
6
8
5
23

Then are running the update statement it should finish up like this:

<<NULL>>
2
4
<<NULL>>
6
8
5
23
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 17887284
Did you try the Queries i have given
For testing purpose run this

BEGIN TRAN

SELECT CombineID, Count(*) as [Count]
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1

Update dbo.Name
Set CombineID = NULL
where CombineID In (
SELECT CombineID
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1 )

SELECT CombineID, Count(*) as [Count]
from dbo.Name
GROUP By CombineID
Having count(CombineID)<=1

ROLLBACK TRAN
0
 

Author Comment

by:PeterErhard
ID: 17887295
Thanks, worked perfectly :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now