简体   繁体   中英

Replace cursor in SQL Server 2008 R2

I have a Data Warehouse that we reload every night which takes two hours. Right now we truncate the tables and reload it from our live Microsoft Dynamics 2000 server which splits out the company tables separately.

For instance, we have company ABC, DEF, and GHI and we have the table Customers . I have three separate tables in our production server called ABC_$Customers, DEF_$Customers, and GHI_$Customers.

In the Data Warehouse table I want only one Customers table which is the union of ABC_$Customers, DEF_$Customers, and GHI_$Customers.

Currently we use a cursor to loop through the Company Names and insert the table for the company and then fetch the next company for all our tables.

When we did not truncate the tables, and used joins with insert/updates it filled the transaction log really fast.

What would be your suggestion to replace the cursors? I have researched and found SET and CTE but am not sure if it would work because of the dynamic table names.

Example of one of the cursors:

use DataWarehouse

truncate table Customers

declare @company varchar(250)
declare @companyID varchar(50)
declare @sql nvarchar(4000)

DECLARE Company_cursor CURSOR FOR 
SELECT CompanyID, CompanyName
FROM CompanyNames

OPEN Company_cursor

FETCH NEXT FROM Company_cursor
INTO @companyID, @company

WHILE @@FETCH_STATUS = 0
BEGIN

set @sql = 'Insert Into Customers(CompanyID, CustomerID, CustomerName)
             '+ ' select ' + Cast(@companyID as varchar(10)) 
           + ',[CustomerID],[CustomerName] from [Database].dbo.[' + @company + '$Customers]      '

exec sp_executesql @sql

FETCH NEXT FROM Company_cursor
INTO @companyID, @company
END

CLOSE Copmany_cursor
DEALLOCATE Copmany_cursor

If you use dynamic table names, your code seems reasonable.

You might have made a join with system tables to retrieve all company names in a single query into one giant dynamic SQL statement then run this statement, but I doubt it would make much difference.

Cursors are perfectly justified here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM