| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2007-01-24 : 08:21:45
|
| Snehalata writes "i have a #table as followsCREATE TABLE #ReportingNumbers ( HeadID INT, EntityID INT, Type VARCHAR(10), Value DECIMAL(30,12) )a master table as followsCREATE TABLE HeadIDFieldMapping( HeadID INT, FieldName VARCHAR(100))and one transaction table (vData) which have columns as all rows of table HeadIDFieldMapping i.e.table vData is having FieldName which are rows(value of fieldName) in table HeadIDFieldMapping . table #ReportingNumbers has field HeadID and table HeadIDFieldMapping has field HeadID.now i want to update field Value of #ReportingNumbers Table with the value of fields of vData table using dynamic query. how can be this done?" |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-24 : 09:24:04
|
please post the structure for vData and sample data for all 3 tables with the expected result KH |
 |
|
|
snehsoni
Starting Member
3 Posts |
Posted - 2007-01-25 : 02:20:39
|
| table structure for #ReportingNumbers CREATE TABLE #ReportingNumbers ( HeadID INT, EntityID INT, Type VARCHAR(10), Value DECIMAL(30,12) )table structure for HeadIDFieldMappingCREATE TABLE HeadIDFieldMapping(HeadID INT,FieldName VARCHAR(100))and table structure for vData as followsCREATE TABLE vData( FundID INT, InvestorID INT, AccountingPeriodDate DATETIME, BeginUnits DECIMAL(30,12), BeginAdditionUnits DECIMAL(30,12), BeginAdditionAmount DECIMAL(30,12), EndingAdditionAmount DECIMAL(30,12), GrossRealizedGain DECIMAL(30,12), BrokerCommission DECIMAL(30,12), NetRealizedGain DECIMAL(30,12), OperatingExpense DECIMAL(30,12), OfferingsellingExpense DECIMAL(30,12), OrganizationalExpense DECIMAL(30,12), USObligationNonTaxableInterestIncome DECIMAL(30,12), BrokerBankBalanceInterestIncome DECIMAL(30,12), FixedIncomeInterestIncome DECIMAL(30,12), CapitalGain DECIMAL(30,12), SellingFee DECIMAL(30,12), SellingManagementFeeMidQuarter DECIMAL(30,12), SyndicateCost DECIMAL(30,12), BeginNetCapital DECIMAL(30,12), DomesticDividendExpense DECIMAL(30,12), FixedIncomeinterestexpense DECIMAL(30,12), ForeignTaxWithheld DECIMAL(30,12), DomesticDividendIncome DECIMAL(30,12), RealizedGainShortTerm DECIMAL(30,12))i have following records in table HeadIDFieldMappingHeadID FieldName1 BeginUnits3 BeginAdditionUnits4 BeginAdditionAmount5 EndingAdditionAmount6 GrossRealizedGain7 BrokerCommission8 NetRealizedGain10 OperatingExpense11 OfferingsellingExpense 12 OrganizationalExpense13 USObligationNonTaxableInterestIncome14 BrokerBankBalanceInterestIncome 15 FixedIncomeInterestIncome16 CapitalGain17 SellingFee18 SellingManagementFeeMidQuarter19 SyndicateCost20 BeginNetCapital21 DomesticDividendExpense22 FixedIncomeinterestexpense23 ForeignTaxWithheld 24 DomesticDividendIncome25 RealizedGainShortTermin table #ReportingNumbers i have field HeadID, now how can i update value for headID from vData table using dynamic query having join from these two tables (vData,HeadIDFieldMapping)?sneha |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:26:38
|
| Sample data for ALL three tables, please...Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-25 : 02:27:23
|
| And if you care enough for a proper answer, you will also post your expected output based on the sample data you provide.Peter LarssonHelsingborg, Sweden |
 |
