如何将当前行值作为参数传递给存储过程中的函数

时间:2015-11-25 11:26:18

标签: sql sql-server tsql stored-procedures

此proc将返回许多行。我需要将当前行的a.assetID值作为参数传递给函数fGetAuditDescrConcat。

ALTER PROCEDURE [dbo].[AuditReportLeaseID]
    @leaseID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        a.assetID as Asset, a.Location, 
        CONVERT(VARCHAR, a.auditdate, 101) AS Date, 
        a.qtyaudit AS Qty, 
        c.classname AS Class, a.grade AS Grade, 
        a.serialnumber AS [S/N], 
        a.materialdescription AS Description, a.Notes, 
        (SELECT tf.AD 
         FROM fGetAuditDescrConcat(a.assetId) tf) AS AuditDescription   
    FROM
        audit a
    LEFT OUTER JOIN
        ORDER_DETAILS od ON a.assetID = od.assetId
    INNER JOIN
        class c ON a.classid = c.classid 
    WHERE
        a.classID = c.classID 
        AND a.leaseID = @leaseID
    ORDER BY
        class, grade, a.materialDescription
END

proc不会编译。我收到错误的"'附近的语法无效。'。 我已经确认它是a.assetID参数,它不满意。 我如何将其作为参数传递?

UPDATE这里是函数的代码

  ALTER FUNCTION [dbo].[fGetAuditDescrConcat]
    (   
        -- Add the parameters for the function here
    @AssetID varchar(64)
    )
    RETURNS TABLE 
    AS
    RETURN 
    (
        SELECT Distinct(T1.AssetID), AD=STUFF((Select N', ' + dbo.audit_descriptionText.audit_text 
 FROM  
dbo.audit_descriptions INNER JOIN
dbo.audit_descriptionValues ON dbo.audit_descriptions.audit_descrID = dbo.audit_descriptionValues.audit_descrID INNER JOIN
dbo.audit as T2 ON dbo.audit_descriptions.assetID = T2.assetID INNER JOIN
dbo.audit_descriptionText ON dbo.audit_descriptionValues.audit_textID = dbo.audit_descriptionText.audit_textID
Where T2.AssetID=T1.AssetID 
order by dbo.audit_descriptionText.audit_text 
FOR XML PATH(N'')), 1, 2, N'')
From dbo.audit_descriptions INNER JOIN dbo.audit_descriptionValues ON dbo.audit_descriptions.audit_descrID = dbo.audit_descriptionValues.audit_descrID INNER JOIN
dbo.audit as T1 ON dbo.audit_descriptions.assetID = t1.assetID INNER JOIN
 dbo.audit_descriptionText ON dbo.audit_descriptionValues.audit_textID = dbo.audit_descriptionText.audit_textID                      
Where T1.AssetID=@AssetID
    )

    GO

2 个答案:

答案 0 :(得分:3)

尝试使用OUTER APPLY

ALTER PROCEDURE [dbo].[AuditReportLeaseID]
    @leaseID int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        a.assetID as Asset, 
        a.Location, 
        CONVERT(VARCHAR, a.auditdate, 101) AS Date, 
        a.qtyaudit AS Qty, 
        c.classname AS Class, 
        a.grade AS Grade, 
        a.serialnumber AS [S/N], 
        a.materialdescription AS Description, 
        a.Notes, 
        tf.AD AS AuditDescription   
    FROM
        audit a
    OUTER APPLY fGetAuditDescrConcat(a.assetId) tf
    LEFT OUTER JOIN
        ORDER_DETAILS od ON a.assetID = od.assetId
    INNER JOIN
        class c ON a.classid = c.classid 
    WHERE
        a.classID = c.classID 
        AND a.leaseID = @leaseID
    ORDER BY
        class, grade, a.materialDescription
END

答案 1 :(得分:0)

试试这个 -

ALTER PROCEDURE [dbo].[AuditReportLeaseID]
(
    @leaseID INT
)
AS BEGIN

    SET NOCOUNT ON;

    SELECT
        a.assetID AS Asset,
        a.Location,
        CONVERT(VARCHAR(20), a.auditdate, 101) AS [Date],
        a.qtyaudit AS Qty,
        c.classname AS Class,
        a.Grade AS Grade,
        a.serialnumber AS [S/N],
        a.materialdescription AS [Description],
        a.Notes,
        dbo.fGetAuditDescrConcat(a.assetId) AS AuditDescription
    FROM dbo.[audit] a
    LEFT JOIN dbo.ORDER_DETAILS od ON a.assetID = od.assetId
    JOIN dbo.Class c ON a.classID = c.classID
    WHERE a.leaseID = @leaseID
    ORDER BY
          Class
        , Grade
        , [Description]

END

优化后检查功能 -

ALTER FUNCTION [dbo].[fGetAuditDescrConcat]
(
    @AssetID VARCHAR(64)
)
RETURNS NVARCHAR(MAX)
AS BEGIN

    DECLARE @val NVARCHAR(MAX)
    SELECT @val =
         STUFF((
            SELECT N', ' + t.audit_text
            FROM dbo.audit_descriptions d
            JOIN dbo.audit_descriptionValues v ON d.audit_descrID = v.audit_descrID
            JOIN dbo.audit T2 ON d.assetID = T2.assetID
            JOIN dbo.audit_descriptionText t ON v.audit_textID = t.audit_textID
            WHERE T2.AssetID = @AssetID
            ORDER BY d.audit_text
            FOR XML PATH (N'')), 1, 2, N'')

    RETURN @val

END
GO