Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9148247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:13:21+00:00 2026-06-17T11:13:21+00:00

I have below SQL function which I am calling from a view ALTER FUNCTION

  • 0

I have below SQL function which I am calling from a view

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

I am getting

Msg 156, Level 15, State 1, Procedure GetModulesforTesCaseID, Line 16
Incorrect syntax near the keyword ‘SELECT’

please help me in correcting syntax error at the If condition

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:13:22+00:00Added an answer on June 17, 2026 at 11:13 am

    The error is when you assign the value to the @Modules variable inside your IF. Try this instead (you need to make sure that the result for that SELECT returns only one row):

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Sql-Server, and I have the below query which returns all columns that
I have sql server procedure, please see below. ALTER PROCEDURE [dbo].[uspInsertDelegate] ( @CourseID int,
I have the code below: string SQL = select * from + TableName; using
I have an SQL query (below) that essentially takes a student from tbStudents, and
I have a T-SQL query as below which queries a table holding the search
I have a problem with the SQL statement detailed below. The query returns the
I have a stored procedure which executes some dynamic sql, shown below. I've tried
I have an ASP.NET GridView which is populated from a SQL Server database. This
I have a SQL Server table-valued function which is created by me I have
I have a SQL-Statement which uses a Check-Function which takes a quite long time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.