sql server 2008 中的计划程序我的做法正确还是存在问题?

发布于 2024-09-11 07:38:14 字数 1751 浏览 2 评论 0原文

编辑:我收到的电子邮件有问题 邮箱不可用异常! DBEmail.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Xml.Linq;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
    {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        m.From = new System.Net.Mail.MailAddress(Sender);
        m.To.Add(new System.Net.Mail.MailAddress(SendTo));
        m.Subject = Subject;
        m.Body = Body;
        System.Net.Mail.SmtpClient client = null;
        client = new System.Net.Mail.SmtpClient();
        client.Host = mailServer;
        client.UseDefaultCredentials = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        client.Send(m);
    }
}

我必须每晚运行一些存储过程。所以我创建了一个 .sql 文件,它基本上包含 -

exec proc1
exec proc2...

那么,如果我只是像这样运行它们,或者它们需要包装在 begin end 或其他内容中,这样可以吗?此外,我有一个批处理文件,我将安排运行它们 -

sqlcmd -S myserver.myserver.com -i 
D:\Scripts\StartProcs.sql -U username -P password -o D:\Scripts\log.txt

上面的脚本正确吗?我的意思是它可以运行它进行测试...但我只需要确保我是否在这里错过了任何其他问题?因为我看到其他一些 sqlcmd 命令非常长并且需要很多参数...我只是想确保我没有丢失任何重要参数..

请使用 .sql 文件或批处理文件纠正我上面的问题...如果我没有考虑到什么问题吗? ...谢谢

EDIT : Problem with the email I get
mailbox unavailable exception!
DBEmail.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Xml.Linq;


public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
    {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        m.From = new System.Net.Mail.MailAddress(Sender);
        m.To.Add(new System.Net.Mail.MailAddress(SendTo));
        m.Subject = Subject;
        m.Body = Body;
        System.Net.Mail.SmtpClient client = null;
        client = new System.Net.Mail.SmtpClient();
        client.Host = mailServer;
        client.UseDefaultCredentials = false;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        client.Send(m);
    }
}

I have to run some nightly stored procedures. So I have created a .sql file which has basically -

exec proc1
exec proc2...

So is it fine if i just run them like that or they need to wrapped in begin end or something else... Fruther I have a batch file which i will schedule to run-

sqlcmd -S myserver.myserver.com -i 
D:\Scripts\StartProcs.sql -U username -P password -o D:\Scripts\log.txt

Is this the above script correct ? I mean it works it tested running it...but i just need to make sure if i am not missing any other issues here ? Because I saw some other sqlcmd commands which where really long and took lot of parameters...I just want to make sure i am not missing any important parameters..

Please correct me above with either .sql file or batch file...if i have not taken in consideration any issues? ...Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-09-18 07:38:14

我看到的唯一问题是,使用这种技术(除非您在过程中内置了某些内容),如果出现问题,或者在这种情况下成功完成,您将不会自动收到通知。

在 SQL Server Express 中,您可以添加一个 CLR 函数,您可以使用该函数向您发送一封电子邮件,报告上述存储过程的结果。

以下是使用所有免费工具的方法。

首先确保通过执行此命令启用 CLR 集成。

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO

您还需要对程序集进行签名或将数据库标记为“可信”。要将 DB 标记为可信,请确保您以 SysAdmin 角色成员的身份登录 SQL Server,然后执行此操作,

ALTER DATABASE YourDBName SET TRUSTWORTHY ON;

然后将以下内容保存为 C:\Code\DBEmail.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
    {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        m.From = new System.Net.Mail.MailAddress(Sender);
        m.To.Add(new System.Net.Mail.MailAddress(SendTo));
        m.Subject = Subject;
        m.Body = Body;

        System.Net.Mail.SmtpClient client = null;
        client = new System.Net.Mail.SmtpClient();
        client.Host = mailServer;

        client.Send(m);
    }
}

然后在相应的 .Net Framework 目录中找到 C# 命令行编译器,在我的例子中是“C:\Windows\Microsoft.NET\Framework\v2.0.50727”) cd 到它并执行它。

csc.exe /target:library /out:C:\Code\DBEmail.dll C:\Code\DBEmail.cs

然后将生成的 C:\Code\DBEmail.dll 复制到 SQL Server(如果尚未使用)并在 Management studio 中执行此操作。

CREATE ASSEMBLY [DBEmail]
AUTHORIZATION [dbo]
From 'C:\Code\DBEmail.dll'
WITH PERMISSION_SET = External_Access

最后通过执行此命令创建一个映射到 CLR 代码的存储过程

