热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Sqlserver中一直在用又经常被忽略的知识点一

已经有快2个月没有更新博客了,实在是因为最近发生了太多的事情,辞了工作,在湘雅医院待了一个多月,然后又新换了工作在平时的工作中,Sqlserver中许多知识点是经常用到的,但是有时候我

已经有快2个月没有更新博客了,实在是因为最近发生了太多的事情,辞了工作,在湘雅医院待了一个多月,然后又新换了工作......

在平时的工作中,Sqlserver中许多知识点是经常用到的,但是有时候我们往往忽略了它们,在过去的一年里,一直使用的是Mysql,现在又开始接触Sqlserver了,所以就把一些常用又容易忽视的Sqlserver知识点总结一点,以便备忘之用。

所有的操作都将基于Northwind数据库来进行操作。

注意:当采用附加Northwind数据库的方式进行数据库迁移的时候,可能会出现错误:“Access is denied”。解决方案: right-click the SQL Server  Management Studio icon and select  Run as administrator。

数据在线Mock地址:https://www.mockaroo.com/

SET NOCOUNT ON 介绍

在存储过程中,经常用到SET NOCOUNT ON;

作用:阻止在结果集中返回显示受T-SQL语句或则usp影响的行计数信息。
当SET NOCOUNT ON 时候,不返回计数,当SET NOCOUNT OFF时候,返回计数。

当SET NOCOUNT ON 时候,会更新@@RowCount,但是不向客户端发送存储过程每个语句的DONE_IN_proc消息。

如果存储过程中包含一些并不返回实际数据的语句,使用SET NOCOUNT ON时,网络通信流量便会大量减少,可以显著提高应用程序性能。

SET NOCOUNT 指定的设置只在执行或运行时候生效,分析时候不生效。

示例:

USE Northwind
GO
SET NOCOUNT OFF;
SELECT TOP 5 OrderDate FROM Orders
GO

执行结果如下:

USE Northwind
GO
SET NOCOUNT ON;
SELECT TOP 5 OrderDate FROM Orders
GO

执行结果如下:

Go 介绍

如果只是执行一条语句,有没有GO都一样。

如果多条语句之间用GO分隔开就不一样了。

每个被GO分隔的语句都是一个单独的事务,一个语句执行失败不会影响其它语句执行。

GO 不是 Transact-SQL 语句;而是可为  SQL Server 查询分析器识别的命令。

如果你的SQL过长的时候,就要写GO,或者有一些语句,它只能是第一句操作的,在之前你也得写 GO ,GO的意思是分批处理语句,有加这个 GO ,就执行GO 行的代码,执行后再执行接下来的代码。

像以下这种情况下就要用到GO ,以达到分批处理数据的目的,否则将会报错。

IF EXISTS(SELECT 1 FROM sys.views WHERE name='View_OrderInfo')
DROP VIEW View_OrderInfo
create view View_OrderInfo
as
select c.ContactName,c.CompanyName,o.OrderDate,o.ShipName,o.OrderID,o.Freight from [Orders] o inner join Customers c on o.CustomerID=c.CustomerID

会报错

必须是:

IF EXISTS(SELECT 1 FROM sys.views WHERE name='View_OrderInfo')
DROP VIEW View_OrderInfo
GO
create view View_OrderInfo
as
select c.ContactName,c.CompanyName,o.OrderDate,o.ShipName,o.OrderID,o.Freight from [Orders] o inner join Customers c on o.CustomerID=c.CustomerID

select count(*)  count(1) count(2) count('a') 之间的区别

count(*):找表中最短的列进行统计行数

count(1) count(2) count('a'):对常数列进行统计行数。它们的执行方式是一样的,没有任何区别。

很显然采用count(1) count(2) count('a')的方式,效率会更高,因为count(*)会先去算出最短的列,然后再去统计。虽然现在的Sqlserver查询分析器自动会帮我们做一些优化,但是我们必须知道它们的实现原理。

WITH (NOLOCK)

缺点:

  1.会产生脏读

  2.只适用于select查询语句

优点:

  1.有些文章说,加了WITH (NOLOCK)的SQL查询效率可以增加33%。

  2.可以用于inner join 或者left join等语句

脏读: 一个用户对一个资源做了修改,此时另外一个用户正好读取了这条被修改的记录,然后,第一个用户放弃修改,数据回到修改之前,这两个不同的结果就是脏读。

