sql2005与sql2000的语法区别

这篇博客对比了SQL2005和SQL2000在创建存储过程时的语法差异,特别是在使用TOP关键字与参数结合时的处理方式。在SQL2005中,可以直接在TOP后使用变量,而在SQL2000中则需要通过动态SQL实现。此外,还提到了SQL2000中变量使用时字符串的处理注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.sql2005中的存储过程

CREATE PROCEDURE [dbo].[GetNewsAllPage]
 -- Add the parameters for the stored procedure here
  (@PageIndex int,
      @PageSize int)
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
  if (@PageIndex=1)
   begin
       select TOP(@PageSize) * from TableNews order by NewsId desc
   end
 else
   begin
    Select TOP (@PageSize) * from TableNews where (NewsId <
    (Select Min(NewsId) From
     (Select TOP ((@PageIndex-1)*@PageSize) NewsId from TableNews order by NewsId desc)
    as T))
    order by NewsId desc
   end
END
2.sql2000中的存储过程

在SQL2000中,Select Top后是不能直接更变量的,例如:

ALTER PROCEDURE [dbo].[GetNewsAllPage]
 -- Add the parameters for the stored procedure here
 (@PageIndex int,
      @PageSize int)
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
    declare @sql varchar(500),@count int
--    set @sql = 'select top ' + cast(@i as varchar) + ' * from table'

    -- Insert statements for procedure here
 if (@PageIndex=1)
   begin
       set @sql='select top '+cast(@PageSize as varchar)+' * from  TableNews order by NewsId desc'
      -- print @sql
exec(@sql)
--       select TOP(@PageSize)* from TableNews order by NewsId desc
   end
 else
   begin
    set @count=(@PageIndex-1)*@PageSize
    set @sql=
    'Select TOP '+cast(@PageSize as varchar)+' * from TableNews where (NewsId <
    (Select Min(NewsId) From
     (Select TOP '+cast(@count as varchar)+' NewsId from TableNews order by NewsId desc)
    as T))
    order by NewsId desc'
--print @sql
    exec(@sql)
   end
END

3.另外在sql2000中变量的使用也应该注意'' 的使用

ALTER PROCEDURE [dbo].[GetLeaveWordPages]
 -- Add the parameters for the stored procedure here
  (@PageIndex int,
      @PageSize int)
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
   declare @b varchar(50)
   declare @sql varchar(500),@count int
   set @b='True'
  
    -- Insert statements for procedure here
 if (@PageIndex=1)
   begin
       set @sql='select TOP '+cast(@PageSize as varchar)+' * from TableLeaveWord where ShowWord='''+@b+''' order by LeaveWordId desc'
       exec(@sql)
   end
 else
   begin
     set @count=(@PageIndex-1)*@PageSize
     set @sql=
    'Select TOP '+cast(@PageSize as varchar)+' * from TableLeaveWord where (LeaveWordId <
    (Select Min(LeaveWordId) From
     (Select TOP '+cast(@count as varchar)+' LeaveWordId from TableLeaveWord where ShowWord='''+@b+''' order by LeaveWordId desc)
    as T))
    order by LeaveWordId desc'
    exec(@sql)
   end
  
END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值