如何把,1,2,3,这样id存放格式通过视图查询对应的名称来?

fgyAdmin 2010-10-07 11:49:32
在表 material有一个字段保存多个类型id,是字符串类型。比如: " ,1,2,3, "
其中 1,2,3 是类型的ID, 表示Material有3中类型。

如何实现在视图用一个字段返回对应的类型名称,如“,类型名称1,类型名称2,类型名称3,”?


...全文
125 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ganchunsaixx 2010-10-10
  • 打赏
  • 举报
回复
可以用CASE做啊
id = CASE
WHEN id = 1 THEN '类型1'
WHEN id = 2 THEN '类型2'
WHEN id = 3 THEN '类型3'
ELSE '类型4'
END
但是类型太多的话就写的太多啦
天下如山 2010-10-09
  • 打赏
  • 举报
回复
给你个例子:

if object_id('test.dbo.recordet') is not null drop table recordet
create table recordet(reId int identity(1,1),code varchar(20),rName varchar(100))
insert into recordet
select '01','第一' union all
select '01','第二' union all
select '01','第三' union all
select '02','第四' union all
select '01','第五' union all
select '02','第六' union all
select '04','第七'
select * from recordet
--
reId code rName
----------- -------------------- ------------------------------------
1 01 第一
2 01 第二
3 01 第三
4 02 第四
5 01 第五
6 02 第六
7 04 第七

if object_id('test.dbo.fstr') is not null drop function fstr
go
create function fstr(@code varchar(20)) returns varchar(1000)
as
begin
declare @fstr varchar(1000)
select @fstr=isnull(@fstr+',','')+rtrim(rName) from recordet where code=@code
return @fstr
end
go
select distinct code,rName=dbo.fstr(code) from recordet group by code
code rName
-------------------- ------------------------------------------------
01 第一,第二,第三,第五
02 第四,第六
04 第七
--sql2005方法:
--SQL2005用XML:
select distinct code,rName=stuff((select ','+rtrim(rName) from recordet
where code=t.code for xml path('')),1,1,'')
from recordet t
fgyAdmin 2010-10-09
  • 打赏
  • 举报
回复
2005
可以xml path()解决

如何书写语句?
水族杰纶 2010-10-07
  • 打赏
  • 举报
回复
2000的话
只有函数
2005
可以xml path()解决
SQLCenter 2010-10-07
  • 打赏
  • 举报
回复
拆分,关联material查出来
「已注销」 2010-10-07
  • 打赏
  • 举报
回复
declare @str nvarchar(20)
set @str=',1,2,3'

select replace(@str,',',N',类型名称') A

A
-------------------------------
,类型名称1,类型名称2,类型名称3

(1 row(s) affected)



hao1hao2hao3 2010-10-07
  • 打赏
  • 举报
回复

create function [dbo].[fn_split](@inputstr varchar(8000), @seprator varchar(10))
returns @temp table (a varchar(200))
as
begin
declare @i int
set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator , @inputstr)
while @i >= 1
begin
insert @temp values(left(@inputstr , @i - 1))
set @inputstr = substring(@inputstr , @i + 1 , len(@inputstr) - @i)
set @i = charindex(@seprator , @inputstr)
end
if @inputstr <> '\'
insert @temp values(@inputstr)
return
end

declare @s varchar(20)
set @s =',1,2,3,';
select * from dbo.fn_split(substring(@s,2,len(@s)-2),',')
--结果
1
2
3


fgyAdmin 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wufeng4552 的回复:]
2000的话
只有函数
2005
可以xml path()解决
[/Quote]


如果用函数写,那么参数是字符串,传递'1,2,3'类似参数。在函数里面的sql语句就需要用动态写吗?
函数好像不支持动态sql执行返回值的。

能否粘贴出来完整的sql函数语句了?
suchaoxiaohao 2010-10-07
  • 打赏
  • 举报
回复
恩 貌似是这个样子的
fgyAdmin 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wufeng4552 的回复:]
2000的话
只有函数
2005
可以xml path()解决
[/Quote]


2000的函数如何写了?

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