Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
uchennavt
Starting Member
4 Posts |
Posted - 2009-11-17 : 13:23:52
|
| Hi All,I have two tables. Table 1 is called DATA, which has several columns, most important of them are DATA_ID and METRIC_ID. Table 2 is called PARAMETERS, which also has several columns, most important of them are DATA_ID, PARAMETER_ID, and Client_Platform. I want to traverse through every record in the DATA table, and compare it with its respective record in the PARAMETERS table, matching their DATA_IDs. Once I find a match, I would like to update the PARAMETER table's Client_Platform column. This task is ran until the end of the DATA table. Below is you will find my algorithm that should accomplish the above description, however, it fails to do that. Please let me know where I have made a mistake.ALTER PROCEDURE [dbo].[UPDATE_PLATFORM] -- Add the parameters for the stored procedure hereASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @data_id INT DECLARE @parameter_id INT DECLARE @new_client_name varchar(255) DECLARE @metric_id INT DECLARE datacursor CURSOR FOR SELECT DATA_ID,METRIC_ID FROM DATA DECLARE parametercursor CURSOR FOR SELECT DATA_ID FROM PARAMETERs OPEN datacursor OPEN parametercursor SET @new_client_name = '.a.' DECLARE @flag INT SET @flag = 0 FETCH NEXT FROM datacursor into @data_id, @metric_id while (@@FETCH_STATUS = 0) begin if(@metric_id >= 1 and @metric_id<=72) /*iometer*/ begin FETCH NEXT FROM parametercursor into @parameter_id while (@flag = 0) begin if (@parameter_id = @data_id) /*found the parameter */ begin /*UPDATE PARAMETERs SET Client_Platform = @new_client_name*/ SET @flag = 1 /*update done*/ end else begin SET @flag =0 FETCH NEXT FROM parametercursor into @parameter_id end end end SET @flag = 0 /*reset update*/ FETCH NEXT FROM datacursor into @data_id, @metric_id end -- Insert statements for procedure hereENDThanks, |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-11-17 : 14:51:55
|
I got kinda lost on the cursor stuff. Here is a sample that may or may not work:UPDATE PSET Client_Platform = ??FROM Data AS DINNER JOIN Parameters AS P ON D.Data_ID = P.Data_IDWHERE METRIC_ID >= 1 AND METRIC_ID <= 72 I'd suggest you post some sample data and expected output and we can help you better. Here is a link that describes how to do that:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
uchennavt
Starting Member
4 Posts |
Posted - 2009-11-17 : 21:41:44
|
| Hi Guys,Thanks for avail. to help me. I guess I will clearify what I said a little earlier.Table 1 has a column (DATA_ID) name and value that is exactly the same as a column (DATA_ID) in Table 2. I want to traverse through each record in table 1 and match its column value with the one on second table, and then do an update on another column on table 2. I guess you can think of this as hashing (onto table 2) onto table 2 then update on a member of the record.Please let me know how I can write my code .. Really stuck on thisThanks, |
 |
|
|
uchennavt
Starting Member
4 Posts |
Posted - 2009-11-17 : 21:54:07
|
| The following will give you an example of what I envision the code doing...Sample data would be :Table 1DATA_ID, METRIC_ID1,342,1003,664, 72Table 2DATA_ID, Client_Platform1, OS12, OS33, OS64, OS7Ouptut:*for every record in table 1, that is between 1 and 72, inclusive, the algorithm will find the matching data_id in table 2 and then update the client_platform to Linux. Here is the output sample:Table 2DATA_ID, Client_Platform1, Linux2, OS33, Linux4, Linuxhowever, my code, the one from the previous post, just sets all records' client_platform to LINUX, which is WRONG .. PLEASE HELP.Thanks, |
 |
|
|
xpandre
Posting Yak Master
212 Posts |
Posted - 2009-11-18 : 10:28:11
|
| isnt this as simple asupdate t2 set Client_Platform = 'LINUX' from t2 inner join t1 on t1.DATA_ID = t2.DATA_IDwhere t1.METRIC_ID >= 1 and t1.METRIC_ID <= 72or am I missing something here? |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-11-18 : 11:10:17
|
| Little point.You should have a os table rather than storing multiple 'Linux', 'OS2' whatever.No need for cursor here. as illustrated you can do this with simple joins.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
uchennavt
Starting Member
4 Posts |
Posted - 2009-11-18 : 14:06:22
|
| Thanks a lot guys! The simple SQL statement worked just fine. |
 |
|
|
|
|
|
|
|