存储过程中将sql语句结果赋值给变量
2010-07-26 14:56
447 查看
create procedure procname
AS
declare @recount int
select @recount=count(id) from school
--最关键在这句
select @recount=count(id) from school
select @recount=select count(id) from school -- 错误:这里就不再需要select了
set @recount=count(id) from school -- 错误这里只能用select不能用set
如果school为一个@chart参数的话,上面方法就不适用了
应该为:
declare @getRecordSql nvarchar(100)
set @getRecordSql='select @recordcount=count(1) from '+@chart
exec sp_executesql @getRecordSql,N'@recordCount int output',@recordCount output
N'@recordCount int output',@recordCount output
做一个参数的替换,如果测试一下:print @recordCount的话就会显示行数
//示例:选择最新的5条数据从20条开始
execute A_School_getNew 5,20
--存储过程
create procedure A_School_getNew
@Size int,
@StartIndex int
AS
declare @recount int
declare @Sql nvarchar(120)
select @recount=count(id) from school
set @Sql='select top '+convert(nvarchar,@Size)+'id,school_name from school where('+
'ID not in(select top '+convert(nvarchar,@recount-@startIndex+@size)+
' id from school)) order by id desc';
execute (@Sql)
AS
declare @recount int
select @recount=count(id) from school
--最关键在这句
select @recount=count(id) from school
select @recount=select count(id) from school -- 错误:这里就不再需要select了
set @recount=count(id) from school -- 错误这里只能用select不能用set
如果school为一个@chart参数的话,上面方法就不适用了
应该为:
declare @getRecordSql nvarchar(100)
set @getRecordSql='select @recordcount=count(1) from '+@chart
exec sp_executesql @getRecordSql,N'@recordCount int output',@recordCount output
N'@recordCount int output',@recordCount output
做一个参数的替换,如果测试一下:print @recordCount的话就会显示行数
//示例:选择最新的5条数据从20条开始
execute A_School_getNew 5,20
--存储过程
create procedure A_School_getNew
@Size int,
@StartIndex int
AS
declare @recount int
declare @Sql nvarchar(120)
select @recount=count(id) from school
set @Sql='select top '+convert(nvarchar,@Size)+'id,school_name from school where('+
'ID not in(select top '+convert(nvarchar,@recount-@startIndex+@size)+
' id from school)) order by id desc';
execute (@Sql)
相关文章推荐
- 存储过程中将sql语句结果赋值给变量
- 存储过程中将sql语句结果赋值给变量
- 存储过程中将查询语句结果赋值给变量
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
- sp_executesql动态执行sql语句并将结果赋值给一变量
- sql 如何将拼的sql语句结果赋值给变量
- SQL中将查询结果赋值给变量
- SQL中将select结果赋值给变量
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
- SQL中将查询结果赋值给变量
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
- 在Excel VBA中将SQL查询的结果赋值给变量的方法
- 不创建表结构,直接放入临时表结果(包括存储过程,直接sql语句)
- JDBC中将一个变量加入SQL语句执行
- shell中将命令结果赋值给变量
- sql语句变量赋值
- Oracle 在函数或存储过程中执行sql查询字符串并将结果值赋值给变量
- php将SQL查询结果赋值给变量
- Oracle中PL/SQL语句的变量、常量声明和赋值