很牛的储存过程
来源:互联网 发布:js代码的执行方法 编辑:程序博客网 时间:2024/06/05 02:53
t1对应值
a b
-------------
a1 b1
a2 b1,b2
a3 b3
a4 b5,b9,b10
t2对应值
b c
-------------
b1 c1
b2 c2
b3 c3
b4 c4
b5 c5
....
b10 c10
t1中的b字段和t2中的b字段里的内容有关联的
我想根据b字段的关联查询得到下面的结果
t3
a d
--------------
a1 c1
a2 c1,c2
a3 c3
a4 c5,c9,c10
if object_id('t1') is not null
drop table t1
if object_id('t2') is not null
drop table t2
if object_id('f_getall') is not null
drop function f_getall
go
create table t1(a varchar(10),b varchar(10))
create table t2(b varchar(10),c varchar(10))
insert into t1 values('a1','b1')
insert into t1 values('a2','b1,b2')
insert into t1 values('a3','b3')
insert into t1 values('a4','b5,b9,b10')
insert into t2 values('b1','c1')
insert into t2 values('b2','c2')
insert into t2 values('b3','c3')
insert into t2 values('b4','c4')
insert into t2 values('b5','c5')
insert into t2 values('b6','c6')
insert into t2 values('b7','c7')
insert into t2 values('b8','c8')
insert into t2 values('b9','c9')
insert into t2 values('b10','c10')
go
select * from t1
select * from t2
create function f_getall
(@b varchar(10))
returns varchar(1000)
as
begin
declare @s varchar(1000)
declare @now_b varchar(1000),@DH_i int
set @now_b=@b
if(charindex(',',@now_b)>0)
begin
while(charindex(',',@now_b)>0)
begin
set @DH_i=charindex(',',@now_b)
declare @b_c varchar(100)
set @b_c=substring(@now_b,1,@DH_i-1)
select @s=isnull(@s,'')+c+',' from t2 where b=@b_c
set @now_b=substring(@now_b,@DH_i+1,len(@now_b))
end
set @s=@s+@now_b
end
else
begin
select @s=isnull(@s,'')+c from t2 where b=@now_b
end
return @s
end
go
--执行结果语句
select a,dbo.f_getall(b) as d from t1
- 很牛的储存过程
- 储存过程 的 创建
- 储存过程的优势
- mysql的储存过程
- 通用的分页储存过程
- 储存过程,游标的使用
- JDBC的储存过程 剖解
- MySQL储存过程的步骤
- 储存过程
- 储存过程
- 储存过程
- 储存过程
- mysql 储存过程的特点 (翻译)
- 操作类的一些SQL 储存过程
- java调用储存过程的方法
- java调用储存过程的方法
- mysql 储存过程的流程控制
- Oracle储存过程的基本语法
- c#通过oledb获取excel文件表结构信息
- 函数对象简单入门
- 好游戏是什么样的
- 5.12汶川7.8级大地震
- 解析Java类和对象的初始化过程_J2EE_Java开发_软件开发-编程-IT资源网
- 很牛的储存过程
- com和dll的区别
- petshop4.0 详解之五(PetShop之业务逻辑层设计)
- com和dll的区别
- (翻译)设计Qt风格的C++的应用程序接口 (转)
- 这些你注意了吗?
- 如何设计中间业务层
- Visual C#.Net网络程序开发-Socket篇
- 使用VirtualAlloc()创建虚拟内存,修改可读、可写属性