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 |
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 11:55:08
|
Okay, I can't see the forest through the trees. I am trying to update 1 table with data from another based on an id in another. When I use this simple select statement all is well, but when I do an update it only seems to pull one id only.If I just use the following statement to view, it works.SELECT table2.[trans_num], table2.[ref_num], table3.[po_num]from table2, table3 WHERE table2.[ref_type] = 'P' and table2.[ref_num] = table3.[po_num]UPDATE table1SET table1.[TxnNumGUID] = table2.[trans_num]FROM table2, table3 WHERE table2.[ref_type] = 'P' and table2.[ref_num] = table3.[po_num]What am I doing wrong??? |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-08-07 : 11:59:44
|
You are not telling it how to update table1. You need to sepecify how the data you are updating with is related to the corresponding row in table1.UPDATE aSET a.[TxnNumGUID] = b.[trans_num]fromtable1 aInner JoinTable2 bon a.Linkingfield = b.LinkingfieldInner JoinTable 3 con b.Ref_Num = c.po_NumWHERE table2.[ref_type] = 'P' |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 14:53:30
|
| The thing is that table1 is accepting data from table2, but table2.ref_num needs to equal table3.po_num to update the correct value for table1.transNumGUID, which is found in table2. So, your inner join table2 on a.linkfield = b.linkfield would not work. I just can't seem to get my head wrapped around this one. Thanks in advance |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-07 : 14:57:15
|
quote: Originally posted by theladycoder The thing is that table1 is accepting data from table2, but table2.ref_num needs to equal table3.po_num to update the correct value for table1.transNumGUID, which is found in table2. So, your inner join table2 on a.linkfield = b.linkfield would not work. I just can't seem to get my head wrapped around this one. Thanks in advance
but whats the relationship between table1 and others? on what basis you decide which value is to be updated for which record of table1? |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-08-07 : 14:59:56
|
| what I was trying to illustrate is how do you know what row in table1 to update?If you have 30 records in table 1, how do you know where to apply the trans num? Without table1 having some kind of referance to one of the other tables (i.e. a ID field or something), you are just blindly applying the trans_num in table b.There needs be some logic.You are not providing enough information for someone to solve this. Please post your table structure for tables 1 2 and 3, some sample data (Just a couple rows), and your desired outcome. |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 15:39:53
|
| Here is my code:truncate table MQG_FIFO_Processing_HTS_Values_Export--Insert data from grn_line for IP--HTSIndex and HTSNum are purposely being fed twice to IPINSERT INTO MQG_FIFO_Processing_HTS_Values_Export (HTSNum, StatusCode, ManufacturerID, HTSIndex) SELECT [Uf_GRNLines_HTS] AS HTSNum, [Uf_ZoneDesignation] AS StatusCode, [Uf_ManufacturerID] AS ManufacturerID, [Uf_GRNLines_HTS] AS HTSIndexFROM grn_line --Update certain fields for IP that are not stored in our ERP SystemUpdate MQG_FIFO_Processing_HTS_Values_Export SET PartnerID = 'xxxxxx' WHERE PartnerID is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET EffDate =REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(varchar(20), GETDATE(), 126),' ','T'),'-',''),':',''),'.','') WHERE EffDate is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET SpiCode1Source = 'I' WHERE SpiCode1Source is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET SpiCode2Source = 'I' WHERE SpiCode2Source is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET RelationshipFlagSource = 'I' WHERE RelationshipFlagSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET HTSDescSource = 'I' WHERE HTSDescSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET HTSNum2Source = 'I' WHERE HTSNum2Source is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET Value2 = '0' WHERE Value2 is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET CurrencyCode = 'USD' WHERE CurrencyCode is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AltValue = '0' WHERE AltValue is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AltValue2 = '0' WHERE AltValue2 is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AltCurrencyCodeSource = 'I' WHERE AltCurrencyCodeSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AdValoremRateSource = 'I' WHERE AdValoremRateSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET SpecificRateSource = 'I' WHERE SpecificRateSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET HtsUomConvFactor = '0' WHERE HtsUomConvFactor is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET HtsUomConvFactorSource = 'I' WHERE HtsUomConvFactorSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AddlHtsUomConvFactor = '0' WHERE AddlHtsUomConvFactor is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AddlHtsUomConvFactorSource = 'I' WHERE AddlHtsUomConvFactorSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET RptQtyUomSource = 'I' WHERE RptQtyUomSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AddlRptQtyUomSource = 'I' WHERE AddlRptQtyUomSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AddlSpecificRateSource = 'I' WHERE AddlSpecificRateSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AdDutyRate = '0' WHERE AdDutyRate is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AdDutyRateSource = 'I' WHERE AdDutyRateSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET AdCaseNumberSource = 'I' WHERE AdCaseNumberSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET CVDutyRate = '0' WHERE CVDutyRate is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET CVDutyRateSource = 'I' WHERE CVDutyRateSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET CVCaseNumberSource = 'I' WHERE CVCaseNumberSource is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET RCO21Source = 'I' WHERE RCO21Source is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET RCO22Source = 'I' WHERE RCO22Source is nullUpdate MQG_FIFO_Processing_HTS_Values_Export SET RCO23Source = 'I' WHERE RCO23Source is null--Need to pull in material cost for IP using IDO po_bln based on field po_numUPDATE MQG_FIFO_Processing_HTS_Values_ExportSET MQG_FIFO_Processing_HTS_Values_Export.[Value] = po_bln.[unit_mat_cost_conv]FROM po_bln, grn_lineWHERE grn_line.[po_num] = po_bln.[po_num]--Need to pull in Country of Origin for IPs CountryOfOrigin from IDO vendaddr based on field ven_numUPDATE MQG_FIFO_Processing_HTS_Values_ExportSET MQG_FIFO_Processing_HTS_Values_Export.[CountryOfOrigin] = vendaddr.[country]FROM vendaddr, grn_lineWHERE vendaddr.[vend_num] = grn_line.[vend_num]--Need to pull in trans_date and trans_num for IPs fields TxnNumGuid and TxnDate from IDO matltran based on field po_numUPDATE MQG_FIFO_Processing_HTS_Values_ExportSET MQG_FIFO_Processing_HTS_Values_Export.[TxnNumGUID] = matltran.[trans_num]FROM matltran, grn_lineWHERE matltran.[ref_type] = 'P' and matltran.[ref_num] = grn_line.[po_num] |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-08-07 : 16:14:19
|
Wow... you are really not making things easy. Please read the original responses. You still are not answering how MQG_FIFO_Processing_HTS_Values_Export relates to any of the other tables. For example does it share a common field with ANY other table in the following list (po_bln, grn_line,vendaddr,matltran) If it does please list each field that is shared.regarding the 8000 update statements here's a better way (Assuming setting a default value on your table is not a option).Update aset a.PartnerID = Coalesce(a.PartnerID , 'xxxxxx'),a.EffDate =Coalesce(a.EffDate ,REPLACE(REPLACE(REPLACE(REPLACE(CONVERT(varchar(20), GETDATE(), 126),' ','T'),'-',''),':',''),'.','') ),a.SpiCode1Source = Coalesce(a.SpiCode1Source , 'I') ,a.SpiCode2Source =Coalesce(a.SpiCode2Source , 'I') ,a.RelationshipFlagSource = Coalesce(a.RelationshipFlagSource , 'I') ,a.HTSDescSource = Coalesce(a.HTSDescSource , 'I') ,a.HTSNum2Source = Coalesce(a.HTSNum2Source , 'I') ,a.Value2 = Coalesce(a.Value2 , '0' ),a.CurrencyCode = Coalesce(a.CurrencyCode , 'USD' ),a.AltValue = Coalesce(a.AltValue , '0' ),a.AltValue2 =Coalesce(a.AltValue2 , '0' ),a.AltCurrencyCodeSource = Coalesce(a.AltCurrencyCodeSource , 'I'),a.AdValoremRateSource = Coalesce(a.AdValoremRateSource , 'I') ,a.SpecificRateSource =Coalesce(a.SpecificRateSource , 'I'),a.HtsUomConvFactor =Coalesce(a.HtsUomConvFactor , '0'),a.HtsUomConvFactorSource =Coalesce(a.HtsUomConvFactorSource , 'I') ,a.Add1HtsUomConvFactor =Coalesce(a.AddlHtsUomConvFactor , '0' ),a.Add1HTSUomConvFactorSource =Coalesce(a.AddlHtsUomConvFactorSource , 'I'),a.RptQtyUomSource = Coalesce(a.RptQtyUomSource , 'I') ,a.Add1RptQtyUomSource = Coalesce(a.AddlRptQtyUomSource , 'I') ,a.Add1SpecificRateSource = Coalesce(a.AddlSpecificRateSource , 'I') ,a.AdDutyRate = Coalesce(a.AdDutyRate , '0' ),a.AdDutyRateSource = Coalesce(a.AdDutyRateSource , 'I') ,a.AdCaseNum = Coalesce(a.AdCaseNum berSource , 'I') ,a.CVDutyRate = Coalesce(a.CVDutyRate , '0' ),a.CVDutyRateSource =Coalesce(a.CVDutyRateSource , 'I'),a.CVCaseNumberSource = Coalesce(a.CVCaseNumberSource , 'I') ,a.RC021Source = Coalesce(a.RCO21Source , 'I') ,a.Rco22Source = Coalesce(a.RCO22Source , 'I') ,a.RC023Source =Coalesce(a.RCO23Source , 'I') fromMQG_FIFO_Processing_HTS_Values_Export a |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 16:36:49
|
| The only common denominator are as follows:grn_line.po_num = matltran.ref_num where matl_tran.ref_type = P (references po number)grn_line.[po_num] = po_bln.[po_num](references po number)These are the 2 main tables (with grn_line being primary over matltran) as the most data that is needed resides here. vendaddr.[vend_num] = grn_line.[vend_num] (this is the vendor number)Table vendaddr has no po number stored and this is to strictly extract the country of the vendorIs this what you are asking? |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 16:38:14
|
| So I am wondering if I need to pull the trans_date and trans_num during the initial insert statement using an inner join perhaps?INSERT INTO MQG_FIFO_Processing_HTS_Values_Export (HTSNum, StatusCode, ManufacturerID, HTSIndex) SELECT [Uf_GRNLines_HTS] AS HTSNum, [Uf_ZoneDesignation] AS StatusCode, [Uf_ManufacturerID] AS ManufacturerID, [Uf_GRNLines_HTS] AS HTSIndexFROM grn_line |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-08-07 : 16:39:51
|
| No,Is there any field in MQG_FIFO_Processing_HTS_Values_Export that relates to any of those tables? |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-07 : 16:40:47
|
| The lightbulb is slowly starting to burn, I think, LOL. The more I look at this I wonder if I willbe able to correctly pull the data. Right now, as it stands there are 320 records and when I do the update it only pulls the first record information for trans_date and trans_num and populates all 320 records with 33 as the trans_num(TxnNumGUID) and the same date for all trans_date(TxnDate). |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2008-08-07 : 16:55:43
|
| Yes, you got it.. You need a way to referance the records to the other tables. You will need to change how you are creating that table. |
 |
|
|
theladycoder
Starting Member
16 Posts |
Posted - 2008-08-08 : 08:30:16
|
| Okay, I have finally seen the forest through the trees! Talking it out here helped turn the light bulb on to full power. I am the only programmer here and don't have the fresh pair of eyes when I need it. I feel the need to hit myself over the head for this issue, which turned out to be a very simple mistake on my part, LOL. Anyway, I captured the identifier during the insert, as I should have in the first place, and the rest fell into place. I appreciate the responses! |
 |
|
|
|
|
|
|
|