RRinTetons
IS-IT--Management
The Task: Update all accounting general ledger segments in a database. There are 4 account segments. Tables that contain gl accounts have columns named 'xxxxSegment1',
'xxxxSegment2',
'xxxxSegment3',
'xxxxSegment4'.
I've got the table names and column names in a table. There are 496 of them. The 'xxxx' part is consistent, so, as an example, we've got
TableName, Seg1ColName, Seg2ColName, Seg3ColName, Seg4ColName
--------------------------------------------------------------------------
ARClass,FinanceSegment1,FinanceSegment2,FinanceSegment3,FinanceSegment4
ARClass,Segment1,Segment2,Segment3,Segment4
ARInvoiceDetail,Segment1,Segment2,Segment3,Segment4
ARInvoiceHeader,Segment1,Segment2,Segment3,Segment4
ARPaymentHeader,ARSegment1,ARSegment2,ARSegment3,ARSegment4
ARPaymentHeader,PaymentSegment1,PaymentSegment2,PaymentSegment3,PaymentSegment4
I've also got a mapping table with the current segment values and the new ones.
I want to write an update query that updates the old segment values to the new ones in each table, so one would be:
I can do it with some very ugly and convoluted text processing to actually write several hundred distinct UPDATE statements, but I'm always looking for more elegant ways to get things done. Since Accounting is likely to change their minds multiple times as to just what they want changed to what, I'd like a way that doesn't require all the convolution!
-
Richard Ray
DBA, Developer, Data Analyst
Jackson Hole Mountain Resort
'xxxxSegment2',
'xxxxSegment3',
'xxxxSegment4'.
I've got the table names and column names in a table. There are 496 of them. The 'xxxx' part is consistent, so, as an example, we've got
TableName, Seg1ColName, Seg2ColName, Seg3ColName, Seg4ColName
--------------------------------------------------------------------------
ARClass,FinanceSegment1,FinanceSegment2,FinanceSegment3,FinanceSegment4
ARClass,Segment1,Segment2,Segment3,Segment4
ARInvoiceDetail,Segment1,Segment2,Segment3,Segment4
ARInvoiceHeader,Segment1,Segment2,Segment3,Segment4
ARPaymentHeader,ARSegment1,ARSegment2,ARSegment3,ARSegment4
ARPaymentHeader,PaymentSegment1,PaymentSegment2,PaymentSegment3,PaymentSegment4
I've also got a mapping table with the current segment values and the new ones.
I want to write an update query that updates the old segment values to the new ones in each table, so one would be:
Code:
UPDATE [dbo].[ARInvoiceHeader]
SET [Segment1] = [gau].[NewSegment1Value]
[Segment2] = [gau].[NewSegment2Value]
[Segment3] = [gau].[NewSegment3Value]
[Segment4] = [gau].[NewSegment4Value]
FROM [dbo].[ARInvoiceHeader] AS [aih]
JOIN [JHMR_Custom].[900_Utility].[GLAccountUpdate] AS [gau]
ON [aih].[Segment1] = [gau].[OldSegment1Value]
AND [aih].[Segment2] = [gau].[OldSegment2Value]
AND [aih].[Segment3] = [gau].[OldSegment3Value]
AND [aih].[Segment4] = [gau].[OldSegment4Value]
I can do it with some very ugly and convoluted text processing to actually write several hundred distinct UPDATE statements, but I'm always looking for more elegant ways to get things done. Since Accounting is likely to change their minds multiple times as to just what they want changed to what, I'd like a way that doesn't require all the convolution!
-
Richard Ray
DBA, Developer, Data Analyst
Jackson Hole Mountain Resort