Solved

Select within update

Posted on 2006-11-06
11
218 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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
 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

831 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