zoukankan      html  css  js  c++  java
  • 存储过程中将查询语句结果赋值给变量

    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)
  • 相关阅读:
    Linux cooked-mode capture 格式转换
    lo口环路问题分析
    感知器与多层感知机
    梯度下降
    Unity UGUI Canvas 使用Slider制作角色血条
    NGUI 通过按压Button控制角色持续行走
    python基础--函数(二)
    python基础--函数(一)
    python基础--推导式
    python基础--公共操作
  • 原文地址:https://www.cnblogs.com/shuai/p/1596705.html
Copyright © 2011-2022 走看看