当前位置: 首页 > 图文教程 > 数据库 > MSSQL > 如何在SQL Server中由原子建立分子查询

MSSQL
SQL Server导出表到EXCEL文件的存储过程
如何利用触发器实现两个数据库间的同步
SQL Server数据库连接查询的种类及其应用
SQL Server 2005中利用xml拆分字符串序列
Microsoft SQL Server数据库SA权限总结
用SQL语句生成带有小计合计的数据集脚本
深入剖析SQL Server的六种数据移动方法
为导入文件加上时间戳标记的两种方法
SQL Server与Oracle、DB2的优劣对比
带你深入了解SQL Server 2008的独到之处
通过视图修改数据时所应掌握的基本准则
SQL Server中如何优化磁带备份设备性能
教你轻松解决几种常见的SQL疑难问题
怎样取得局域网中所有SQL Server的实例
在系统崩溃的时候如何恢复原有的数据
获得当前数据库对象依赖关系的实用算法
SQL Server如何才能访问Sybase中的表
如何才能保护好我们的SQL Server数据库
从两个方面讲解SQL Server口令的脆弱性
SQL Server数据库日志清除的两个方法

MSSQL 中的 如何在SQL Server中由原子建立分子查询


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 106 ::
收藏到网摘: n/a

每位SQL Server开发员都有自己的首选操作方法。我的方法叫做分子查询。这些是由原子查询组合起来的查询,通过它们我可以处理一个表格。将原子组合起来,可以建立分子。当然也会有限制(化学家所称的化合价),但一般来说,这个原理还是适用的。

在本文中,我将探讨这种策略的几种变化。我从最基本的内容开始(即最详细的内容),然后逐步深化。为让你了解这种方法的灵活性,我会在不同层次使用几种技巧。(警告:这并不是唯一的解决方法,我只是在讨论一些可行的选择。)

我从普遍使用的数据库Northwind开始(虽然为了保留原貌,我把它复制到Northwind_New中,实际这才是我使用的数据库。)在我的拷贝中,我做出这些重要的修改。

我删除了复合主键,增加一个叫做PK的新列,并将其设为Identity列。

我增加了一个称作ExtendedAmount的计算列。

以下为引用的内容:

