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

MSSQL
如何在 SQL SERVER 中快速有条件删除海量数据
Transact_SQL 小手册
一个过滤重复数据的 SQL 语句
MS-SQL Server 2005 其中三个版本的比较
SQL Server 的 SQL 语句导入导出大全
一些 T-SQL 技巧
显示 Sql Server 中所有表中的信息
MS-SQL Server 中单引号的两种处理方法
sql数据库不能直接用instr函数
MDF文件在SQL Server中的恢复技术
SQL Server 中调整自增字段的当前初始值
如何创建SQL Server 2000故障转移群集
SQL Server连接失败错误及解决
最长用最基本的MSSQL数据库备份与还原
清除SQLServer日志的两种方法
如何强制删除或恢复SQLServer正在使用的数据库
如何恢复数据库备份到一个已存在的正在使用的数据库上
设定sql server定期自动备份数据库
xp_cmdshell开启与关闭
更改SQL Server更改当前数据库的所有者:sp_changedbowner

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 52 ::
收藏到网摘: 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数据库中没有大量的行,但我认为这种技巧可以得到很好地扩充。更为重要的是,我喜欢它提供的粒度。我能够做我想做的,并按要求重新使用或重新组合原子。