如何从存储过程中获取结果值

时间:2016-10-13 15:22:23

标签: stored-procedures

请帮助我,我创建了这个存储过程:

ALTER PROCEDURE [dbo].[objueb]
    @UebKat varchar(500) = NULL,
    @Multikz nchar(1) = "|",
    @Objekte nchar(999) = NULL OUTPUT
AS
BEGIN
    SELECT DISTINCT 
        St2.Ueberk, 
        substring((SELECT @Multikz + ST1.ObjektId AS [text()]
                   FROM dbo.DtaVor ST1
                   WHERE ST1.Ueberk = ST2.Ueberk
                   ORDER BY ST1.Ueberk
                   FOR XML PATH ('') ), 2, 1000) [Objekte] 
    FROM
        dbo.DtaVor ST2 
    WHERE
        Ueberk = @Uebkat
END

结果是:

|----------------|---------------------------------------------|
|  Ueberk        | Objekte                                     |
|----------------|---------------------------------------------|
|   BLX HS       | 10002533  |10001132  |10001560  |10002156   |
|----------------|---------------------------------------------|

结果总是一行。

我想在参数(@Objekte

中收到'Objekte'的结果

谢谢!

1 个答案:

答案 0 :(得分:0)

假设DBMS是基于FOR XML PATH的使用的SQL Server。您只需将结果分配给变量:

ALTER PROCEDURE [dbo].[objueb]
@UebKat varchar(500) = NULL,
@Multikz nchar(1) = "|",
@Objekte nchar(999) = NULL OUTPUT
AS
BEGIN

    SELECT @Objekte = Objekte
    FROM
    (SELECT DISTINCT 
    St2.Ueberk, 
    substring((SELECT @Multikz + ST1.ObjektId AS [text()]
               FROM dbo.DtaVor ST1
               WHERE ST1.Ueberk = ST2.Ueberk
               ORDER BY ST1.Ueberk
               FOR XML PATH ('') ), 2, 1000) AS [Objekte] 
    FROM
        dbo.DtaVor ST2 
    WHERE
        Ueberk = @Uebkat) o
END

DISTINCT是否需要确保一行(Ueberk是主键?)?如果没有,您可以删除子选择:

ALTER PROCEDURE [dbo].[objueb]
@UebKat varchar(500) = NULL,
@Multikz nchar(1) = "|",
@Objekte nchar(999) = NULL OUTPUT
AS
BEGIN

    SELECT  
    @Objekte = substring((SELECT @Multikz + ST1.ObjektId AS [text()]
               FROM dbo.DtaVor ST1
               WHERE ST1.Ueberk = ST2.Ueberk
               ORDER BY ST1.Ueberk
               FOR XML PATH ('') ), 2, 1000)
    FROM
        dbo.DtaVor ST2 
    WHERE
        Ueberk = @Uebkat
END