troubleshooting Question

SQL set flag if any 1 row has certain data

Avatar of Andy Green
Andy GreenFlag for United Kingdom of Great Britain and Northern Ireland asked on
SQL
6 Comments1 Solution68 ViewsLast Modified:
Hi

I have an sql query that returns several rows of data. I need to set a flag on all rows if any one row has certain data.

Example

Declare  @myTable TABLE(dID int, Data varchar(25),  Flag bit)

  Insert into @myTable Select 1,'1', 0
  Insert into @myTable Select 2,'2', 0
  Insert into @myTable Select 3,'3', 0
  Insert into @myTable Select 4,'4', 0
  Insert into @myTable Select 5,'5', 0
  Insert into @myTable Select 6,'6', 0
  Insert into @myTable Select 7,'7', 0
  Insert into @myTable Select 8,'8', 0
  Insert into @myTable Select 9,'9', 0
  Insert into @myTable Select 10,'10', 0

Select * from @myTable

If any row has the value of 5 in the data column, that the flag for all rows is to be set to 1.

I'm using SQL server 2008

Andy
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros