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

  

最新文章

  1. 《连载 | 物联网框架ServerSuperIO教程》- 8.单例通讯模式开发及注意事项
  2. 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(1)数据库访问(0)
  3. (C++)窗口置前SetForegroundWindow(pThis-&gt;hwndWindow);
  4. Ubuntu16.10 主题flatabulous安装
  5. Cocoapods的使用教程
  6. 移动设备和SharePoint 2013 - 第1部分:概述
  7. JQuery Uplodify上传附件(同一个页面多个uplodify控件解决方案)
  8. MyBatis 物理分页
  9. 《APUE》第四章笔记(3)
  10. webpack 配置 (支持 React SCSS ES6 编译打包 和 模块热更新 / 生成SourceMap)
  11. 一步一步重写 CodeIgniter 框架 (8) —— 视图的嵌套输出与返回
  12. [SinGuLaRiTy] SplayTree 伸展树
  13. IDEA引MAVEN项目jar包依赖导入问题解决
  14. java应用程序的运行机制
  15. Day01_Python学习今日收获
  16. java将图片传为设定编码值显示(可做刺绣)
  17. [BZOJ3038]遥远的国度
  18. U3D外包团队—技术分享 U3d中获得物体的size
  19. keepalive高可用
  20. Java多线程:SimpleDateFormat

热门文章

  1. 洛谷P3193 [HNOI2008]GT考试 kmp+dp
  2. eclipse debug调试时老是被URLClassLoader这个类拦截到,不能进入到要调试的类里面去
  3. 程序------&gt;数据结构
  4. HttpUrlConneciton上传JSON数据
  5. UILabel部分文字可点击
  6. MSSQL查询收缩和备份进度
  7. wx 文件编辑框
  8. vue中$set的用法
  9. vue中根据生日计算年龄
  10. vue.js 兄弟组件传值