SQL Server占用行并写入1行

时间:2017-06-22 06:18:28

标签: sql-server

table

alter function yazarkitap(
@yazarID int)
returns nchar(100)
as
begin
declare @kitaplar nchar(500)
select @yazarID=y_id from yazar 
select (STUFF((select','+kitap_adi from Kitap where @yazarID=y_id FOR XML PATH('')
),1,2,''))as kitapadi
end

我正在尝试yazar_id并显示所有kitap_adi在一行中都有相同的yazar_id。我搜索了合并功能,但我做不到。

1 个答案:

答案 0 :(得分:0)

你的写作有点不对劲。您可以按如下方式更改它。您可能会再次收到错误。我建议你删除它并重新创建它。

ALTER FUNCTION yazarkitap
(
    @yazarID INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN

    DECLARE @kitaplar NVARCHAR(MAX)     

    SELECT @kitaplar = STUFF((
            SELECT ',' + kitap_adi
            FROM Kitap
            WHERE 
                y_id = @yazarID
            FOR XML PATH('')
            ), 1, 1, '')

    RETURN @kitaplar

END