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 ...
随机推荐
- MySQL之更新型触发器
DELIMITER || CREATE TRIGGER tri_video_class AFTER UPDATE ON 数据库名.表名称 FOR EACH ROW begin IF((old.stat ...
- sqlserver binary varbinary image 的区别
sqlserver binary varbinary image 的区别 binary 固定长度的二进制数据,其最大长度为 8,000 个字节. varbinary 可变长度的二进制数 ...
- Python开发【笔记】:抓包(实时分隔)
抓包 进行linux系统抓包,并且定时分隔防止文件太大 #!/usr/bin/env python # -*- coding:utf-8 -*- import os import sys import ...
- js中值的基本类型与引用类型,以及对象引用,对象的浅拷贝与深拷贝
js有两种类型的值:栈:原始数据类型(undefinen,null,boolead,number,string)堆:引用数据类型(对象,函数和数组)两种类型的区别是:储存位置不同,原始数据类型直接存储 ...
- replace函数的使用(替换单个和全局)
//单个 var aa="1-2-3"; aa=aa.replace(',',"."); console.log(aa); //全局替换 var aa=&quo ...
- 前端 HTML form表单标签 input标签 type属性 重置按钮 reset
input type="reset" value="重置" reset重置 还原到默认状态 <!DOCTYPE html> <html lan ...
- 预见2019吴晓波年终秀演讲PPT整理
在2018年倒数的第二天12月30日晚上7点在广东珠海横琴拉开帷幕,吴晓波以一场“预见2019”的年终盛典,和大家一起回望即将告别的跌宕一年,细数过去的焦虑和改变,瞭望未来的激越和走向.下面我们一起来 ...
- C++ 常用算法
http://blog.csdn.net/jgzquanquan/article/details/77185711
- 20165236郭金涛 预备作业3 Linux安装及学习
我在Linux安装过程遇到的问题: 1.“不能为虚拟电脑XX打开一个新任务”: 出现这种情况是电脑没有开启blos,解决方法是:开机进入联想界面的时候,直接按F2可以快速进入选择开启blos. Lin ...
- Nginx配置虚拟主机
就是在一台服务器启动多个网站. 如何区分不同的网站: 1.域名不同 2.端口不同 在Nginx的安装目录的conf目录下有个配置文件nginx.conf 1.端口区分: 复制server节点,更改端口 ...