SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Check each cell in one table and update another table


Check each cell in one table and update another table

Author
Message
adonetok
adonetok
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 789
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
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3687 Visits: 5177
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/
adonetok
adonetok
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 789
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25940 Visits: 17519
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 Modens 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)
adonetok
adonetok
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 789
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?
HPS_PRS
HPS_PRS
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 10
Then in that case you need to provide the complete problem condition in your question.
Its never be good to provide half of detail.
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3687 Visits: 5177
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/
adonetok
adonetok
SSChasing Mays
SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)SSChasing Mays (643 reputation)

Group: General Forum Members
Points: 643 Visits: 789
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25940 Visits: 17519
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 Modens 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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25940 Visits: 17519
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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search