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 |
Dev@nlkss
134 Posts |
Posted - 2009-01-21 : 09:24:49
|
Hi All,I am writing stored procedure for transfering data from 4 staging tables to 4 master tables using sql server 2005 which are in same db and same server.I have following requirements-->I will first check the count in both staging and master tables-->if staging table count is greater than master table count then-->truncate master table records.-->then insert query executes which fetches records from staging to master table.For each staging to master table i am writnig individual transactionbegin trybegin transactioncode goes herecommit transactionend trybegin catchrollback transactionend catchlike for for 4 staging tables to 4 master tableBut the problem is when i am executing this sql server getting very slow almost getting struck.Is this the correct way which i am following if not please suggest me the write way.its really urgent.Thanks in advanceSatya |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-21 : 09:30:40
|
are all four transfers dependent? |
 |
|
Dev@nlkss
134 Posts |
Posted - 2009-01-21 : 09:38:13
|
Visakh thanks for ur replyFirst 2 are not dependentbut after inserting from third staging to master table i will get one of the columns from third master table and insert into fourth master tablei.e last two are dependentSatya |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-21 : 09:40:51
|
then why putting them in seperate tranbsactions? put dependent ones in same tran |
 |
|
Dev@nlkss
134 Posts |
Posted - 2009-01-21 : 09:46:39
|
does it mean dependent tables are in single transactionSatya |
 |
|
Dev@nlkss
134 Posts |
Posted - 2009-01-21 : 09:57:29
|
Thanks visakhI am posting my sql scriptplease go throgth once and guide me if i am going wrong.BEGINSET NOCOUNT ON;--Load the Records from Staging tables to master table BEGIN TRY BEGIN TRANSACTION; IF((SELECT COUNT(VendorID) FROM [dbo].[Vendor_Stage]) > (SELECT COUNT(VendorID) FROM [dbo].[Vendor])) BEGIN TRUNCATE TABLE [dbo].[Vendor] INSERT INTO [dbo].[Vendor] SELECT VendorID,VendorName,VendorType,CircleID FROM [dbo].[Vendor_stage] COMMIT TRANSACTION END END TRY BEGIN CATCH ROLLBACK TRANSACTION END CATCH BEGIN TRY BEGIN TRANSACTION; IF((SELECT COUNT(DeptID) FROM [dbo].[DeptInfo_HRMSPark]) > (SELECT COUNT(DeptID) FROM [dbo].[DeptInfo])) BEGIN TRUNCATE TABLE [dbo].[DeptInfo] INSERT INTO [dbo].[DeptInfo] SELECT DeptID,DeptDesc FROM [dbo].[DeptInfo_HRMSPark] COMMIT TRANSACTION END END TRY BEGIN CATCH ROLLBACK TRANSACTION END CATCH BEGIN TRY BEGIN TRANSACTION; IF((SELECT COUNT(DesgID) FROM [dbo].[Designations_HRMSPark]) > (SELECT COUNT(DesgID) FROM [dbo].[Designations])) BEGIN TRUNCATE TABLE [dbo].[Designations] INSERT INTO [dbo].[Designations] SELECT DesgID,DesgName FROM [dbo].[Designations_HRMSPark] COMMIT TRANSACTION END END TRY BEGIN CATCH ROLLBACK TRANSACTION END CATCH BEGIN TRY BEGIN TRANSACTION; IF((SELECT COUNT(DISTINCT EmpID) FROM [dbo].[empdetails_hrmspark]) > (SELECT COUNT(DISTINCT EmpID) FROM [dbo].[Employees])) BEGIN TRUNCATE TABLE [dbo].[Employees] INSERT INTO [dbo].[Employees] SELECT DISTINCT ED.EmpID,ED.EmpCode,CL.Display AS DisplayName,NULL,NULL,Gender,CL.BirthDay AS DOB,DOJ,DelFlag AS HRMSStatus,ED.CircleID,Designation,DeptID FROM [dbo].[empdetails_hrmspark] ED INNER JOIN [dbo].[contactslist_hrmspark] CL ON ED.EmpID=CL.ContactID INSERT INTO [dbo].[ITOEngineerList](EmpID) SELECT EmpID FROM [dbo].[Employees] WHERE EmpID NOT IN(SELECT EmpID FROM [dbo].[EngineerList]) END END TRY BEGIN CATCH ROLLBACK TRANSACTION END CATCH SET NOCOUNT OFFENDSatya |
 |
|
Dev@nlkss
134 Posts |
Posted - 2009-01-22 : 00:25:34
|
Hiplease look into the above script.Satya |
 |
|
|
|
|
|
|