May 16, 2013 at 6:59 am
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
IDISCOLOR
111YES
222YES
333YES
444NO
May 16, 2013 at 7:47 am
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.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 16, 2013 at 7:54 am
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
May 16, 2013 at 7:57 am
adonetok (5/16/2013)
This is the original table named as COLORCOLOR
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 16, 2013 at 8:03 am
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?
May 16, 2013 at 8:07 am
Then in that case you need to provide the complete problem condition in your question.
Its never be good to provide half of detail.
May 16, 2013 at 8:10 am
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?
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 16, 2013 at 8:16 am
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.
May 16, 2013 at 8:19 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 16, 2013 at 8:21 am
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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 16, 2013 at 8:22 am
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.
What is this table that you are talking about?
Is it some database table or some .NET structure?
The solution provided to you will work only for database tables.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
May 17, 2013 at 6:08 am
Below is a query for your requirment,
UPDATE ISCOLOR
SET ISCOLOR = CASE when COLOR.Blue IS NOT NULL OR COLOR.Red IS NOT NULL or COLOR.Yellow IS NOT NULL then 'Yes' else 'NO' END
FROM ISCOLOR INNER JOIN COLOR ON ISCOLOR.ID = COLOR.ID
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply