0

我有以下从视图调用的 SQL 函数

ALTER FUNCTION [dbo].[GetModulesforTesCaseID] 
(@TestCaseID numeric(18))
RETURNS VARCHAR(1000)
AS
BEGIN
declare @Modules VARCHAR(1000)
select @Modules = SUBSTRING(
(SELECT ',' + cast (S.MODULENAME as varchar)
FROM MODULE s JOIN EFFMODFORTESTCASE EMTC ON S.MODULEID = EMTC.MODULEID
JOIN TESTCASE TC ON TC.TESTCASEID =  EMTC.TESTCASEID
where tc.testcaseid = @TestCaseID
ORDER BY s.MODULENAME
FOR XML PATH('')),2,200000)

    if @Modules is null BEGIN
            set @Modules = SELECT M.MODULENAME FROM MODULE M JOIN SUBMODULESFORMODULE SFM ON SFM.MODULEID = M.MODULEID 
            JOIN EFFSUBMODFORTESTCASE ESMTC ON ESMTC.SUBMODULEID = SFM.SUBMODULEID WHERE TESTCASEID = @TestCaseID
    END 

return @Modules
end

我正进入(状态

消息 156,级别 15,状态 1,过程 GetModulesforTesCaseID,第 16 行
关键字“SELECT”附近的语法不正确

请帮助我纠正 If 条件下的语法错误

4

1 回答 1

4

错误是当您将值分配@ModulesIF. 试试这个(你需要确保结果SELECT只返回一行):

ALTER FUNCTION [dbo].[GetModulesforTesCaseID] 
(@TestCaseID numeric(18))
RETURNS VARCHAR(1000)
AS
BEGIN
declare @Modules VARCHAR(1000)
select @Modules = SUBSTRING(
(SELECT ',' + cast (S.MODULENAME as varchar)
FROM MODULE s JOIN EFFMODFORTESTCASE EMTC ON S.MODULEID = EMTC.MODULEID
JOIN TESTCASE TC ON TC.TESTCASEID =  EMTC.TESTCASEID
where tc.testcaseid = @TestCaseID
ORDER BY s.MODULENAME
FOR XML PATH('')),2,200000)

    if @Modules is null BEGIN
            SELECT @Modules = M.MODULENAME 
            FROM MODULE M 
            INNER JOIN SUBMODULESFORMODULE SFM 
                ON SFM.MODULEID = M.MODULEID 
            INNER JOIN EFFSUBMODFORTESTCASE ESMTC 
                ON ESMTC.SUBMODULEID = SFM.SUBMODULEID 
            WHERE TESTCASEID = @TestCaseID
    END 

return @Modules
end
于 2013-01-17T20:53:17.280 回答