您的位置:程序门 -> ms-sql server -> 基础类



求一个视图或存储过程


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求一个视图或存储过程[已结贴,结贴人:do_ve]
发表于:2007-04-20 16:39:52 楼主
tablea

customnerid         productlist(nvarchar)
1000                       200
1001                       201,202,
1002                       200,202
=======================================
tableb

productid           description
200                       description0
201                       description1
202                       description2
203                       description3
204                       description4

希望得到的结果:
customnerid         productid       description
1000                       200                   description0
1001                       201                   description1
1001                       202                   description2
1002                       200                   description0
1002                       202                   description2

也就是把productlist中包含到的product都查询出来,关联到customerid.得到一个新表。其中productlist中的值以“,”隔开。


发表于:2007-04-20 16:56:161楼 得分:100
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   charindex(b.productid,   a.productlist)   >   0
发表于:2007-04-20 16:56:552楼 得分:0
或者

select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   a.productlist   like   '% '   +   b.productid   +   '% '
发表于:2007-04-20 16:59:103楼 得分:0
--建立測試環境
create   table   tablea
(customnerid char(4),
  productlist nvarchar(100))

insert   tablea   select   '1000 ',                       '200 '
union   all   select   '1001 ',                       '201,202 '
union   all   select   '1002 ',                       '200,202 '

create   table   tableb
(productid char(3),
  description nvarchar(100))
insert   tableb   select   '200 ',                       'description0 '
union   all   select   '201 ',                       'description1 '
union   all   select   '202 ',                       'description2 '
union   all   select   '203 ',                       'description3 '
union   all   select   '204 ',                       'description4 '
go
--測試
--方法一:
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   charindex(b.productid,   a.productlist)   >   0

--方法二:
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   a.productlist   like   '% '   +   b.productid   +   '% '
go
--刪除測試環境
drop   table   tablea,   tableb
go
--結果
/*
customnerid productid description
1000 200 description0
1001 201 description1
1001 202 description2
1002 200 description0
1002 202 description2
*/
发表于:2007-04-20 17:00:194楼 得分:0
lz楼上的可以吗
发表于:2007-04-20 17:01:115楼 得分:0
--如果要寫成視圖
--方法一:
create   view   v_test   as
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   charindex(b.productid,   a.productlist)   >   0
go

--方法二:
create   view   v_test   as
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   a.productlist   like   '% '   +   b.productid   +   '% '
go

--調用
select   *   from   v_test
发表于:2007-04-20 17:02:156楼 得分:0
--如果要寫成存儲過程
--方法一:
create   procedure   p_test   as
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   charindex(b.productid,   a.productlist)   >   0
go

--方法二:
create   procedure   p_test   as
select
a.customnerid,
b.productid,
b.[description]
from
tablea   a
inner   join
tableb   b
on   a.productlist   like   '% '   +   b.productid   +   '% '
go

--調用
EXEC   p_test
发表于:2007-04-20 17:03:117楼 得分:0
ls正解


快速检索

最新资讯
热门点击