select top 后面不能跟变量吗?

Oceanson 2007-04-18 11:49:26
需求:
实现一个简单的分页存储过程。不使用通用存储过程,不到万不得已不使用存储过程内的字符串拼接。
实现:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[sp_QueryPage_User]
@Page int = 1,
@PageSize int = 10
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

select top @PageSize * from tbl_User
where (UserNativeId not in (select top @Page*@PageSize UserNativeId from tbl_User order by UserNativeId))
order by UserNativeId
END
go

错误提示:
Msg 102, Level 15, State 1, Procedure sp_QueryPage_User, Line 15
Incorrect syntax near '@PageSize'.
Msg 102, Level 15, State 1, Procedure sp_QueryPage_User, Line 16
Incorrect syntax near '@Page'.

分析: 将变量替换为常量后可以通过
例如将
select top @PageSize * from tbl_User
where (UserNativeId not in (select top @Page*@PageSize UserNativeId from tbl_User order by UserNativeId))
order by UserNativeId
替换为
select top 10 * from tbl_User
where (UserNativeId not in (select top 30 UserNativeId from tbl_User order by UserNativeId))
order by UserNativeId

求解: select top 后面不能跟变量?还是有什么别的语法可以通过的?
...全文
1650 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yingzhilian2008 2009-01-22
  • 打赏
  • 举报
回复
select top (变量或者表达式) * from tb
delete top (变量或者表达式) tb
lanxia39 2009-01-21
  • 打赏
  • 举报
回复
对于简单的sql楼上是正解,但对于
分页存储过程和多层嵌套的sql就无能为力了
qazzxc2008 2008-12-19
  • 打赏
  • 举报
回复
我晕,还有人问这个问题。你直接用SET rowcount @rowcount 就行了。sql2000,我测试了,没问题的
lddp034 2008-04-11
  • 打赏
  • 举报
回复
我也遇到这个问题,现在也没解决
真没想到SQL2000竟然不支持TOP 后变量
zjcxc 元老 2007-04-18
  • 打赏
  • 举报
回复
sql 2005支持.

如果你用sql 2005, 那可以写:

select top (变量或者表达式) * from tb
delete top (变量或者表达式) tb
Oceanson 2007-04-18
  • 打赏
  • 举报
回复
感谢各位捧场
各位的解决方案基本上都围绕着使用字符串拼接后使用exec(@sqls) 来 执行
我的需求中有提到尽量不要使用这种方法,因为曾经我执行拼接字符串出现莫名的错误。
但如果select top 语法不支持变量 那我也只能屈就了 :(
不知道还有那位高手有别的招数
brio8425 2007-04-18
  • 打赏
  • 举报
回复
用动态SQL语句。
OracleRoob 2007-04-18
  • 打赏
  • 举报
回复
用动态SQL语句。




动态sql语句基本语法
1 :普通SQL语句可以用Exec执行

eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N

2:字段名,表名,数据库名之类作为变量时,必须用动态SQL

eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格

当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名

declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确

3. 输出参数
declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?

declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num

leo_lesley 2007-04-18
  • 打赏
  • 举报
回复
----------例子-------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[sp_QueryPage_User]
@Page int ,
@PageSize int
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @str varchar(1000)
select @str = ''
select @str = 'select top '+rtrim(@PageSize)+' * from sysobjects where id not in (select top '+rtrim(@Page*@PageSize)+' id from sysobjects order by id) order by id'
exec(@str)
END
go


exec [dbo].[sp_QueryPage_User] 1,10
exec [dbo].[sp_QueryPage_User] 2,10
----------------例子结束---------------------


--------------------你的存储过程------------------

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[sp_QueryPage_User]
@Page int ,
@PageSize int
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @str varchar(1000)
select @str = ''
select @str = 'select top '+RTRIM(@PageSize)+' * from tbl_User where (UserNativeId not in (select top '+RTRIM(@Page*@PageSize)+' UserNativeId from tbl_User order by UserNativeId)) order by UserNativeId'
exec(@str)
END
go

leo_lesley 2007-04-18
  • 打赏
  • 举报
回复
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

CREATE PROCEDURE [dbo].[sp_QueryPage_User]
@Page int = 1,
@PageSize int = 10
AS
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @str varchar(1000)
select @str = ''
select @str = 'select top '+@PageSize+' * from tbl_User where (UserNativeId not in (select top '+@Page*@PageSize+' UserNativeId from tbl_User order by UserNativeId)) order by UserNativeId'
exec(@str)
END
go
leo_lesley 2007-04-18
  • 打赏
  • 举报
回复
declare @num int
select @num = 10
exec('select top '+@num+' * from sysobjects')
w75251455 2007-04-18
  • 打赏
  • 举报
回复
exec('select top '+@int+' * form sysobjects')
..........
xikboy 2007-04-18
  • 打赏
  • 举报
回复
CREATE PROCEDURE [dbo].[sp_QueryPage_User]
@Page int = 1,
@PageSize int = 10
AS
declare @sql varchar(1000)
BEGIN

-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
set @sql='
select top '+@PageSize+' * from tbl_User
where (UserNativeId not in (select top '+@Page*@PageSize+' UserNativeId from tbl_User order by UserNativeId))
order by UserNativeId'
exec(@sql)
END
go
w75251455 2007-04-18
  • 打赏
  • 举报
回复
declare @int int
set @int =1
exec('select top '+@int+' form sysobjects')

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