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. [httpd] httpd directory list character encoding

    在httpd下放置文件用来直接从浏览器访问的时候,中文文件名有可能产生乱码. 做以下设置,调整字符编码: 1: Add this to your .htaccess: IndexOptions +Ch ...

  2. Windows 内存管理

    参考文献: http://blog.csdn.net/wubin1124/article/details/3760242 工作集(内存): 可以这么理解, 此值就是该进程所占用的总物理内存. 但是这个 ...

  3. python多线程相关知识点

    1. 信号量 信号机维护着一个计数器,指定可同时访问资源或者进入临界区的线程数.每次有一个线程获得信号机时,计数器-1.若计数器为0,其他线程就停止访问信号机 Semphore,是一种带计数的线程同步 ...

  4. HTML中select的option设置selected="selected"无效的解决方案

    今天遇到了一个奇葩问题,写HTML时有个select控件,通过设置option的selected="selected"居然无效,但是在其他浏览器是可以的,问了一下Google大神, ...

  5. tensorflow入门笔记(一) tf.app.flags.FLAGS

    tf.app.flags.DEFINE_xxx()就是添加命令行的optional argument(可选参数),而tf.app.flags.FLAGS可以从对应的命令行参数取出参数.举例如下: FL ...

  6. Xshell远程连接 与 Xftp文件传输

    刚开始接触Linux的时候,会想我该怎么在Windows连接到另一台Linux服务器,怎么把我Windows上的文件放到我Linux上面,网上搜索之后,知道可以用Xshell远程连接到Linux,用X ...

  7. 二、Spring Boot 配置文件

    1.配置文件 Spring Boot使用一个全局的配置文件,配置文件名是固定的 application.properties applicatioin.yml 配置文件的作用:修改Spring Boo ...

  8. 计算机网络 0.初识Internet与TCP/IP协议

    互联网,即因特网,Internet.互联网是一个世界范围的计算机网络.连接了世界上无数的计算设备,这些计算设备为PC.基于Linux的工作站,serverservers等等. 这些设备依据其作用不同可 ...

  9. unix下命令窗分屏工具

    运行 sudo apt-get install terminator 效果 新建分屏窗口:右键鼠标选择

  10. 安装与破解PyCharm专业版

    安装与破解PyCharm专业版(有两种方法) 一.安装与破解PyCharm专业版 1.安装pycharm(建议安装python3) 这是网址:(https://www.jetbrains.com/py ...