CREATE PROCEDURE [dbo].[usp_DBEmail]
    @Sender [nvarchar](255),
    @SendTo [nvarchar](255),
    @Subject [nvarchar](255),
    @Body [nvarchar](max),
    @mailserver [nvarchar](55)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DBEmail].[StoredProcedures].[DBEmail]

然后您可以将代码包装在 Try Catch 中的计划存储过程中,如果发生错误,请在电子邮件中向自己发送错误消息,如下所示。

BEGIN TRY
  --Your SQL Stuff
END TRY
BEGIN CATCH
    Declare @error as nvarchar(max);
    SELECT @error = ERROR_NUMBER();
    Exec usp_DBEmail @Sender='[email protected]', @SendTo='[email protected]', @subject='Whoops', @Body=@error, @mailserver='smtp_ip'
END CATCH;

希望这有帮助!

The only problem I see is that with this technique (unless you have something built into the procs) you won't be automatically notified if something goes wrong, or in that case if it completes successfully.

In SQL Server Express you can add a CLR Function that you can use to send you an email reporting the results of the sprocs above.

And here is how you would do it using all free tools.

First Make sure CLR Integration is enabled by executing this

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'clr enabled', 1;
GO
RECONFIGURE;
GO

You will also need to either sign the assembly or mark the DB as Trustworthy. To mark the DB Trustworthy make sure you are logged into SQL Server as a member of the SysAdmin role and Execute this

ALTER DATABASE YourDBName SET TRUSTWORTHY ON;

Next save the following as C:\Code\DBEmail.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure()]
    public static void DBEmail(string Sender, string SendTo, string Subject, string Body, string mailServer)
    {
        System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
        m.From = new System.Net.Mail.MailAddress(Sender);
        m.To.Add(new System.Net.Mail.MailAddress(SendTo));
        m.Subject = Subject;
        m.Body = Body;

        System.Net.Mail.SmtpClient client = null;
        client = new System.Net.Mail.SmtpClient();
        client.Host = mailServer;

        client.Send(m);
    }
}

Then find the C# Comand line compiler in the appropriate .Net Framework directory, in my case it is "C:\Windows\Microsoft.NET\Framework\v2.0.50727") cd to it and execute this.

csc.exe /target:library /out:C:\Code\DBEmail.dll C:\Code\DBEmail.cs

Then copy the resulting C:\Code\DBEmail.dll to the SQL Server if you are not on it already and in Management studio execute this.

CREATE ASSEMBLY [DBEmail]
AUTHORIZATION [dbo]
From 'C:\Code\DBEmail.dll'
WITH PERMISSION_SET = External_Access

Finally create a sproc to map to the CLR Code by executing this

CREATE PROCEDURE [dbo].[usp_DBEmail]
    @Sender [nvarchar](255),
    @SendTo [nvarchar](255),
    @Subject [nvarchar](255),
    @Body [nvarchar](max),
    @mailserver [nvarchar](55)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DBEmail].[StoredProcedures].[DBEmail]

Then you can just wrap the code inside your scheduled sprocs in Try Catch and if an error occurs send youself the error message in the email like this.

BEGIN TRY
  --Your SQL Stuff
END TRY
BEGIN CATCH
    Declare @error as nvarchar(max);
    SELECT @error = ERROR_NUMBER();
    Exec usp_DBEmail @Sender='[email protected]', @SendTo='[email protected]', @subject='Whoops', @Body=@error, @mailserver='smtp_ip'
END CATCH;

Hope this is helpful!

半衾梦 2024-09-18 07:38:14

连续多个执行官就可以了。多年来我们一直以这种方式进行夜间工作。

考虑在 SQL 代理中安排我们的作业,而不是 Windows 任务计划程序。 SQL Agent 是 SQL Server 的一部分,也是大多数 DBA 期望找到 SQL 工作的地方。 SQL Agent 能够在出现问题时发送错误邮件,并且具有内置的错误日志系统。

但预定的批处理文件也可以正常工作。

Multiple execs in a row is just fine. We've run nightly jobs that way for many years.

Instead of the Windows Task Scheduler, consider scheduling our jobs in SQL Agent. SQL Agent is a part of SQL Server, and it's where most DBA's would expect to find SQL jobs. SQL Agent comes with the ability to send error mails when something goes wrong, and has a built-in error logging system.

But a scheduled batch file will work fine too.

陪你搞怪i 2024-09-18 07:38:14

另一个想法是使用 GO 批处理命令分隔每个执行程序。

例如,

exec proc1<
GO
exec proc2
GO...

这意味着每个 exec 将被单独对待。

Another idea is to separate each exec with the GO batch command.

e.g.

exec proc1<
GO
exec proc2
GO...

This means that each exec will be treated on its own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文