|
|
snehsoni
Starting Member
3 Posts |
Posted - 2007-01-29 : 02:06:44
|
| sample data for table HeadIDFieldMappingHeadID FieldName1 BeginUnits3 BeginAdditionUnits4 BeginAdditionAmount5 EndingAdditionAmount6 GrossRealizedGain7 BrokerCommission8 NetRealizedGain10 OperatingExpense11 OfferingsellingExpense 12 OrganizationalExpense13 USObligationNonTaxableInterestIncome14 BrokerBankBalanceInterestIncome 15 FixedIncomeInterestIncome16 CapitalGain17 SellingFee18 SellingManagementFeeMidQuarter19 SyndicateCost20 BeginNetCapital21 DomesticDividendExpense22 FixedIncomeinterestexpense23 ForeignTaxWithheld 24 DomesticDividendIncome25 RealizedGainShortTermsample data for table #ReportingNumbers HeadID EntityID Type Value 1 1 'IN' NULL3 1 'IN' NULL4 1 'IN' NULL5 1 'IN' NULL6 1 'IN' NULLsample data for table vDataFundID InvestorID AccountingPeriodDate BeginUnits BeginAdditionUnits BeginAdditionAmount EndingAdditionAmount GrossRealizedGain 1 1 '1/1/5' 10 10 100.20 120.3 20now i want to update table #ReportingNumbers (value field) as followsHeadID EntityID Type Value 1 1 'IN' 103 1 'IN' 104 1 'IN' 100.205 1 'IN' 120.36 1 'IN' 20how it can be done?sneha |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-29 : 02:42:28
|
[code]drop table #ReportingNumbersdrop table HeadIDFieldMappingdrop table vData--------------------------------------------- Prepare the table & data for testing-------------------------------------------create table #ReportingNumbers( HeadID INT, EntityID INT, Type VARCHAR(10), Value DECIMAL(30,12))create table HeadIDFieldMapping( HeadID INT, FieldName VARCHAR(100))create table vData( FundID INT, InvestorID INT, AccountingPeriodDate DATETIME, BeginUnits DECIMAL(30,12), BeginAdditionUnits DECIMAL(30,12), BeginAdditionAmount DECIMAL(30,12), EndingAdditionAmount DECIMAL(30,12), GrossRealizedGain DECIMAL(30,12), BrokerCommission DECIMAL(30,12), NetRealizedGain DECIMAL(30,12), OperatingExpense DECIMAL(30,12), OfferingsellingExpense DECIMAL(30,12), OrganizationalExpense DECIMAL(30,12), USObligationNonTaxableInterestIncome DECIMAL(30,12), BrokerBankBalanceInterestIncome DECIMAL(30,12), FixedIncomeInterestIncome DECIMAL(30,12), CapitalGain DECIMAL(30,12), SellingFee DECIMAL(30,12), SellingManagementFeeMidQuarter DECIMAL(30,12), SyndicateCost DECIMAL(30,12), BeginNetCapital DECIMAL(30,12), DomesticDividendExpense DECIMAL(30,12), FixedIncomeinterestexpense DECIMAL(30,12), ForeignTaxWithheld DECIMAL(30,12), DomesticDividendIncome DECIMAL(30,12), RealizedGainShortTerm DECIMAL(30,12))insert into HeadIDFieldMappingselect 1, 'BeginUnits' union allselect 3, 'BeginAdditionUnits' union allselect 4, 'BeginAdditionAmount' union allselect 5, 'EndingAdditionAmount' union allselect 6, 'GrossRealizedGain' union allselect 7, 'BrokerCommission' union allselect 8, 'NetRealizedGain' union allselect 10, 'OperatingExpense' union allselect 11, 'OfferingsellingExpense' union allselect 12, 'OrganizationalExpense' union allselect 13, 'USObligationNonTaxableInterestIncome' union allselect 14, 'BrokerBankBalanceInterestIncome' union allselect 15, 'FixedIncomeInterestIncome' union allselect 16, 'CapitalGain' union allselect 17, 'SellingFee' union allselect 18, 'SellingManagementFeeMidQuarter' union allselect 19, 'SyndicateCost' union allselect 20, 'BeginNetCapital' union allselect 21, 'DomesticDividendExpense' union allselect 22, 'FixedIncomeinterestexpense' union allselect 23, 'ForeignTaxWithheld' union allselect 24, 'DomesticDividendIncome' union allselect 25, 'RealizedGainShortTerm'insert into #ReportingNumbers(HeadID, EntityID, Type, Value)select 1, 1, 'IN', NULL union allselect 3, 1, 'IN', NULL union allselect 4, 1, 'IN', NULL union allselect 5, 1, 'IN', NULL union allselect 6, 1, 'IN', NULLinsert into vData(FundID, InvestorID, AccountingPeriodDate, BeginUnits, BeginAdditionUnits, BeginAdditionAmount, EndingAdditionAmount, GrossRealizedGain)select 1, 1, '1/1/5', 10, 10, 100.20, 120.3, 20--------------------------------------------- Generate the dynamic query--------------------------------------------- Querydeclare @sql nvarchar(4000)select @sql = ''select @sql = @sql + 'update t' + char(13)select @sql = @sql + 'set t.Value = case' + char(13)select @sql = @sql + char(9) + 'when t.HeadID = ' + convert(varchar(10), t.HeadID) + ' then d.' + m.FieldName + char(13)from #ReportingNumbers t inner join HeadIDFieldMapping m on t.HeadID = m.HeadIDselect @sql = @sql + char(9) + 'end' + char(13)select @sql = @sql + 'from #ReportingNumbers t inner join vData d' + char(13)select @sql = @sql + 'on t.EntityID = d.FundID' + char(13)print @sql/* update tset t.Value = case when t.HeadID = 1 then d.BeginUnits when t.HeadID = 3 then d.BeginAdditionUnits when t.HeadID = 4 then d.BeginAdditionAmount when t.HeadID = 5 then d.EndingAdditionAmount when t.HeadID = 6 then d.GrossRealizedGain endfrom #ReportingNumbers t inner join vData don t.EntityID = d.FundID */exec (@sql)select * from #ReportingNumbers[/code] KH |
 |
|
|
snehsoni
Starting Member
3 Posts |
Posted - 2007-01-29 : 03:06:56
|
| Thanks but i need any other solution or way to convert it into dynamic query because i have more than 125 fields in vData table and very much complicated queries which i need to convert in dynamic queries.sneha |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-01-29 : 03:12:44
|
what is the reason that the query can't handle 125 fields ? KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-01-29 : 03:55:42
|
| If you have 125 columns, there is something wrong with your table design.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|