USE [Northwind_New]
GO
/****** Object: Table [dbo].[OrderDetails_New]
Script Date: 08/23/2006 16:15:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATETABLE [dbo].[OrderDetails_New](
[OrderDetailID] [int] IDENTITY(1,1)NOTNULL,
[OrderID] [int] NOTNULL,
[ProductID] [int] NOTNULL,
[UnitPrice] [money] NOTNULL,
[Quantity] [smallint] NOTNULL,
[Discount] [real] NOTNULL,
[ExtendedAmount] AS([Quantity] * [UnitPrice] *(1 - [Discount])),
CONSTRAINT [PK_OrderDetails_New] PRIMARYKEYCLUSTERED
(
[OrderDetailID] ASC
)ON [PRIMARY]
)ON [PRIMARY]

列表A

列表A中包含建立这个新表格的新建(Create)语句。我用这个命令将它和值从原始表格中迅速移植出来:

INSERT INTO [Northwind_New].[dbo].[OrderDetails_New]

SELECT * FROM [Northwind_New].[dbo].[Order Details]
 
既然我有一个多行的表格,是时候开始实验了。(一旦新列ExtendedAmount被计算,其值会自动移植。)最初,我想用OrderID计算ExtendedAmount列的总数。我建立一个视图,如列表B所示。

以下为引用的内容:

USE [Northwind_New]
GO
/****** Object:?View [dbo].
[OrderDetailsSumByOrderID_vue]Script Date: 08/23/2006 16:31:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[OrderDetailsSumByOrderID_vue]
AS
SELECT TOP 100 PERCENT OrderID,
SUM(ExtendedAmount) AS TotalAmount
FROM?dbo.OrderDetails_New
GROUP BY OrderID
ORDER BY OrderID

列表B

我每行得到一个OrderID,ExtendedAmount的列数得到总计。(称之为OrderAmount,将航运,税收等相加得到OrderTotal)。

我会在等式中引入一个表格UDF,如列表C所示。注意,我可以用两种方法调用函数:提交CustomerID将表格限定为那个顾客的订单,或提交一个NULL获得所有顾客订单列表。如果我提交CustomerID,那么订单列表就出现在OrderDate中;如果我提交NULL,列表就被分组并在CustomerID中由OrderDate排序。

以下为引用的内容:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

-- =============================================
-- Author:        Arthur Fuller
-- Create date: 23 Aug 2006
-- Description:   Table UDF to return Order Totals by Customer
-- Example: SELECT * FROM dbo.CustomerOrderTotals_fnt('VINET')
-- Example: SELECT * FROM dbo.CustomerOrderTotals_fnt(NULL)
-- Notes:       This udf is designed to serve two
-- purposes. Pass a CustomerID to limit the rows to
-- that customer, or pass nothing to get all customers
-- =============================================
ALTERFUNCTION [dbo].[CustomerOrderTotals_fnt]
(    
      -- Add the parameters for the function here
      @CustomerID varchar(5)=NULL
)
RETURNS TABLE
AS
RETURN
    (
      -- Add the SELECT statement with parameter references here
    SELECTTOP 100 PERCENT
        dbo.Customers.CustomerID,
        dbo.Customers.CompanyName,
        dbo.Orders.OrderID,
        dbo.Orders.OrderDate,
        dbo.OrderDetailsSumByOrderID_vue.TotalAmount
    FROM  
        dbo.Customers
    INNERJOIN
        dbo.Orders ON dbo.Customers.CustomerID = dbo.Orders.CustomerID
    INNERJOIN
        dbo.OrderDetailsSumByOrderID_vue
        ON dbo.Orders.OrderID = dbo.OrderDetailsSumByOrderID_vue.OrderID
    WHERE 
        dbo.Customers.CustomerID = @CustomerID
        OR @CustomerID ISNULL
    ORDERBY dbo.Orders.OrderDate
    )

--try it with these:
--SELECT * FROM dbo.CustomerOrderTotals_fnt('VINET')
--SELECT * FROM dbo.CustomerOrderTotals_fnt(NULL)
 
列表C

-- ================================================
-- Template generated from Template Explorer using:
-- Create Inline Function (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the function.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        Arthur Fuller
-- Create date: 23 Aug 2006
-- Description:   Return total sales by Customer
-- Example:
--  SELECT CustomerID, CustomerTotal
--      FROM DBO.CustomerGrandTotal_fnt(null)
--  SELECT CustomerID, CustomerTotal
--      FROM DBO.CustomerGrandTotal_fnt('VINET')
--  SELECT CustomerID, CustomerTotal
--      FROM DBO.CustomerGrandTotal_fnt('VINET')
-- =============================================
CREATEFUNCTION CustomerGrandTotal_fnt
(    
      @CustomerID varchar(5)
)
RETURNS TABLE
AS
RETURN
(
      -- Add the SELECT statement with parameter references here
    SELECTTOP 100 PERCENT
        CustomerID,
        SUM(TotalAmount)AS CustomerTotal
    FROM dbo.CustomerOrderTotals_fnt(@CustomerID)
    WHERE dbo.CustomerOrderTotals_fnt.CustomerID = @CustomerID
        OR @CustomerID ISNULL
    GROUPBY CustomerID
    ORDERBY CustomerID

)
GO

 
列表D

更深入一步,我每个顾客打印一行,用一列显示每名顾客的总销售额。查看列表D。并不是所有项目都得到组合。在最外层,我能够以两种方式调用这个函数:提交合法CustomerID或UNLL。以下是它们各自的实例:

以下为引用的内容:

SELECT CustomerID, CustomerTotal

FROM DBO.CustomerGrandTotal_fnt(null)

SELECT CustomerID, CustomerTotal

FROM DBO.CustomerGrandTotal_fnt('VINET')

现在你有了一组精确的工具来帮助你挖掘适当层次的细节。计算列OrderDetails.ExtendedAmount隐藏了一个复杂层次,从那我又深入到我希望隐藏的细节。这就是我把这些对象叫做原子和分子查询的原因。我使用“查询”一词来概括这样一个事实,即讨论的对象是视图还是表格UDF并不重要。(虽然由于命名规则,suffix _fnt表示一个表格UDF,而suffix _vue表示一个视图。)

必须承认,Northwind数据库中没有大量的行,但我认为这种技巧可以得到很好地扩充。更为重要的是,我喜欢它提供的粒度。我能够做我想做的,并按要求重新使用或重新组合原子。