公司的销售管理人员日常想要接收订单在$10,000元以上的电子邮件,同时也很愿意感谢来信订购的用户。电子邮件的主体要包括所有必要的信息,因此就没有必要访问公司客户/服务器程序。第一步是近可能地委派数据库服务器的数据。在这里的例子中,我选择了随SQL Server 7.0安装后随带的Northwind数据库。
原文出处:http://www.15seconds.com/issue/991007.htm
我决定在Northwind数据库中建立一个视图,它将提供所需要的一切。启动SQL Server Enterprise管理器,检查一下Northwind数据库下的视图列表,Order Subtotals视图就在眼前,它计算所有放置在Northwind数据库中的定单总和。我决定在新视图10K_Order_Qry中引用这个视图,如下:
CREATE VIEW dbo.[10K_Order_Qry]
AS
SELECT [order subtotals].OrderID, CONVERT(varchar(15),
[order subtotals].Subtotal) AS 'Subtotal', CONVERT(char(10),
Orders.OrderDate, 110) AS 'OrderDate',
Customers.CompanyName, Customers.ContactName,
Customers.Country, Customers.Phone
FROM [order subtotals], Orders, Customers
WHERE [order subtotals].OrderID = Orders.OrderID AND
Orders.CustomerID = Customers.CustomerID AND
subtotal >= 10000
-- Below lines can be switched in order to look for today's sales over $10,000
-- (comment the next line, and uncomment the second line)
AND OrderDate >= '02-01-1998' AND OrderDate <= '02-28-1998'
--AND OrderDate = convert(char(10),getdate(),110)
注意:在视图中引用另外一个视图不是一个好的方法。但是,这里对此不做详细地讨论。
为了验证输出的报告所需要的所有字段是否准备好,我们可以在SQL Server Query Analyzer中执行这个视图进行测试。
我们大多数人,在相当多的时候,都在MS-DOS批处理文件中使用过脚本。融入Windows 98、Internet Information Server 4.0、Windows NT Workstation 2000和Windows NT Server 2000中的微软Windows Scripting Host是一个独立语言的脚本引擎。Visual Basic和JavaScript脚本引擎也被包含在Windows Scripting Host中。
现在准备编写脚本来建立一个报告,并且将它发送电子邮件给服务商管理者。我决定以Visual Basic Scripting语言编写这个脚本,可以使用任何文字编辑器来创建它。唯一的要求是以“vbs”扩展名来保存文件,比如
“MyScript.vbs”。如果安装了Windows NT Option Pack 4,就存在了脚本调试器MSSCRDBG.EXE,它能被用做创建和调试脚本。它远远强于Notepad!
Dim objSendMail
Dim strTo, strFrom
Dim strSubject, strBody
Dim shipUic
' mail constants
Const CdoBodyFormatType = 0 ' Body property is HTML
Const CdoMailFormatType = 0 ' NewMail object is in MIME format
Const CdoNormal = 1 ' Normal importance (default)
strFrom = "admin@northwind.com" ' System administrator or DBA mail account
strTo =" manager@northwind.com" ' Recipient mail account - i.e. Sales Manager
strSubject = "Sales over $10,000" ' Mail subject
' Call function to build the HTML mail body
strBody = MailBody()
' The following section creates the E-mail object and sends the mail
Set objSendMail = CreateObject("CDONTS.NewMail")
Dim oConn
Dim oCmd
Dim oRs
Dim tmpBody
set oConn = CreateObject("ADODB.Connection")
oConn.Open("DATABASE=Northwind;DSN=Northwind;UID=sa;Password=;")
set oCmd = CreateObject("ADODB.Command")
oCmd.ActiveConnection = oConn
oCmd.CommandText = "select * from Northwind.dbo.[10k_order_qry] order by subtotal desc"
oCmd.CommandType = 1
oCmd.Prepared = True
set oRs = oCmd.Execute
ORDER ID SUBTOTAL COMPANY CONTACT COUNTRY PHONE
10865 $16387.50 QUICK-Stop Hors t Kloss Germany 0372-035188
10889 $11380.00 Rattlesnake Canyon Grocery Paula Wilson USA (505) 555-5939
10897 $10835.24 Hungry Owl All-Night Grocers Patricia McKenna Ireland 2967 542