试试吧
create table tb_product
(
id INT
,typeproduct CHAR(5)
)
create table tb_product_attribute (
idProduct int, typeAttribute char(5), valueAttribute char(6)
)
GO
CREATE Function dbo.TypeAttribute2Comma(@idProduct int)
returns varchar(max)
BEGIN
declare @tmp varchar(max)
SET @tmp = ''
select @tmp = @tmp + typeAttribute + ', ' from tb_product_attribute where idProduct = @idProduct
RETURN (select SUBSTRING(@tmp, 0, LEN(@tmp)))
END
GO
CREATE Function dbo.ValueAttribute2Comma(@idProduct int)
returns varchar(max)
BEGIN
declare @tmp varchar(max)
SET @tmp = ''
select @tmp = @tmp + typeAttribute + ', ' from tb_product_attribute where idProduct = @idProduct
RETURN (select SUBSTRING(@tmp, 0, LEN(@tmp)))
END
GO
INSERT tb_product
(id ,typeproduct)
VALUES (1 ,'Type1'),
(2 ,'Type1'),
(3 ,'Type1')
insert tb_product_attribute(idProduct ,typeAttribute ,valueAttribute) values
(1, 'Attr1', 'Value1'),
(1, 'Attr2', 'Value3'),
(2, 'Attr1', 'Value1'),
(2, 'Attr2', 'Value3'),
(3, 'Attr1', 'Value2'),
(3, 'Attr3', 'Value3')
GO
select
*
,dbo.TypeAttribute2Comma(id) TypeAttribute2
,dbo.ValueAttribute2Comma(id) ValueAttribute2
from tb_product
结果
id typeproduct TypeAttribute2 ValueAttribute2
----------- ----------- -------------- ----------------
1 Type1 Attr1, Attr2 Attr1, Attr2
2 Type1 Attr1, Attr2 Attr1, Attr2
3 Type1 Attr1, Attr3 Attr1, Attr3
你的问题不太清楚(我)..请更新您的问题,加上预期的结果..或更好地解释你的目标 – scaisEdge
你如何确定如果一个产品被复制? idTipoProduto与某事有关吗?我假定Product.Id与ProductAttributes.idPododuto有关。这是真的? –
有了您提供的样本数据,您能向我们展示您的预期结果吗? –