sql server case
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的更多相关文章
- sql server Case when 的用法
sql Case 仅仅返回第一个符合条件的值,剩下的Case部分将会被自动忽略. Case 的使用有两种格式:简单Case函数和Case搜索函数. 简单Case 函数: Case sex when ' ...
- SQL Server - case when...then...else...end
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...
- SQL Server case表达式的用法
★CASE表达式是一个标量表达式,它基于条件逻辑来返回一个值.因为CASE是一个标量表达式,所以它可以应用在SELECT.WHERE.HAVING以及ORDER BY子句中. CASE表达式有两种格式 ...
- SQL Server case when 日期字符串转换 多表查询 嵌套子查询
select distinct stu.*, dbo.GetClassNameByStudentCode(stu.Code) as ClassName, dbo.GetCourseNameByStud ...
- sql server case when 判断为空
代码如下 select distinct G.* ,(select BUSINESS_NAME from BusinessInfo where BusinessInfo.BUSINESS_BID=G. ...
- SQL SERVER:CASE判断空,错误一例
-----错误判断------------------------------------------------------------------------------------ SELEC ...
- SQL Server CASE语句中关于Null的处理
问: 从数据表中选择一个字段“field”,如果“field”值是1或NULL就赋值为1,其它情况为0,该怎么写啊?这样写对不对啊?(CASE fieldWHEN '1' THEN '1'WHEN N ...
- SQL Server case when 实现分类汇总
case when 实现分类汇总
- sql server case when
case具有两种格式:简单Case函数和Case搜索函数 简单case函数 实例:CASE sex when '1' then '男' when '2' then'女' els ...
随机推荐
- ionic中执行pop返回上一个页面,还需要执行操作
<ion-navbar> </ion-navbar> 从A页面push到B页面拿到数据以后,从B页面pop到A页面,在A页面展示刚刚拿到的数据,用 ionViewDidEnte ...
- minikube k8 ingress--https://kubernetes.io/docs
https://ehlxr.me/2018/01/12/kubernetes-minikube-installation/[Kubernetes 学习笔记之 MiniKube 安装 in CHINA] ...
- iOS - 常用的CG结构体
CGPoint.CGSize.CGRect.CGRectEdge实际上都是结构体 一,几个常用的结构体 CGPoint 定义一个点,设置x坐标和y坐标 struct CGPoint { CGFlo ...
- [ovs][dpdk] ovs-dpdk, dpdk port 大量丢包
gdb了ovs的代码,发现是 dpdk的imiss计数在不断的丢包. 看了ovs-openvswitchd的日志,重启时发现如下行: --21T11::.427Z||timeval|WARN|Unre ...
- [skill][telnet] 用telnet获取一个网页
一直也搞不懂, telnet到底是干嘛用的. 然而, 它可以得到一个网页. /home/tong/Data/performance_test [tong@T7] [:] > telnet nyu ...
- [development][vim] vim显示空白字符
1. 作为一个严谨的程序员,你必须关心你敲下过的没一个字符.其中包括空白字符. 2. 有时候你需要review别人的代码,对于哪些肆意使用tab,space,enter的人.你怎么发现那些被他们留下的 ...
- Cmake入门资料
1.http://blog.sina.com.cn/s/blog_3f3422fd010009vn.html 2.http://www.cnblogs.com/coderfenghc/tag/cmak ...
- 在VMware运行Linux下,密码错误的原因
抱歉,没有奏效,请再试一次 密码明明是正确的,但依旧报错 原因: 密码中有大写,但键盘中的大写按键在登录界面无效,需要按住shift才能实现大写.
- 转:Java properties | FileNotFoundException: properties (系统找不到指定的文件。)
原文地址:https://blog.csdn.net/cor_twi/article/details/44514871 web项目 (dynamic web project ) 读取propertie ...
- 内部排序->插入排序->其它插入排序->折半插入排序
文字描述 和直接插入排序比较,只是把“查找”操作利用“折半查找”来实现,由此进行的插入排序叫做折半插入排序. 示意图 略 算法分析 和直接插入排序比,减少了比较次数,但是移动次数没有变,所以折半插入排 ...