IF EXISTS ( SELECT * FROM sys.procedures WHERE name = 'sp_Send_ER_Mail' )
DROP PROC dbo.sp_Send_ER_Mail ;
GO
CREATE PROC sp_Send_ER_Mail
AS
BEGIN
SET NOCOUNT ON ;
SET XACT_ABORT ON ;
IF NOT EXISTS ( SELECT * FROM dbo.sp_MailAccount_Setting )
RETURN ;
---1.定义参数
BEGIN
--待发送账户,用';'分割
DECLARE @Account_SQL NVARCHAR (MAX) ;
--邮件内容
DECLARE @Body NVARCHAR (MAX) ;
--邮件队列ID
DECLARE @MailItemID INT ;
--主题
DECLARE @Subject NVARCHAR (100) = N'' ;
--ER图版本ID
DECLARE @ERVerID INT ;
--当前时间
DECLARE @NowDate DATETIME ;
--版本记录表变量
DECLARE @Temp_Table TABLE ( CombinSQL NVARCHAR (MAX)) ;
END ;
---2.参数赋值(下面所有的BAcount_ER_Map_Version_DB都改成对应自己的仓库数据库)
BEGIN
SET @Account_SQL = ( SELECT Account + ';' FROM dbo.sp_MailAccount_Setting FOR XML PATH('')) ;
SET @ERVerID = COALESCE(( SELECT MAX(ERVerID)FROM dbo.sp_ER_Mail_Record ), 0) ;
IF NOT EXISTS ( SELECT * FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT WHERE POID = 102 AND VCMT > @ERVerID )
RETURN ;
SET @Subject = (
SELECT MAX(A.NAME)FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT A WHERE A.POID = 102
) + N'项目ER图新提交版本;' + CHAR(39) ;
WITH CTE AS
(
SELECT ROW_NUMBER() OVER ( ORDER BY A.OCDT ) AS Ver,
A.VCMT,
A.OCDT,
B.TDAT
FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT A
INNER JOIN BAcount_ER_Map_Version_DB.dbo.PMTEXT B ON A.VCMT = B.TXID
WHERE
A.POID = 102
),
CTE2 AS
(
SELECT ( CONVERT(NVARCHAR (100), DATEADD(HOUR, 8, CTE.OCDT), 20) + ' 提交Ver' + CONVERT(NVARCHAR (10), CTE.Ver) + '.0:' + CHAR(10) + CTE.TDAT )
+ CHAR(10) AS CombinSQL
FROM CTE
WHERE
CTE.VCMT > @ERVerID
)
INSERT INTO @Temp_Table ( CombinSQL ) SELECT CombinSQL FROM CTE2 ;
SET @Body = ( SELECT CombinSQL + CHAR(10)FROM @Temp_Table FOR XML PATH('')) ;
SET @NowDate = GETDATE() ;
END ;
---3.发送邮件,并写入日志
BEGIN
EXECUTE [msdb].[dbo].[sp_send_dbmail] @profile_name = 'Send_ER_Mail',
@recipients = @Account_SQL,
@body = @Body,
@subject = @Subject,
@mailitem_id = @MailItemID OUTPUT ;
--重新获取最大ER版本ID
SET @ERVerID = ( SELECT MAX(VCMT)FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT ) ;
INSERT INTO dbo.sp_ER_Mail_Record ( MailItemID, ERVerID, Recipient, Sender, MailSubject, MailBody, SendTime )
SELECT @MailItemID,
@ERVerID,
Account,
N'[email protected]',--(这里改成对应自己的发件邮箱)
@Subject,
@Body,
@NowDate
FROM dbo.sp_MailAccount_Setting ;
END ;
END ;
EXEC sp_MS_marksystemobject 'sp_Send_ER_Mail' ; --标识为系统对象
GO
背景:E-R图仓库存放在SQL server数据库中,之后的代码是基于SQL server实现
1.查询版本更替数据
直接给代码,在你对应的仓库数据库执行
WITH CTE AS
(
SELECT A.NAME AS [仓库名称],
A.CODE AS [仓库码],
A.OCDT AS [提交时间],
B.TDAT AS [备注],
ROW_NUMBER() OVER ( ORDER BY A.OCDT ) AS [版本]
FROM dbo.PMOBJT A
INNER JOIN dbo.PMTEXT B ON A.VCMT = B.TXID
WHERE
A.POID = 102
)
SELECT CTE.仓库名称, CTE.仓库码, CTE.版本, CTE.提交时间, CTE.备注 FROM CTE ;
2.配置相关记录表/存储过程
(1)收件人Table:sp_MailAccount_Setting
IF EXISTS ( SELECT * FROM sys.tables WHERE name = 'sp_MailAccount_Setting' )
DROP TABLE sp_MailAccount_Setting ;
CREATE TABLE sp_MailAccount_Setting
(
Account NVARCHAR (100), --邮箱账户
AccountName NVARCHAR (100), --邮箱名称
Memo NVARCHAR (MAX)
) ;
EXEC sp_MS_marksystemobject 'sp_MailAccount_Setting' ; --标识为系统对象
GO
(2)发送记录Table:sp_ER_Mail_Record
IF EXISTS ( SELECT * FROM sys.tables WHERE name = 'sp_ER_Mail_Record' )
DROP TABLE sp_ER_Mail_Record ;
CREATE TABLE sp_ER_Mail_Record
(
MailItemID INT, --邮件队列ID
ERVerID INT, --ER图版本ID
Recipient NVARCHAR (100), --收件人
Sender NVARCHAR (100), --发件人
MailSubject NVARCHAR (100), --主题
MailBody NVARCHAR (MAX), --邮件内容
SendTime DATETIME --发件时间
) ;
EXEC sp_MS_marksystemobject 'sp_ER_Mail_Record' ; --标识为系统对象
GO
(3)发送邮件存储过程
IF EXISTS ( SELECT * FROM sys.procedures WHERE name = 'sp_Send_ER_Mail' )
DROP PROC dbo.sp_Send_ER_Mail ;
GO
CREATE PROC sp_Send_ER_Mail
AS
BEGIN
SET NOCOUNT ON ;
SET XACT_ABORT ON ;
IF NOT EXISTS ( SELECT * FROM dbo.sp_MailAccount_Setting )
RETURN ;
---1.定义参数
BEGIN
--待发送账户,用';'分割
DECLARE @Account_SQL NVARCHAR (MAX) ;
--邮件内容
DECLARE @Body NVARCHAR (MAX) ;
--邮件队列ID
DECLARE @MailItemID INT ;
--主题
DECLARE @Subject NVARCHAR (100) = N'' ;
--ER图版本ID
DECLARE @ERVerID INT ;
--当前时间
DECLARE @NowDate DATETIME ;
--版本记录表变量
DECLARE @Temp_Table TABLE ( CombinSQL NVARCHAR (MAX)) ;
END ;
---2.参数赋值(下面所有的BAcount_ER_Map_Version_DB替换成自己的邮箱)
BEGIN
SET @Account_SQL = ( SELECT Account + ';' FROM dbo.sp_MailAccount_Setting FOR XML PATH('')) ;
SET @ERVerID = COALESCE(( SELECT MAX(ERVerID)FROM dbo.sp_ER_Mail_Record ), 0) ;
IF NOT EXISTS ( SELECT * FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT WHERE POID = 102 AND VCMT > @ERVerID )
RETURN ;
SET @Subject = (
SELECT MAX(A.NAME)FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT A WHERE A.POID = 102
) + N'项目ER图新提交版本;' + CHAR(39) ;
WITH CTE AS
(
SELECT ROW_NUMBER() OVER ( ORDER BY A.OCDT ) AS Ver,
A.VCMT,
A.OCDT,
B.TDAT
FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT A
INNER JOIN BAcount_ER_Map_Version_DB.dbo.PMTEXT B ON A.VCMT = B.TXID
WHERE
A.POID = 102
),
CTE2 AS
(
SELECT ( CONVERT(NVARCHAR (100), DATEADD(HOUR, 8, CTE.OCDT), 20) + ' 提交Ver' + CONVERT(NVARCHAR (10), CTE.Ver) + '.0:' + CHAR(10) + CTE.TDAT )
+ CHAR(10) AS CombinSQL
FROM CTE
WHERE
CTE.VCMT > @ERVerID
)
INSERT INTO @Temp_Table ( CombinSQL ) SELECT CombinSQL FROM CTE2 ;
SET @Body = ( SELECT CombinSQL + CHAR(10)FROM @Temp_Table FOR XML PATH('')) ;
SET @NowDate = GETDATE() ;
END ;
---3.发送邮件,并写入日志
BEGIN
EXECUTE [msdb].[dbo].[sp_send_dbmail] @profile_name = 'Send_ER_Mail',--(填入自己配置好的发送邮件)
@recipients = @Account_SQL,
@body = @Body,
@subject = @Subject,
@mailitem_id = @MailItemID OUTPUT ;
--重新获取最大ER版本ID
SET @ERVerID = ( SELECT MAX(VCMT)FROM BAcount_ER_Map_Version_DB.dbo.PMOBJT ) ;
INSERT INTO dbo.sp_ER_Mail_Record ( MailItemID, ERVerID, Recipient, Sender, MailSubject, MailBody, SendTime )
SELECT @MailItemID,
@ERVerID,
Account,
N'[email protected]',--(改成对应自己的发件邮箱)
@Subject,
@Body,
@NowDate
FROM dbo.sp_MailAccount_Setting ;
END ;
END ;
EXEC sp_MS_marksystemobject 'sp_Send_ER_Mail' ; --标识为系统对象
GO
(4)表触发器(在自己的仓库数据库执行)
CREATE TRIGGER dbo.PMTEXT_Insert_Trriger ON dbo.PMTEXT AFTER INSERT AS BEGIN
EXEC master.dbo.sp_Send_ER_Mail ;
END ;
3.配置发送邮件邮箱(本人用的QQ邮箱,推荐是几个讲的比较详细博主,可以参考,这里不做多赘述)
https://blog.csdn.net/jack8674/article/details/85058489
https://blog.csdn.net/HinKai/article/details/86525515
4.查看发送记录
以上,就是我们配置实现E-R图提交版本之后做邮箱自动推送提醒
------------原创,纯手打,觉得对您有帮助的话,帮忙点个赞哦!