请教一条简单的SQL语句, 谢谢!
来源:百度知道 编辑:UC知道 时间:2024/06/10 21:22:33
//Name, Size_Price是Products表的字段名,用于存储产品不同包装对应的不同价格.
以下为该字段中的任意两条数据:
Name Size_Price
-- --
Tea 25G@1350,100G@4500
Coffee 10MG@350,100MG@1950,500MG@9845
......
我想利用SQL语句将数据分3个字段显示为如下格式:
Name Size Price
-- __ __
Tea 25G 1350
Tea 100G 4500
Coffee 10MG 350
Coffee 100MG 1950
Coffee 500MG 9845
......
备注:
1、Size_Price字段,采用:“包装@价格”为一组的格式存储,不同的组间以逗号分隔[,]分隔,在每行数据中,包含的“组”的数目是不固定的,可能2组,3组,或者X组,详见以上示例表。
2、Name与Size_Price间的对应关系请留意。
3、只想利用SQL语句达到这种效果,网页终端采用编程语言将数据分隔并数组化的方式,或者分隔后循环显示,我已经能够很好的达到,请不必要重复。
4、网站开发平台,ASP.NET 2.0, SQL Server 2005.
5、请直接回答提问,非专业人士勿扰,方案越简单精练越好。
6、对每一位提出完美解决方案的回答者表示敬意,如方便,希望提供E-Mail 或 IM通讯方式,非常感谢!
Name 和 Size_Pri
select identity(int,1,1)as id,* into #a from Products
select identity(int,1,1)as id,* into #tempProducts from Products where Name=''
declare @id int
declare @maxid int
declare @flag int
set @id=1
select @maxid=max(id) from #a
while @id<=@maxid
begin
select @flag=patindex('%,%',Size_Price) from #a where id=@id
while @flag<>0
begin
declare @S_P varchar(500)
declare @name varchar(50)
select @S_P=substring(Size_Price,0,patindex('%,%',Size_Price)),@name=Name from #a where id=@id
insert into #tempProducts values(@name,@S_P)
update #a set Size_Price=replace(Size_Price,@S_P+',','') where
id=@id
select @flag=patindex('%,%',Size_Price) from #a where id=@id
end
insert into #tempProducts select Name,Size_Price from #a where id=@id
set @id=@id+1
end
(
至此#tempProducts