use mytest
go exec p_city 2,4 exec p_city_cnt 2,3 select stuff((select ',' + city_id from cities for xml path('')),1,1,'') concatStr
select stuff((select ',' + city_name from cities for xml path('')),1,1,'') concatStr

  

USE mytest
GO IF EXISTS ( select * from dbo.sysobjects where id=OBJECT_ID('dbo.p_city') and type = 'P')
drop procedure p_city
GO create procedure p_city
@idx int,
@idy int
--,@cnt int output --solution 2.3
AS
BEGIN declare @sql nvarchar(2000)
declare @param nvarchar(2000) /*
--print 'solution - 1.1 - common situation'
set @sql = N'select city_name from cities where city_id>=@id_start and city_id<=@id_end'
set @param = N'@id_start int, @id_end int'
EXEC sp_executesql @sql, @param, @id_start=@idx, @id_end=@idy
*/ --print 'solution - 1.2 - for some special situation'
create table #tmp (city_name varchar(100))
set @sql = N'insert into #tmp(city_name) select city_name from cities where city_id between @id_start and @id_end'
set @param = N'@id_start int, @id_end int'
EXEC sp_executesql @sql, @param, @id_start=@idx, @id_end=@idy
select * from #tmp
--set @cnt = (select count(1) from #tmp) --solution 2.3
IF EXISTS (select name from tempdb..sysobjects where id=OBJECT_ID('tempdb..#tmp') and type='U')
drop table #tmp /*
-- not ok
print 'solution - 1.3 - use table variable'
exec( N'declare @ctname table(city_name varchar(100))')
set @sql = N'insert into @ctname(city_name) select city_name from cities where city_id between @id_start and @id_end'
set @param = N'@id_start int, @id_end int'
EXEC sp_executesql @sql, @param, @id_start=@idx, @id_end=@idy
exec(N'select * from @ctname')
*/ END

  

USE mytest
GO IF EXISTS ( select name from sysobjects where name = 'p_city_cnt' and type = 'P')
drop procedure p_city_cnt
GO create procedure p_city_cnt
@idx int,
@idy int
AS
BEGIN --print 'solution - 2.1'
create table #tmp (
city_name varchar(100)
)
insert into #tmp(city_name) exec p_city @idx, @idy
--select count(1) as number from #tmp
select @@ROWCOUNT as number
drop table #tmp /*
--print 'solution - 2.2'
declare @ctname table(
city_name varchar(100)
)
insert into @ctname (city_name) exec p_city @idx, @idy
select count(1) as number from @ctname
*/
/*
-- solution 2.3.1, will response 2 result sets.
declare @cnt int
exec p_city @idx, @idy, @cnt out
select @cnt as number -- solution 2.3.2, will response only one result set.
create table #tmp (
city_name varchar(100)
)
declare @cnt int
insert into #tmp(city_name) exec p_city @idx, @idy, @cnt out
select @cnt as number
drop table #tmp
*/
END

  

sql server case的更多相关文章

  1. sql server Case when 的用法

    sql Case 仅仅返回第一个符合条件的值,剩下的Case部分将会被自动忽略. Case 的使用有两种格式:简单Case函数和Case搜索函数. 简单Case 函数: Case sex when ' ...

  2. SQL Server - case when...then...else...end

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...

  3. SQL Server case表达式的用法

    ★CASE表达式是一个标量表达式,它基于条件逻辑来返回一个值.因为CASE是一个标量表达式,所以它可以应用在SELECT.WHERE.HAVING以及ORDER BY子句中. CASE表达式有两种格式 ...

  4. SQL Server case when 日期字符串转换 多表查询 嵌套子查询

    select distinct stu.*, dbo.GetClassNameByStudentCode(stu.Code) as ClassName, dbo.GetCourseNameByStud ...

  5. sql server case when 判断为空

    代码如下 select distinct G.* ,(select BUSINESS_NAME from BusinessInfo where BusinessInfo.BUSINESS_BID=G. ...

  6. SQL SERVER:CASE判断空,错误一例

     -----错误判断------------------------------------------------------------------------------------ SELEC ...

  7. SQL Server CASE语句中关于Null的处理

    问: 从数据表中选择一个字段“field”,如果“field”值是1或NULL就赋值为1,其它情况为0,该怎么写啊?这样写对不对啊?(CASE fieldWHEN '1' THEN '1'WHEN N ...

  8. SQL Server case when 实现分类汇总

    case when 实现分类汇总

  9. sql server case when

    case具有两种格式:简单Case函数和Case搜索函数 简单case函数 实例:CASE sex     when '1' then '男'     when '2' then'女'     els ...

随机推荐

  1. [development][C][thread_local] 线程全局变量

    线程全局变量, 线程本地存储, thread_local storage 只需要在变量定义的最前边添加 __thread 关键字. 用法: https://gcc.gnu.org/onlinedocs ...

  2. 使用XPath对象解析xml文件

    使用XPath对象解析xml文件 1.DocumentBuilderFactory类  工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器 其构造方法受保护,用newInstance() ...

  3. scrollView截取指定区域的图片

    把scrollView放到一个容器里面,再截图就可以了 scrollview放到容器: UIView *lunboCarrier = [[UIView alloc] initWithFrame:CGR ...

  4. 使用jquery.uploadify上传文件

    今天在网上找了一天,想要找到一个比较全的使用案例,结果发现基本上全是一个版本的... 我的问题主要是上传完成后,还需要将路径获取到,然后保存到数据库. 查了一下资料发现有这么一个参数onComplet ...

  5. 产品列表中使用v-lazyload插件懒加载img图片,但是当产品列表重新排序(人气,销量,价格...),产品info信息改变,但是 img 图片没有发生变化;

    1.控制台查看 DOM 结构,发现 DOM 绑定的图片链接也没有发生变化: 2.查阅资料找到解决方法,只需要在 img 标签中增加 :key='imgUrl',即可实现 img 图片随数据排序的改变动 ...

  6. Docker 容器(六)

    镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的 类 和 实例 一样,镜像是静态的定义,容器是镜像运行时的实体.容器可以被创建.启动.停止.删除.暂停等. 容器的实质是 ...

  7. 升级my.cnf注意

    升级my.cnf注意 mkdir -p /ngbs/data/{logs,tmp} vi  /etc/init.d/mysqlbasedir=/usr/local/mysql datadir=/ngb ...

  8. mysql连接池不能回避的wait timeout问题(转)

    起因 我们的项目组一直在使用albianj作为开发框架在开发应用.使用至今倒也是没有出现很大的问题,但最近加过监控的接口基本上都会在使用一段时间后,突然之间执行数据库操作变得很慢.虽然会变慢,但持续的 ...

  9. Python3学习之路~6.5 私有属性和私有方法

    属性分为静态属性和动态属性,静态属性就是变量,动态属性就是方法.但是一般我们说的属性就是变量,方法就是方法.私有属性/方法就是外面访问不了,只有自己能够访问的属性/方法.如何将属性和方法分别变成私有属 ...

  10. 并发编程---Process对象的其他属性或方法

    Process对象的其他属性或方法 #join方法 from multiprocessing import Process import time,os ''' 需求:让主进程等着子进程运行完毕,才能 ...