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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help streamline sql code

Author  Topic 

gator
Starting Member

5 Posts

Posted - 2012-05-03 : 20:23:22
I need help making the code below run smoother/faster. Do you have any suggestions for making it better?

What it does is take a huge database that has changes to records all in a single row and outputs a table with the field that changed and the old value and new value along with the name and date of the user that changed it. Each time a field changes there is a 'Y' placed in a field named CI_[fieldname]. This is just the code for one of 34 columns that can change.

I know there must be a better way than I am doing it so any help is appreciated.


--CI_ACT_STATUS A change in the action status
IF OBJECT_ID('TempDB..[#Temp1]','U') IS NOT NULL Drop table [#Temp1]
SELECT ACT_REF, TRANSACTION_TYPE, PRJVER_ID, HULL, WORKPACKAGE_ACTIVITY, WORKORDER_ACTIVITY, JOB_ACTIVITY,
ACT_TYPE, ACTIVITY_TYPE, WORKSTATION, OP, DEPT_ID, SCHEDULERELDATE, ACTUALRELDATE, ACT_PRESETSTARTDATE,
ACT_PRESETENDDATE, ACT_ACTSTARTDATE, ACT_ACTENDDATE, ACT_BASELINESTARTDATE, ACT_BASELINEENDDATE,
STORE_BUDGET1, STORE_BUDGET2, STORE_BUDGET3, STORE_BUDGET4, STORE_BUDGETPLANNED,
ACT_STATUS, 'ACT_STATUS' as Changed_Column,
NULL as Old_Value, ACT_STATUS as New_Value, MOD_DATE, MOD_USER
INTO #Temp1
FROM dbAuditLogMgr.dbo.AuditData
WHERE CI_ACT_STATUS = 'Y'
ORDER BY MOD_DATE
go

ALTER TABLE #Temp1
ALTER COLUMN Old_Value varchar(255)
go

UPDATE #Temp1
SET Old_Value = dbAuditLogMgr.dbo.AuditData.ACT_STATUS
FROM #Temp1 INNER JOIN dbAuditLogMgr.dbo.AuditData
ON #Temp1.ACT_REF = dbAuditLogMgr.dbo.AuditData.ACT_REF
AND #Temp1.MOD_DATE <> dbAuditLogMgr.dbo.AuditData.MOD_DATE
WHERE dbAuditLogMgr.dbo.AuditData.MOD_DATE = (SELECT MAX(dbAuditLogMgr.dbo.AuditData.MOD_DATE)
FROM dbAuditLogMgr.dbo.AuditData
WHERE dbAuditLogMgr.dbo.AuditData.MOD_DATE < #Temp1.MOD_DATE
AND #Temp1.ACT_REF = dbAuditLogMgr.dbo.AuditData.ACT_REF)
go

UPDATE #Temp1
SET Old_Value = dbAuditLogMgr.dbo.AuditData.ACT_STATUS
FROM #Temp1 INNER JOIN dbAuditLogMgr.dbo.AuditData
ON #Temp1.ACT_REF = dbAuditLogMgr.dbo.AuditData.ACT_REF
WHERE dbAuditLogMgr.dbo.AuditData.MOD_DATE IS NULL
AND #Temp1.OLD_Value IS NULL
AND dbAuditLogMgr.dbo.AuditData.MOD_DATE IS NULL
go

UPDATE #Temp1
SET Old_Value = dbAuditLogMgr.dbo.AuditData.ACT_STATUS
FROM #Temp1 INNER JOIN dbAuditLogMgr.dbo.AuditData
ON #Temp1.ACT_REF = dbAuditLogMgr.dbo.AuditData.ACT_REF
WHERE dbAuditLogMgr.dbo.AuditData.MOD_DATE IS NULL
AND #Temp1.OLD_Value IS NULL
AND dbAuditLogMgr.dbo.AuditData.TRANSACTION_TYPE = 'INSERT'
go

INSERT INTO dbAuditLogMgr.dbo.condensedData
SELECT *
FROM #Temp1
go

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 20:32:27
are you using SQL Server 2008? if yes,have a look at Change Data Capture feature

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gator
Starting Member

5 Posts

Posted - 2012-05-03 : 20:35:29
I am using SQL Server 2000.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 20:45:59
you can implement a trigger for capturing this info. How frequently DML operations happen in your table? are they bulk operations?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gator
Starting Member

5 Posts

Posted - 2012-05-03 : 20:58:20
My database does not capture the data. The large table is pushed to my server once a day from an Oracle server.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 21:01:46
quote:
Originally posted by gator

My database does not capture the data. The large table is pushed to my server once a day from an Oracle server.


sorry i'm confused
so does that mean above code is in Oracle?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gator
Starting Member

5 Posts

Posted - 2012-05-03 : 21:14:37
The big table comes from an Oracle databse that I never see. I do not have access to the Oracle server. I only get a copy of the table. The code is used on my SQL Server 2000 box.

A user can make multiple field changes at a time on the Oracle box.
My users want me to take the big table that is loaded to my server each day that has all changes from a single change and split it up into a table that shows each field that changed as its own record with the old and new data.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-03 : 21:23:49
quote:
Originally posted by gator

The big table comes from an Oracle databse that I never see. I do not have access to the Oracle server. I only get a copy of the table. The code is used on my SQL Server 2000 box.

A user can make multiple field changes at a time on the Oracle box.
My users want me to take the big table that is loaded to my server each day that has all changes from a single change and split it up into a table that shows each field that changed as its own record with the old and new data.


for that do you've any audit fields in your oracle table to identify changed records?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gator
Starting Member

5 Posts

Posted - 2012-05-03 : 21:37:07
Every field the user can change has a field before it that begins with CI_. For example if they change the PART_NO the field CI_PART_NO will have a 'Y' in it.


I don't need to know a better way to capture the data as I will never be capturing the data. All I need is help making the posted code more efficient.
Go to Top of Page
   

- Advertisement -