試試吧
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有關。這是真的? –
有了您提供的樣本數據,您能向我們展示您的預期結果嗎? –