详细内容:

  要提升SQL的查询效能,一般来说大家首先会考虑建立索引(index)。其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善在线大量查询的环境中数据集被LOCK的现象藉此改善查询的效能。

不过有一点千万要注意的就是,WITH (NOLOCK)的SQL SELECT有可能会造成Dirty Read(脏读)。

例如:

SELECT o.OrderID,o.OrderDate,o.Freight,d.Quantity,d.UnitPrice
FROM [dbo].[Orders] o WITH (NOLOCK)
JOIN [dbo].[Order Details] d WITH (NOLOCK)
ON o.OrderID=d.OrderID

DELETE、INSERT、UPDATE这些需要transaction的指令就不能使用WITH (NOLOCK)。

加了WITH (NOLOCK)即告诉SQL Server,我们的这段SELECT指令无需去考虑目前table的transaction lock状态,因此效能上会有明显的提升,而且数据库系统的Lock现象会有明显的减少(包含Dead Lock)。

当使用NoLock时,它允许阅读那些已经修改但是还没有交易完成的数据。因此如果有需要考虑transaction事务数据的实时完整性时,使用WITH (NOLOCK)就要好好考虑一下。
如果不需考虑transaction,WITH (NOLOCK)或许是个好用的参考。

Sqlserver高效分页

在SQLserver2012之前,分页我们一般是使用ROW_NUMBER()。以获取第11条到第50条数据为例:

ROW_NUMBER()的实现方式:

select * from (
    select *, ROW_NUMBER() OVER(Order by a.OrderID DESC ) AS RowNumber from Orders as a
  ) as b
  where RowNumber BETWEEN 11 and 50 

SQLserver2012中OFFSET & FETCH实现方式:

