我有一个名为Product的表.
产品表有更多记录,有些时候表没有记录.
所以我想查看产品表,
if it's have the records i send all table information as mail.
if it's not have record no need to send mail.
Run Code Online (Sandbox Code Playgroud)
请帮我.
小智 59
这是一个可能的选择.以下示例可能会让您了解如何使用通过电子邮件发送结果集Send Email task.此示例显示如何遍历查询结果集以形成将随后通过电子邮件发送的邮件正文Send Email task.
如果您不希望在结果集为空时发送电子邮件,则可以Expression在循环结果集和发送电子邮件任务之间添加优先约束.
该示例使用SSIS 2008 R2和SQL Server 2008 R2数据库.
分步过程:
dbo.EmailData使用SQL Scripts部分下提供的脚本创建一个名为的表.
屏幕截图#1显示了该示例中Execute SQL任务将在电子邮件中查询和发送的示例数据.
在SSIS包上,创建5个变量,如屏幕截图#2所示.
在SSIS包,请将以下任务:Execute SQL task,Foreach loop container,Script task在内部的foreach循环容器和Send Email task.
配置Execute SQL task如屏幕截图#3和#4所示.
配置Foreach loop container如屏幕截图#5和#6所示."变量映射"部分显示查询结果列的显示顺序以及如何将它们分配给SSIS变量.这些变量将用于在其中形成电子邮件消息Script task.
在Script task,使用脚本任务代码部分下显示的代码替换代码.脚本任务具有非常简单的纯文本电子邮件格式.
配置发送电子邮件任务,如屏幕截图#7所示.您需要在From和To字段中使用有效的电子邮件地址对其进行配置.
配置控制流任务后,您的包应该如屏幕截图#8所示.
示例包执行显示在屏幕截图#9中.
包裹发送的电子邮件显示在屏幕截图#10中.某些信息已从屏幕截图中删除.您可以将屏幕截图#1中显示的表数据与此电子邮件输出进行比较,它们应该相同.
希望有所帮助.
SQL脚本: .
CREATE TABLE [dbo].[EmailData](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ItemId] [varchar](255) NOT NULL,
[ItemName] [varchar](255) NOT NULL,
[ItemType] [varchar](255) NOT NULL,
[IsProcessed] [bit] NULL,
CONSTRAINT [PK_EmailData] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO
Run Code Online (Sandbox Code Playgroud)
脚本任务代码:
只能使用的C#代码SSIS 2008 and above..
/*Microsoft SQL Server Integration Services Script Task
Write scripts using Microsoft Visual C# 2008.
The ScriptMain is the entry point class of the script.
*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_7f59d09774914001b60a99a90809d5c5.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
Variables varCollection = null;
string header = string.Empty;
string message = string.Empty;
Dts.VariableDispenser.LockForWrite("User::EmailMessage");
Dts.VariableDispenser.LockForWrite("User::ItemId");
Dts.VariableDispenser.LockForWrite("User::ItemName");
Dts.VariableDispenser.LockForWrite("User::ItemType");
Dts.VariableDispenser.GetVariables(ref varCollection);
//Set the header message for the query result
if (varCollection["User::EmailMessage"].Value == string.Empty)
{
header = "Execute SQL task output sent using Send Email Task in SSIS:\n\n";
header += string.Format("{0}\t{1}\t\t\t{2}\n", "Item number", "Item name", "Item type");
varCollection["User::EmailMessage"].Value = header;
}
//Format the query result with tab delimiters
message = string.Format("{0}\t{1}\t{2}",
varCollection["User::ItemId"].Value,
varCollection["User::ItemName"].Value,
varCollection["User::ItemType"].Value);
varCollection["User::EmailMessage"].Value = varCollection["User::EmailMessage"].Value + message;
Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Run Code Online (Sandbox Code Playgroud)
截图#1:

截图#2:

截图#3:

截图#4:

截图#5:

截图#6:

截图#7:

截图#8:

截图#9:

截图#10:

| 归档时间: |
|
| 查看次数: |
67625 次 |
| 最近记录: |