动态存储过程

时间:2015-07-21 03:32:33

标签: sql-server stored-procedures

我已尝试过以下存储过程。我必须为7个类似的表做。所以我创建了一个并且在ssis任务中,我将使用带有表名的exec sp编写命令并传递参数。但我想这会影响性能。有什么方法可以做得更好吗?

CREATE PROCEDURE [dbo].[Proc_deletedups] (@tablename varchar(30))
As  
   -- Add the parameters for the stored procedure here
BEGIN
SET NOCOUNT ON;

declare @deletequery varchar(max), @insertquery varchar(max)

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- delete dups from dbo.billtoparty table

Select @deletequery = '
DELETE FROM dbo.'+@tablename + ' WHERE ID NOT IN
(
SELECT MAX(ID)
FROM dbo.' + @tablename + 
' GROUP BY QuoteNumber, Quotedate, Quoteversion)';

exec (@deletequery);

--insert unique rows into tar.billtoparty table
select @insertquery = '
insert into tar.' + @tablename + ' (QuoteNumber, QuoteDate, QuoteVersion,
AccountNumber, AccountName, SuperUserFirstName,     SuperUserLastName,
SuperUserEmail, ProcurementUserFirstName, ProcurementUserLastName,
ProcurementUserEmail, CountryDialing, AreaDialing, LandNumber, Extension,
 Mobile, AddressLine1, AddressLine2, AddressLine3, Province, Zipcode, City,
 State, County, Country)
(select QuoteNumber, QuoteDate, QuoteVersion, AccountNumber, AccountName,
 SuperUserFirstName, SuperUserLastName, SuperUserEmail, 
ProcurementUserFirstName, ProcurementUserLastName, ProcurementUserEmail,
 CountryDialing, AreaDialing, LandNumber, Extension, Mobile, AddressLine1, 
AddressLine2, AddressLine3, Province, Zipcode, City, State, County, Country 
from dbo.' + @tablename + ')';
exec (@insertquery);
END
GO

0 个答案:

没有答案