SELECT o.* FROM dbo.Orders o
ORDER BY o.OrderID DESC OFFSET 10 ROWS FETCH NEXT 40 ROWS ONLY
OFFSET & FETCH实现方式 更加精简更加高效。
使用 OFFSET-FETCH 中的限制
  • ORDER BY 是使用 OFFSET 和 FETCH 子句所必需的。
  • OFFSET 子句必须与 FETCH 一起使用。永远不能使用 ORDER BY … FETCH。
  • TOP 不能在同一个查询表达式中与 OFFSET 和 FETCH 一起使用。
  • OFFSET/FETCH 行计数表达式可以是将返回整数值的任何算术、常量或参数表达式。该行计数表达式不支持标量子查询
    ALTER PROCEDURE getLightningSendOverTimeOrder --闪电送到期存储过程
    (@OverTimeMinutes INT,--超时时间阈值
    @GetLasterDay int --取最近多少天的订单
    )
    As
    DECLARE @sql NVARCHAR(MAX);
    BEGIN
    set nocount on;
    set @Sql=
    N'WITH    OrderInfo
              --查询近一天的,处理超时的闪电送订单
              AS ( SELECT   ID ,
                            CommunityId ,
                            SubOrderNumber
                   FROM     dbo.[Order] WITH ( NOLOCK )
                   WHERE    DATEDIFF(day, CreatedOn, GETDATE()) <= @GetLasterDay
                            AND DATEDIFF(mi, CreatedOn, ModifiedOn) > @OverTimeMinutes
                            AND LightningSend = 1
                            AND IsDeleted = 0
     --待付款(商品订单),待确认(商品订单),待发货(商品订单),待收货(商品订单)
                            AND OrderStatusId IN (
                            ''C2EE784F-F29B-4E18-8D73-761264339005'',
                            ''FA1A31DF-3855-41BB-9F5C-A09F3AB4C408'',
                            ''ACF190B2-0CA2-43A0-8E6B-70DF8521F4C5'',
                            ''15F06407-C82C-4CA8-984E-37FB3BD9963C'' )
                 ),
           SmsOrder
              --没有发送过信息的订单
              AS ( SELECT   o.ID ,
                            o.CommunityId ,
                            o.SubOrderNumber
                   FROM     OrderInfo o WITH ( NOLOCK )
                            where not EXISTS(SELECT 1 FROM  [dbo].[Sms_LightningSendOrderOverTime] lso
                            WITH ( NOLOCK ) WHERE o.SubOrderNumber = lso.SubOrderNumber)                      
                 ),
            SmsOrderInfo
              AS ( SELECT   
                            so.CommunityId ,
                            so.SubOrderNumber ,
                            p.PublisherId ,
                            c.ConvenienceStoreID ,
                            oi.ProductId ,
                            p.CategoryId ,
                            p.ProcessType
                   FROM     SmsOrder AS so WITH ( NOLOCK )
                            INNER JOIN Community c WITH ( NOLOCK ) ON so.CommunityId = c.ID
                            INNER JOIN dbo.OrderItem oi WITH ( NOLOCK ) ON so.ID = oi.OrderId
                            INNER JOIN dbo.Product p WITH ( NOLOCK ) ON oi.ProductId = p.Id
                                                                  AND p.LightningSend = 1
                                                                   AND p.ProcessType != 1 -- 便利店
                 ),
                MerchantSmsOrderInfo
              AS ( SELECT   
                            so.CommunityId ,
                            so.SubOrderNumber ,
                            p.PublisherId ,
                            c.ConvenienceStoreID ,
                            oi.ProductId ,
                            p.CategoryId ,
                            p.ProcessType
                   FROM     SmsOrder AS so WITH ( NOLOCK )
                            INNER JOIN Community c WITH ( NOLOCK ) ON so.CommunityId = c.ID
                            INNER JOIN dbo.OrderItem oi WITH ( NOLOCK ) ON so.ID = oi.OrderId
                            INNER JOIN dbo.Product p WITH ( NOLOCK ) ON oi.ProductId = p.Id
                                                                  AND p.LightningSend = 1
                                                                  AND p.ProcessType = 1 -- 商家
                 ),
            ResultInfo
            AS ( 
        SELECT   usr.Id AS UserId ,
                            soi.SubOrderNumber ,
                            usr.PhoneNumber
                   FROM     SmsOrderInfo AS soi
                            INNER JOIN dbo.[User] AS usr WITH ( NOLOCK ) ON soi.COnvenienceStoreID= usr.OrgId
                            INNER JOIN dbo.Organization org WITH ( NOLOCK ) ON usr.OrgId = org.Id
                                                                  AND org.Levels = 2 --便利店
                            INNER JOIN OrderDistributaryCategoryRelation AS odcr
                            WITH ( NOLOCK ) ON usr.Id = odcr.MerchantUserId
                                               AND odcr.CategoryType = 1
                                               AND odcr.SellerId = soi.ConvenienceStoreID
                                               AND odcr.CategoryId = soi.CategoryId
                            INNER JOIN OrderDistributary AS odb WITH ( NOLOCK ) ON usr.Id = odb.MerchantUserId
                                                                  AND odb.SellerId = soi.ConvenienceStoreID
                            INNER JOIN OrderDistributaryRelation AS odbr WITH ( NOLOCK ) ON usr.Id = odbr.MerchantUserId
                                                                  AND odbr.MerchantId = soi.ConvenienceStoreID
                   UNION ALL
                   SELECT   u.Id AS UserId ,
                            soi.SubOrderNumber ,
                            u.PhoneNumber
                   FROM     dbo.[User] u WITH ( NOLOCK )
                            INNER JOIN OrderDistributary odb WITH ( NOLOCK ) ON u.Id = odb.MerchantUserId
                                                                AND odb.IsDefault = 1
                            INNER JOIN SmsOrderInfo soi WITH ( NOLOCK ) ON odb.SellerId = soi.ConvenienceStoreID
                 ------------------------------商家-----------------------------
                   UNION ALL
                   SELECT   usr.Id AS UserId ,
                            soi.SubOrderNumber ,
                            usr.PhoneNumber
                   FROM     MerchantSmsOrderInfo AS soi
                            INNER JOIN dbo.[User] AS usr WITH ( NOLOCK ) ON soi.COnvenienceStoreID= usr.OrgId
                            INNER JOIN dbo.Organization org WITH ( NOLOCK ) ON usr.OrgId = org.Id
                                                                  AND org.Levels = 1 --商家
                            INNER JOIN OrderDistributaryCategoryRelation AS odcr
                            WITH ( NOLOCK ) ON usr.Id = odcr.MerchantUserId
                                               AND odcr.CategoryType = 1
                                               AND odcr.CategoryId = soi.CategoryId
                            INNER JOIN OrderDistributary AS odb WITH ( NOLOCK ) ON usr.Id = odb.MerchantUserId
                                                                  AND odb.SellerId = soi.PublisherId
                            INNER JOIN OrderDistributaryRelation AS odbr WITH ( NOLOCK ) ON odcr.MerchantUserId = odbr.MerchantUserId
                                                                  AND odbr.MerchantId = soi.ConvenienceStoreID
                   UNION ALL
                   SELECT   u.Id AS UserId ,
                            soi.SubOrderNumber ,
                            u.PhoneNumber
                   FROM     MerchantSmsOrderInfo soi WITH ( NOLOCK )
                            INNER JOIN OrderDistributary odb WITH ( NOLOCK ) ON odb.SellerId = soi.PublisherId
                                                                AND odb.IsDefault = 1
                                                                AND odb.IsDeleted = 0
                            INNER JOIN dbo.[User] u WITH ( NOLOCK ) ON u.Id = odb.MerchantUserId
                 )
                SELECT  r.SubOrderNumber ,
                r.UserId,
                r.PhoneNumber
        FROM    ResultInfo r '
    exec sp_executesql @Sql,N'@OverTimeMinutes int,@GetLasterDay int',@OverTimeMinutes,@GetLasterDay;
    SET NOCOUNT OFF;
    END
    View Code

