Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Check each cell in one table and update another table Expand / Collapse
Author
Message
Posted Thursday, May 16, 2013 6:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 1:26 PM
Points: 312, Visits: 590
There are two tables as below: COLOR and ISCOLOR.
I need a loop to check cell by cell in table COLOR.
If ID has any one color, update table ISCOLOR as "YES" otherwise is "NO"

COLOR

ID----BLUE---RED---YELLOW
111---X--------------------
222-------------X---------X
333---X--------------------
444-------------------------

ISCOLOR

ID ISCOLOR
111 YES
222 YES
333 YES
444 NO
Post #1453494
Posted Thursday, May 16, 2013 7:47 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:34 AM
Points: 2,666, Visits: 4,736
Your requirements are not very clear
Can you provide the DDL of the tables involved, data and the expected results in a ready to use format

Please check the link in my signature if you are not sure on how to do this.



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1453543
Posted Thursday, May 16, 2013 7:54 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 1:26 PM
Points: 312, Visits: 590
This is the original table named as COLOR

COLOR

ID----BLUE---RED---YELLOW
111---X--------------------
222-------------X--------X
333---X--------------------
444-------------------------

I need a loop to check above table cell by cell and then update another table.
The final result is table named as ISCOLOR
For example, after looping, ISCOLOR table should be updated as below.

ID---ISCOLOR
111--YES
222--YES
333--YES
444--NO
Post #1453549
Posted Thursday, May 16, 2013 7:57 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
adonetok (5/16/2013)
This is the original table named as COLOR

COLOR

ID----BLUE---RED---YELLOW
111---X--------------------
222----------X------X------
333---X--------------------
444-------------------------

I need a loop to check above table cell by cell and then update another table.
The final result is table named as ISCOLOR
For example, after looping, ISCOLOR table should be updated as below.

ID---ISCOLOR
111--YES
222--YES
333--YES
444--NO


You know simple copying and pasting a vague description does not actually count as posting details.

You by no means need a loop for this unless you really like to create really slow processes when a much faster and simpler process would do the same thing.

Based on your extremely vague description I think you just need something like this:

update IsColor
set IsColor = case when c.Blue IS NOT NULL OR c.Red IS NOT NULL or c.Yellow IS NOT NULL then 'Yes' else 'NO' end
from IsColor i
join Color c on c.ID = i.ID



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1453553
Posted Thursday, May 16, 2013 8:03 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 1:26 PM
Points: 312, Visits: 590
In fact, in real table there are about 20 colums and 200 ID need to check cell by cell.
I do not think that without loop can do it.
Can someone help me?
Post #1453560
Posted Thursday, May 16, 2013 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, August 6, 2014 6:44 AM
Points: 2, Visits: 9
Then in that case you need to provide the complete problem condition in your question.
Its never be good to provide half of detail.
Post #1453565
Posted Thursday, May 16, 2013 8:10 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, October 9, 2014 7:34 AM
Points: 2,666, Visits: 4,736
adonetok (5/16/2013)
In fact, in real table there are about 20 colums and 200 ID need to check cell by cell.
I do not think that without loop can do it.
Can someone help me?


Does the solution given by Sean earlier work for your 3 columns as required?
If Yes, whats the problem in implementing it for 20 columns?



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1453569
Posted Thursday, May 16, 2013 8:16 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 1:26 PM
Points: 312, Visits: 590
I do not know how to post real table but I did the same case using .net in application (create a dataset and then looping in dataset).
Now, I want to use store procedure to do it but I do not know how to do in SQL.
Post #1453574
Posted Thursday, May 16, 2013 8:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
adonetok (5/16/2013)
In fact, in real table there are about 20 colums and 200 ID need to check cell by cell.
I do not think that without loop can do it.
Can someone help me?


20 columns for this is not that big of a deal. You can code that solution in about 5 - 10 minutes. A loop should be your absolute LAST resort. If you do this in a loop you still have to evaluate all the columns so you aren't saving development time or keystrokes. It is more complicated and far slower.

To be honest from your description it sounds like you could stand to do some normalization on your tables. Are all 20 columns mutually exclusive like you posted? in other words can only 1 of those 20 columns be checked? If so, then you really need to look at normalizing that. You are using 20 columns to hold a single piece of information.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1453575
Posted Thursday, May 16, 2013 8:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
adonetok (5/16/2013)
I do not know how to post real table but I did the same case using .net in application (create a dataset and then looping in dataset).
Now, I want to use store procedure to do it but I do not know how to do in SQL.


You need to make a mental shift in how you think about data. Don't think about what you want to do to a single cell and instead think about what you want to do to a column.

In .NET you would have to loop through this. In SQL you can update the entire dataset in a single step.

I basically showed you exactly how to do this in SQL.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1453576
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse