use master go /* CREATE TABLE TEmployeeInfo ( FMobile VARCHAR(50), FEmpNo VARCHAR(50), FName VARCHAR(50), FPosName VARCHAR(1000), FCompName VARCHAR(1000), FCreateDate DATETIME, PRIMARY KEY(FMobile) ) */ if exists(select 1 from master..sysservers where srvname='ms') begin exec sp_dropserver 'ms','droplogins' end DECLARE @Server VARCHAR(1024) DECLARE @Database VARCHAR(1024) DECLARE @User VARCHAR(1024) DECLARE @Password VARCHAR(1024) SET @Server = '182.247.245.234,9919' SET @User = 'Sa' SET @Password = 'Mik20170510~!@#' exec sp_addlinkedserver 'ms','','SQLOLEDB',@Server exec sp_addlinkedsrvlogin 'ms','false',null,@User,@Password GO --转换日期 IF object_id('dbo.FNBKDate') IS NOT NULL BEGIN DROP FUNCTION dbo.FNBKDate END GO CREATE FUNCTION FNBKDate ( @FmtDate varchar(10) ) RETURNS DATETIME BEGIN IF ISNULL(@FmtDate,'') = '' OR (LEN(@FmtDate) != 8 AND LEN(@FmtDate) != 4) OR @FMTDATE = '000000' BEGIN RETURN NULL END IF LEN(@FmtDate) = 4 BEGIN SELECT @FmtDate = '9999' + @FmtDate END DECLARE @Year VARCHAR(4) DECLARE @Month VARCHAR(2) DECLARE @Day VARCHAR(2) SELECT @Year = LEFT(@FmtDate,4),@Month = SUBSTRING(@FmtDate,5,2),@Day = RIGHT(@FmtDate,2) IF @Year < '1753' BEGIN SELECT @Year = '9999' END IF @Month < '01' OR @Month > '12' OR @Day < '01' OR @Day > '31' BEGIN RETURN NULL END IF @Day > '30' AND @Month IN('04','06','09','11') BEGIN SET @Day = '30' END --IF @Month = '02' AND @Day = '29' --BEGIN -- print ''-- SET @Year = '2000' --END select @FmtDate = @Year + '-' + @Month + '-' + @Day return @FmtDate END GO IF object_id('dbo.FNIsMobile') IS NOT NULL BEGIN DROP FUNCTION dbo.FNIsMobile END GO CREATE FUNCTION dbo.FNIsMobile (@str VARCHAR(12)) RETURNS BIT BEGIN RETURN CASE WHEN LEN(@str)=11 AND @str LIKE '[1][356789]%' AND @str NOT LIKE '%[^0-9]%' THEN 1 ELSE 0 END END GO IF OBJECT_ID('PUploadEmployeeInfo','P') IS NOT NULL BEGIN DROP PROC PUploadEmployeeInfo END GO CREATE PROC PUploadEmployeeInfo ( @Database VARCHAR(100) ) AS BEGIN EXEC (' DECLARE @CompName VARCHAR(50) SELECT @CompName = gae02c FROM '+@Database+'..gam05 WHERE gae01c = ''001'' PRINT ''店名:'' + @CompName SELECT haa20c AS FMobile,haa02c AS FName,FEmpNo, ISNULL(s.gsb03c,'''') AS FPosName,dbo.FNBKDate(haa92d) AS FCreateDate INTO #EmpInfo FROM '+@Database+'..ham01 A LEFT JOIN '+@Database+'..gsm02 AS s on gsb00c = haa00c AND gsb02c = haa25c AND gsb01c = ''R'' INNER JOIN (SELECT FMobile,FEmpNo,MAX(haa00c) AS FCompNo FROM '+@Database+'..ham01 AS A, (SELECT haa20c AS FMobile,MAX(haa01c) AS FEmpNo FROM '+@Database+'..ham01 WHERE dbo.FNIsMobile(haa20c) = 1 GROUP BY haa20c) AS B WHERE A.haa01c = B.FEmpNo AND A.haa20c = B.FMobile AND dbo.FNIsMobile(haa20c) = 1 GROUP BY FMobile,FEmpNo) AS C ON C.FEmpNo = A.haa01c AND C.FMobile = A.haa20c AND C.FCompNo = a.haa00c AND dbo.FNIsMobile(haa20c) = 1 UPDATE A SET A.FName = B.FName, A.FCompName = @CompName, A.FPosName = B.FPosName, A.FCreateDate = B.FCreateDate FROM ms.master.dbo.TEmployeeInfo AS A,#EmpInfo AS B WHERE A.FMobile = B.FMobile AND B.FCreateDate >= A.FCreateDate INSERT ms.master.dbo.TEmployeeInfo(FMobile,FEmpNo,FName,FCompName,FPosName,FCreateDate) SELECT FMobile,FEmpNo,FName,@CompName,FPosName,FCreateDate FROM #EmpInfo AS A WHERE NOT EXISTS(SELECT 1 FROM ms.master.dbo.TEmployeeInfo AS B WHERE B.FMobile = A.FMobile) drop table #EmpInfo ') END GO --SELECT * FROM ms.master.dbo.TEmployeeInfo BEGIN declare @database VARCHAR(200) declare @index int set @index = 1 declare dbList cursor for select name from master..sysdatabases where status & 512 != 512 open dbList fetch dbList into @database while @@fetch_status=0 begin DECLARE @CheckSql NVARCHAR(1000) SELECT @CheckSql = 'SELECT @Biz = (SELECT count(1) FROM ' + @database + '..sysobjects WHERE name = ''ham01'')' DECLARE @biz BIT EXEC sp_executesql @CheckSql,N'@biz BIT out ',@biz out; SELECT @biz = 0 WHERE @biz IS NULL IF @biz != 0 BEGIN print '数据库:' + @database DECLARE @Sql VARCHAR(MAX) SELECT @Sql = ' EXEC PUploadEmployeeInfo ''' + @database + '''' EXEC(@Sql) END fetch dbList into @database end close dbList deallocate dbList END go