ALTER PROCEDURE [MDM].[PRO_COPYTABLE]
--@TableName
nvarchar(50)--例如:MDM.TB_CUST_EXTCOMPANY_copy
AS
BEGIN
DECLARE @delsql nvarchar(2000)
DECLARE @insertsql nvarchar(2000)
DECLARE @TableNames nvarchar(2000)
DECLARE @TableName nvarchar(2000)
DECLARE @m int
DECLARE @n int
SET
@TableNames='MDM.TB_CUST_ATTACHMENT,MDM.TB_CUST_AUTHDATA,
MDM.TB_CUST_BANKDATA,MDM.TB_CUST_EXTCOMPANY,
MDM.TB_CUST_EXTSALESDATA,MDM.TB_CUST_LOG,
MDM.TB_CUST_SALES_ORG_SHIPTO,
MDM.TB_CUST_SHIPTO,MDM.TB_CUST_SOLDTO,'
set @m = charindex(',',@TableNames)
set @n = 1
WHILE(@m > 0)
BEGIN
SET @TableName=substring(@TableNames,@n,@m-@n)
SET @n = @m + 1
SET @m = charindex(',',@TableNames,@n)
print @TableName
SET @delsql='DELETE cp FROM ITSV.mdm.'+@TableName+ ' cp WHERE
NOT EXISTS(SELECT * FROM mdm.'
+@TableName+' WHERE UPDATE_DATETIME =
cp.UPDATE_DATETIME)'
PRINT @delsql
SET @insertsql='INSERT ITSV.mdm.'+@TableName+' SELECT * FROM
mdm.'+@TableName+' c where not exists(SELECT * FROM
ITSV.mdm.'
+@TableName+ ' cp WHERE cp.GUID = c.GUID)'
PRINT @insertsql
EXEC(@delsql)
EXEC(@insertsql)
END
END
http://blog.csdn.net/quxiao2002/article/details/8004403