Author |
Topic |
gilbe73
Starting Member
5 Posts |
Posted - 2009-04-07 : 16:28:25
|
Hello,here is my short script:Create Table #tmp(x_val int,y_val char(30),...)Insert Into #tmpexec stored_procedurenow my #tmp is filled with 214 rows,87 of them have the same x_val value(when I use distinct x_valon #tmp I get 87 rows)my next step is to try to update another table for each row(214 times)I have in the temporary table:Update x_tableset field_x =Case When m1.x_val is null Then m2.x_val Else m1.x_val + ',' + m2.x_val EndFrom x_table m1Inner join #tmp m2where m1.something = m2.something My problem is that the update action seems to run only 87 times and not 214 times as the number of rows in the #tmp table.The m1.x_val should have a value like this in the end: 25,27,30...Thanks for your help, Gil |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
gilbe73
Starting Member
5 Posts |
Posted - 2009-04-07 : 17:22:20
|
Can I force it to run 214 times as the number of rows in #tmp? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
gilbe73
Starting Member
5 Posts |
Posted - 2009-04-08 : 06:04:47
|
no, there are 214 rows that match, but 87 of them have the same ID. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
gilbe73
Starting Member
5 Posts |
Posted - 2009-04-08 : 12:12:08
|
#tmp table has rows like this:id val_1 val_21 25 261 42 312 55 253 60 30Second table has this row:id val_1 val_21 42 312 55 253 60 30I want the update action run 4 times as the number of rows in the #tmpso row with ID 1 in the second table will be update twice.hope it's more clearly now |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-08 : 13:58:21
|
Clear as mud. Follow the first link in my signature, and then restate your question with the information that is asked for.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
|
|
gilbe73
Starting Member
5 Posts |
Posted - 2009-04-14 : 17:22:49
|
I will try to explain myself better.this is my tmp table:ID val_1 val_21 somthing 261 somthing 312 somthing 253 somthing 301 somthing 79Second table has this row:ID val_1 val_21 somthing null2 somthing null3 somthing nullafter this update:Update _tableset val_2 =Case When m1.val_2 is null Then m2.val_2Else m1.val_2 + ',' + m2.val_2 EndFrom _table m1Inner join #tmp m2where m1.ID= m2.IDThe second table _table should look like this afeter the update:ID val_1 val_21 somthing 26,31,792 somthing 253 somthing 30but what I get is:1 somthing 792 somthing 253 somthing 30row with ID = 1 is update only once with the last value. |
|
|
dsindo
Starting Member
45 Posts |
Posted - 2009-04-14 : 19:44:26
|
it did update ex. id=1 something 3 times that is why you have the value of the last matching record ID=1.you have distinct ID rows in #tmp table and multiple ID rows in the _table. i assume you have 1 Something 261 something 311 something 79on you _table.first 1 something val 2 was updated to 26,then 1 something val 2 was updated to 31,then 1 something val 2was updated to 79thats is why you have 79. the update query will not create the other records for you. |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-04-16 : 19:56:58
|
It's been a while since I've used SQL2000 so I don;t thinkyou can use the FOR XML concatenation method. I think you need to use a UDF in order to do this reasonably effecently. Something like:CREATE FUNCTION dbo.ConcatVal (@ID INT)RETURNS VARCHAR(8000)ASBEGIN DECLARE @Output VARCHAR(8000) SET @Output = '' SELECT @Output = CASE @Output WHEN '' THEN CAST(val2 AS VARCHAR(10)) ELSE @Output + ',' + CAST(val2 AS VARCHAR(10)) END FROM MyTable WHERE ID = @ID RETURN @OutputEND Then you can do something like this to update each row in your other table:UPDATE #Table2SET val2 = dbo.ConcatVal(id) |
|
|
|