数据库空闲连接超时限定 用户连接数据库空闲时间超过10分钟后,自动断开连接,并发送信息提示用户。现在通过开启所有电脑的Messenger服务才能发送消息,很麻烦,请教各位高手,有没有其他方式能够向用户发送消息,不需要第三方软件,如:QQ等。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SQL Server 并没有内置邮件服务器(Mail Server),它跟我们发送邮件一样,需要用户名和密码通过 SMTP(Simple Message Transfer Protocol)去连接邮件服务器。我们想让 SQL Server 来发送邮件,首先要告诉它用户名称,密码,服务器地址,网络传送协议,邮件服务器的端口等信息。 以下脚本实现了数据库邮件的配置:--下面是具体的配置邮件步骤 --在 sa 系统帐户下运行。 ----1. 启用 SQL Server 2005 邮件功能。 use mastergoexec sp_configure 'show advanced options',1goreconfiguregoexec sp_configure 'Database mail XPs',1goreconfigurego--2. 在 SQL Server 2005 中添加邮件帐户(account) exec msdb..sysmail_add_account_sp @account_name = 'jgj' -- 邮件帐户名称(SQL Server 使用) ,@email_address = [email='[email protected]']'[email protected]'[/email] -- 发件人邮件地址 ,@display_name = null -- 发件人姓名 ,@replyto_address = null ,@description = null ,@mailserver_name = '203.86.70.229' -- 邮件服务器地址 ,@mailserver_type = 'SMTP' -- 邮件协议(SQL 2005 只支持 SMTP) ,@port = 25 -- 邮件服务器端口 ,@username = [email='[email protected]']'[email protected]'[/email] -- 用户名 ,@password = 'xxxxxx' -- 密码 ,@use_default_credentials = 0 ,@enable_ssl = 0 ,@account_id = null--3. 在 SQL Server 2005 中添加 profile exec msdb..sysmail_add_profile_sp @profile_name = 'dba_profile' -- profile 名称 ,@description = 'dba mail profile' -- profile 描述 ,@profile_id = null-- 在 SQL Server 2005 中映射 account 和 profile exec msdb..sysmail_add_profileaccount_sp @profile_name = 'dba_profile' -- profile 名称 ,@account_name = 'jgj' -- account 名称 ,@sequence_number = 1 -- account 在 profile 中顺序 --5. 利用 SQL Server 2005 Database Mail 功能发送邮件。 exec msdb..sp_send_dbmail @profile_name = 'dba_profile' -- profile 名称 ,@recipients = [email='[email protected]']'[email protected]'[/email] -- 收件人邮箱 ,@subject = 'SQL Server 2005 Mail 测试' -- 邮件标题 ,@body = 'Hello Mail!测试' -- 邮件内容 ,@body_format = 'TEXT' -- 邮件格式 --6. 查看邮件发送情况: use msdbgoselect * from sysmail_allitemsselect * from sysmail_mailitemsselect * from sysmail_event_log--如果不是以 sa 帐户发送邮件,则可能会出现错误: ----Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1--EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'.----这是因为,当前 SQL Server 登陆帐户(login),在 msdb 数据库中没有发送数据库邮件的权限,需要加入 msdb 数据库用户,并通过加入 sp_addrolemember 角色赋予权限。假设该SQL Server 登陆帐户名字为 “dba” ----use msdb--go----create user dba for login dba--go----exec dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole',-- @membername = 'dba'--go----此时,再次发送数据库邮件,仍可能有错误: ----Msg 14607, Level 16, State 1, Procedure sp_send_dbmail, Line 119--profile name is not valid----虽然,数据库用户 “dba” 已经在 msdb 中拥有发送邮件的权限了,但这还不够,他还需要有使用 profile:“dba_profile” 的权限。 ----use msdb--go----exec sysmail_add_principalprofile_sp @principal_name = 'dba'-- ,@profile_name = 'dba_profile'-- ,@is_default = 1----从上面的参数 @is_default=1 可以看出,一个数据库用户可以在多个 mail profile 拥有发送权限。 借助第三方工具或自己写代码,楼主英文咋样?LOOKhttp://www.sql-server-performance.com/articles/dba/email_alerts_notification_p1.aspx http://www.sql-server-performance.com/articles/dba/email_alerts_notification_p1.aspxhttp://www.sql-server-performance.com/articles/dba/email_alerts_notification_p1.aspx 请教关于:XML类型数据的使用。 sql语句问题,请高手帮忙看看 sql 如何实现 split的功能 sql server的视图是实际存在的物理数据还是只是一段SQL语句?? 谁见到过这种问题?非常奇怪 如何从下面这个表的内容生成另外一个表?(之后还有分送,在线等待!!急急急!!!) 数据库质疑--附加问题 【急救】 如何写这个sql语句 请教一个关于对比更新的问题,求大神指点下! 命令设置后结果却无法验证 有谁知道这两道数据库题的解答啊?谢谢! SQL高手来!菜鸟求教!!
以下脚本实现了数据库邮件的配置:
--下面是具体的配置邮件步骤
--在 sa 系统帐户下运行。
--
--1. 启用 SQL Server 2005 邮件功能。
use master
go
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'Database mail XPs',1
go
reconfigure
go
--2. 在 SQL Server 2005 中添加邮件帐户(account)
exec msdb..sysmail_add_account_sp
@account_name = 'jgj' -- 邮件帐户名称(SQL Server 使用)
,@email_address = [email='[email protected]']'[email protected]'[/email] -- 发件人邮件地址
,@display_name = null -- 发件人姓名
,@replyto_address = null
,@description = null
,@mailserver_name = '203.86.70.229' -- 邮件服务器地址
,@mailserver_type = 'SMTP' -- 邮件协议(SQL 2005 只支持 SMTP)
,@port = 25 -- 邮件服务器端口
,@username = [email='[email protected]']'[email protected]'[/email] -- 用户名
,@password = 'xxxxxx' -- 密码
,@use_default_credentials = 0
,@enable_ssl = 0
,@account_id = null
--3. 在 SQL Server 2005 中添加 profile
exec msdb..sysmail_add_profile_sp @profile_name = 'dba_profile' -- profile 名称
,@description = 'dba mail profile' -- profile 描述
,@profile_id = null
-- 在 SQL Server 2005 中映射 account 和 profile
exec msdb..sysmail_add_profileaccount_sp @profile_name = 'dba_profile' -- profile 名称
,@account_name = 'jgj' -- account 名称
,@sequence_number = 1 -- account 在 profile 中顺序
--5. 利用 SQL Server 2005 Database Mail 功能发送邮件。
exec msdb..sp_send_dbmail @profile_name = 'dba_profile' -- profile 名称
,@recipients = [email='[email protected]']'[email protected]'[/email] -- 收件人邮箱
,@subject = 'SQL Server 2005 Mail 测试' -- 邮件标题
,@body = 'Hello Mail!测试' -- 邮件内容
,@body_format = 'TEXT' -- 邮件格式
--6. 查看邮件发送情况:
use msdb
go
select * from sysmail_allitems
select * from sysmail_mailitems
select * from sysmail_event_log
--如果不是以 sa 帐户发送邮件,则可能会出现错误:
--
--Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1
--EXECUTE permission denied on object 'sp_send_dbmail', database 'msdb', schema 'dbo'.
--
--这是因为,当前 SQL Server 登陆帐户(login),在 msdb 数据库中没有发送数据库邮件的权限,需要加入 msdb 数据库用户,并通过加入 sp_addrolemember 角色赋予权限。假设该SQL Server 登陆帐户名字为 “dba”
--
--use msdb
--go
--
--create user dba for login dba
--go
--
--exec dbo.sp_addrolemember @rolename = 'DatabaseMailUserRole',
-- @membername = 'dba'
--go
--
--此时,再次发送数据库邮件,仍可能有错误:
--
--Msg 14607, Level 16, State 1, Procedure sp_send_dbmail, Line 119
--profile name is not valid
--
--虽然,数据库用户 “dba” 已经在 msdb 中拥有发送邮件的权限了,但这还不够,他还需要有使用 profile:“dba_profile” 的权限。
--
--use msdb
--go
--
--exec sysmail_add_principalprofile_sp @principal_name = 'dba'
-- ,@profile_name = 'dba_profile'
-- ,@is_default = 1
--
--从上面的参数 @is_default=1 可以看出,一个数据库用户可以在多个 mail profile 拥有发送权限。