自己写的一个存储过程例子

--------------------------------------创建闪电送存储过程-----------------------------------
IF EXISTS(Select Name From Sysobjects Where Name='getLightningSendOverTimeOrder' And Xtype='P')
DROP PROCEDURE [dbo].getLightningSendOverTimeOrder
GO
CREATE PROCEDURE getLightningSendOverTimeOrder --闪电送到期存储过程
(@OverTimeMinutes INT,--超时时间阈值
@GetLasterDay int --取最近多少天的订单
)
As
DECLARE @sql NVARCHAR(MAX);
BEGIN
set nocount on;
set @Sql=
N'WITH    OrderInfo
          --查询近一天的,处理超时的闪电送订单
          AS ( SELECT   ID ,
                        CommunityId ,
                        SubOrderNumber
               FROM     dbo.[Order] WITH ( NOLOCK )
               WHERE    DATEDIFF(day, CreatedOn, GETDATE()) <= @GetLasterDay
                        AND DATEDIFF(mi, CreatedOn, ModifiedOn) > @OverTimeMinutes
                        AND LightningSend = 1
                        AND IsDeleted = 0
 --待付款(商品订单),待确认(商品订单),待发货(商品订单),待收货(商品订单)
                        AND OrderStatusId IN (
                        ''C2EE784F-F29B-4E18-8D73-761264339005'',
                        ''FA1A31DF-3855-41BB-9F5C-A09F3AB4C408'',
                        ''ACF190B2-0CA2-43A0-8E6B-70DF8521F4C5'',
                        ''15F06407-C82C-4CA8-984E-37FB3BD9963C'' )
            UNION ALL
--状态一直是待支付状态的
SELECT ID ,
                        CommunityId ,
                        SubOrderNumber
               FROM     dbo.[Order] WITH ( NOLOCK )
               WHERE    DATEDIFF(day, CreatedOn, GETDATE()) <=@GetLasterDay
                        AND LightningSend = 1
                        AND IsDeleted = 0
                        AND (OrderStatusId =''ACF190B2-0CA2-43A0-8E6B-70DF8521F4C5'' AND DATEDIFF(mi, CreatedOn, GETDATE()) >@OverTimeMinutes)
             ),
       SmsOrder
          --需要发送过信息的订单
          AS ( SELECT   o.ID ,
                        o.CommunityId ,
                        o.SubOrderNumber
               FROM     OrderInfo o WITH ( NOLOCK )
                        where not EXISTS(SELECT 1 FROM  [dbo].[Sms_LightningSendOrderOverTime] lso
                        WITH ( NOLOCK ) WHERE o.SubOrderNumber = lso.SubOrderNumber)                      
             ),
        SmsOrderInfo
          AS ( SELECT   
                        so.CommunityId ,
                        so.SubOrderNumber ,
                        p.PublisherId ,
                        c.ConvenienceStoreID ,
                        oi.ProductId ,
                        p.CategoryId ,
                        p.ProcessType
               FROM     SmsOrder AS so WITH ( NOLOCK )
                        INNER JOIN Community c WITH ( NOLOCK ) ON so.CommunityId = c.ID
                        INNER JOIN dbo.OrderItem oi WITH ( NOLOCK ) ON so.ID = oi.OrderId
                        INNER JOIN dbo.Product p WITH ( NOLOCK ) ON oi.ProductId = p.Id
                                                              AND p.LightningSend = 1
                                                               AND p.ProcessType != 1 -- 便利店
             ),
            MerchantSmsOrderInfo
          AS ( SELECT   
                        so.CommunityId ,
                        so.SubOrderNumber ,
                        p.PublisherId ,
                        c.ConvenienceStoreID ,
                        oi.ProductId ,
                        p.CategoryId ,
                        p.ProcessType
               FROM     SmsOrder AS so WITH ( NOLOCK )
                        INNER JOIN Community c WITH ( NOLOCK ) ON so.CommunityId = c.ID
                        INNER JOIN dbo.OrderItem oi WITH ( NOLOCK ) ON so.ID = oi.OrderId
                        INNER JOIN dbo.Product p WITH ( NOLOCK ) ON oi.ProductId = p.Id
                                                              AND p.LightningSend = 1
                                                              AND p.ProcessType = 1 -- 商家
             ),
        ResultInfo
        AS ( 
    SELECT   usr.Id AS UserId ,
                        soi.SubOrderNumber ,
                        usr.PhoneNumber
               FROM     SmsOrderInfo AS soi
                        INNER JOIN dbo.[User] AS usr WITH ( NOLOCK ) ON soi.COnvenienceStoreID= usr.OrgId
                        INNER JOIN dbo.Organization org WITH ( NOLOCK ) ON usr.OrgId = org.Id
                                                              AND org.Levels = 2 --便利店
                        INNER JOIN OrderDistributaryCategoryRelation AS odcr
                        WITH ( NOLOCK ) ON usr.Id = odcr.MerchantUserId
                                           AND odcr.CategoryType = 1
                                           AND odcr.SellerId = soi.ConvenienceStoreID
                                           AND odcr.CategoryId = soi.CategoryId
                        INNER JOIN OrderDistributary AS odb WITH ( NOLOCK ) ON usr.Id = odb.MerchantUserId
                                                              AND odb.SellerId = soi.ConvenienceStoreID
                        INNER JOIN OrderDistributaryRelation AS odbr WITH ( NOLOCK ) ON usr.Id = odbr.MerchantUserId
                                                              AND odbr.MerchantId = soi.ConvenienceStoreID
               UNION ALL
               SELECT   u.Id AS UserId ,
                        soi.SubOrderNumber ,
                        u.PhoneNumber
               FROM     dbo.[User] u WITH ( NOLOCK )
                        INNER JOIN OrderDistributary odb WITH ( NOLOCK ) ON u.Id = odb.MerchantUserId
                                                            AND odb.IsDefault = 1
                        INNER JOIN SmsOrderInfo soi WITH ( NOLOCK ) ON odb.SellerId = soi.ConvenienceStoreID
             ------------------------------商家-----------------------------
               UNION ALL
               SELECT   usr.Id AS UserId ,
                        soi.SubOrderNumber ,
                        usr.PhoneNumber
               FROM     MerchantSmsOrderInfo AS soi
                        INNER JOIN dbo.[User] AS usr WITH ( NOLOCK ) ON soi.COnvenienceStoreID= usr.OrgId
                        INNER JOIN dbo.Organization org WITH ( NOLOCK ) ON usr.OrgId = org.Id
                                                              AND org.Levels = 1 --商家
                        INNER JOIN OrderDistributaryCategoryRelation AS odcr
                        WITH ( NOLOCK ) ON usr.Id = odcr.MerchantUserId
                                           AND odcr.CategoryType = 1
                                           AND odcr.CategoryId = soi.CategoryId
                        INNER JOIN OrderDistributary AS odb WITH ( NOLOCK ) ON usr.Id = odb.MerchantUserId
                                                              AND odb.SellerId = soi.PublisherId
                        INNER JOIN OrderDistributaryRelation AS odbr WITH ( NOLOCK ) ON odcr.MerchantUserId = odbr.MerchantUserId
                                                              AND odbr.MerchantId = soi.ConvenienceStoreID
               UNION ALL
               SELECT   u.Id AS UserId ,
                        soi.SubOrderNumber ,
                        u.PhoneNumber
               FROM     MerchantSmsOrderInfo soi WITH ( NOLOCK )
                        INNER JOIN OrderDistributary odb WITH ( NOLOCK ) ON odb.SellerId = soi.PublisherId
                                                            AND odb.IsDefault = 1
                                                            AND odb.IsDeleted = 0
                        INNER JOIN dbo.[User] u WITH ( NOLOCK ) ON u.Id = odb.MerchantUserId
             )
            SELECT  r.SubOrderNumber ,
            r.UserId,
            r.PhoneNumber
    FROM    ResultInfo r '
exec sp_executesql @Sql,N'@OverTimeMinutes int,@GetLasterDay int',@OverTimeMinutes,@GetLasterDay;
SET NOCOUNT OFF;
END


GO
View Code

demo2:分页存储过程

------------------------------------------创建我的优惠券存储过程 created by zouqj-2017-3-1-----------------------------------
IF EXISTS(Select Name From Sysobjects Where Name='usp_GetMyCouponsList' And Xtype='P')
DROP PROCEDURE usp_GetMyCouponsList
GO
CREATE PROC usp_GetMyCouponsList
(
@PageIndex int, --页码
@PageSize int, --
@hostName nvarchar(100),--服务器地址
@MemberID UNIQUEIDENTIFIER, --会员ID
@IsGetTotalInfo BIT,  --是否得到统计信息,1为得到统计信息,0为不得到统计信息,返回记录集
@HasStatus BIT, -- 是否有状态
@CouponsStatus INT --优惠券状态
)
AS
----------------------------------------定义参数--------------------------------------
declare @strSql NVARCHAR(max);
DECLARE @IsUseCount INT
DECLARE @ExpiredCount INT
DECLARE @NoUseCount INT
DECLARE @where NVARCHAR(100);
set nocount on;
----------------------------------------SQL开始--------------------------------------
IF @HasStatus=1
BEGIN
SET @where='m.[State]=@CouponsStatus';
END
ELSE
BEGIN
SET @where='1=1'
END
IF @IsGetTotalInfo=0
BEGIN
SET @strSql=N'SELECT DISTINCT t.* FROM (SELECT m.[State] AS [STATUS],
m.CouponCode AS CouponsCode,
m.CampaignID AS CampaignID,
c.Code as CampaignCode,
c.Name AS CampaignName,
b.ValidityStartTime AS CampaignStartTime,
b.ValidityEndTime AS CampaignEndTime,
c.Price as CouponsAmount,
(CASE WHEN c.IsLimited =1 THEN 1 ELSE 2 END  ) as IsLimited,
c.FullAmount as MinAmount,
e.[Type] AS CouponsType,
(CASE WHEN b.IconUrl IS NULL THEN N'''' WHEN b.IcOnUrl=N'''' THEN N'''' ELSE @hostName+b.IconUrl END) AS IconUrl

FROM  Loy_MemberCoupon m WITH ( NOLOCK ) INNER JOIN 
 [dbo].[MK_Campaign] AS a WITH ( NOLOCK ) ON m.CampaignID=a.ID AND m.MemberID=@MemberID AND '+@where+'
        LEFT OUTER JOIN [dbo].[MK_CouponConfig] AS b  WITH ( NOLOCK ) ON a.[ID] = b.[CampaignID] 
        LEFT OUTER JOIN [dbo].[MK_Coupon] AS c WITH ( NOLOCK ) ON b.[CouponID] = c.[ID]
        LEFT OUTER JOIN [dbo].[MK_CouponRestriction] AS d  WITH ( NOLOCK ) ON c.[ID] = d.[CompainID]
        LEFT OUTER JOIN [dbo].[MK_CouponRestrictCategory] AS e WITH ( NOLOCK ) ON d.[ID] = e.[CouponRestrictionID]
        ) t
        ORDER BY t.CampaignEndTime
offset (@PageIndex-1)*@PageSize  ROWS  FETCH NEXT @PageIndex*@PageSize ROWS ONLY'
exec sp_executesql @strSql,N'@PageIndex int,@PageSize int,@hostName nvarchar(100),@MemberID UNIQUEIDENTIFIER,@where NVARCHAR(100),@CouponsStatus int',@PageIndex=@PageIndex,@PageSize=@PageSize,@hostName=@hostName,@MemberID=@MemberID,@where=@where,@CouponsStatus=@CouponsStatus
END
ELSE
BEGIN
-------------------------------------------------------------
SELECT @IsUseCount=COUNT(1) FROM (SELECT DISTINCT m.CouponCode
FROM  Loy_MemberCoupon m WITH ( NOLOCK ) WHERE m.MemberID=@MemberID AND m.[State]=2
        ) t
SELECT @ExpiredCount=COUNT(1) FROM (SELECT DISTINCT m.CouponCode
FROM  Loy_MemberCoupon m WITH ( NOLOCK ) WHERE m.MemberID=@MemberID AND m.[State]=3
        ) t
SELECT @NoUseCount=COUNT(1) FROM (SELECT DISTINCT m.CouponCode
FROM  Loy_MemberCoupon m WITH ( NOLOCK ) WHERE m.MemberID=@MemberID AND m.[State]=1
        ) t
SELECT @IsUseCount AS IsUseCount,@ExpiredCount AS ExpiredCount,@NoUseCount AS NoUseCount
END
----------------------------------------SQL结束--------------------------------------
set nocount off;
View Code

推荐阅读
  • SQLite是一种轻量级的关系型数据库管理系统,尽管体积小巧,却能支持高达2TB的数据库容量,每个数据库以单个文件形式存储。本文将详细介绍SQLite在Android开发中的应用,包括其数据存储机制、事务处理方式及数据类型的动态特性。 ... [详细]
  • 深入解析轻量级数据库 SQL Server Express LocalDB
    本文详细介绍了 SQL Server Express LocalDB,这是一种轻量级的本地 T-SQL 数据库解决方案,特别适合开发环境使用。文章还探讨了 LocalDB 与其他轻量级数据库的对比,并提供了安装和连接 LocalDB 的步骤。 ... [详细]
  • 本文详细介绍了Oracle RMAN中的增量备份机制,重点解析了差异增量和累积增量备份的概念及其在不同Oracle版本中的实现。通过对比两种备份方式的特点,帮助读者选择合适的备份策略。 ... [详细]
  • 定制数据层关键字:数据层,访问,元数据,数据访问模型http://www.gaodaima.com/35448.html定制数据层_sqlserver ... [详细]
  • SqlServer分区表概述(转载)
    什么是分区表一般情况下,我们建立数据库表时,表数据都存放在一个文件里。但是如果是分区表的话,表数据就会按照你指定的规则分放到不同的文件里,把一个大的数据文件拆分为多个小文件,还可以把这些小文件 ... [详细]
  • 本文详细介绍如何在Spring Boot项目中集成和使用JPA,涵盖JPA的基本概念、Spring Data JPA的功能以及具体的操作步骤,帮助开发者快速掌握这一强大的持久化技术。 ... [详细]
  • 设计模式系列-原型模式
    一、上篇回顾上篇创建者模式中,我们主要讲述了创建者的几类实现方案,和创建者模式的应用的场景和特点,创建者模式适合创建复杂的对象,并且这些对象的每个组成部分的详细创建步骤可以是动态的变化的,但 ... [详细]
  • 抽象工厂模式 c++
    抽象工厂模式包含如下角色:AbstractFactory:抽象工厂ConcreteFactory:具体工厂AbstractProduct:抽象产品Product:具体产品https ... [详细]
  • 本文详细介绍了在 Windows 7 上安装和配置 PHP 5.4 的 Memcached 分布式缓存系统的方法,旨在减少数据库的频繁访问,提高应用程序的响应速度。 ... [详细]
  • 构建Python自助式数据查询系统
    在现代数据密集型环境中,业务团队频繁需要从数据库中提取特定信息。为了提高效率并减少IT部门的工作负担,本文探讨了一种利用Python语言实现的自助数据查询工具的设计与实现。 ... [详细]
  • 1.3.4ProfilerSQLServerProfiler是一个图形化的管理工具用于监督记录和检查SQLServer数据库的使用情况对系统管理员来说它是一个监视用户活动的间谍1. ... [详细]
  • 微软平台的软件开发系统中,有着一套自己的约定规则。熟悉.net开发的都会对异常处理不陌生,现阶段的各种编程语言中,都不乏异常处理机制,个中原理也都大同小异。sqlserver在批处 ... [详细]
  • 在目标队列中对消息进行排队时出现异常。错误:15404,状态:19。CouldnotobtaininformationaboutWindowsNTgroupuserSERVER ... [详细]
  • 本文主要介绍关于数据库,sql,sqlserver的知识点,对【数据库——概述】和【数据库到底要怎么做】有兴趣的朋友可以看下由【用编程写诗】投稿的技术文章,希望该技术和经验能帮到你解决你所遇的数据库相 ... [详细]
  • SQLServer2008全套 数据库简介,分离,收缩,快照
    第三课视频笔记:联机丛书的使用附加和分离数据库---收缩数据库--------数据库快照 ... [详细]
author-avatar
sdfqwerwfds
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有