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 8081581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:46:35+00:00 2026-06-05T16:46:35+00:00

I am trying to make an SQL Function that returns a nvarchar(MAX) string. The

  • 0

I am trying to make an SQL Function that returns a nvarchar(MAX) string. The function uses a CTE recursive WITH statement. I can get all the parts to work correctly when they are not in the function but when I put them all together I get the following error:

Msg 208, Level 16, State 1, Line 1
Invalid object name ‘drillUp’.

The function itself does not give an error during “Compile” but only when it is run.
If I change the SET @RET to “TESTING…” instead of calling the SELECT statement it runs as expected.

I am fairly new to SQL and I am not sure what I am doing wrong. If anyone can give me some advice I would greatly appreciate it.

Thank you.

Joe

CREATE FUNCTION BuildName(@SUBTRAIT_ID int)
RETURNS nvarchar(MAX)
AS
BEGIN
DECLARE @RET nvarchar(MAX)
DECLARE @TEXTMODE nvarchar(50)
DECLARE @PARENTID int
DECLARE @CHECKDROPDOWN TABLE
(
    TextMode nvarchar(MAX),
    ParentId int
);

WITH drillUp(FullName, ParentId, depth) 
AS
(
    SELECT  
        SubValue,
        ParentId,
        0           
    FROM SubTrait
    WHERE Id = @SUBTRAIT_ID 
    UNION ALL
    SELECT                  
        child.SubValue + ' -> ' + drillUp.FullName,
        child.ParentId,
        drillUp.depth + 1
    FROM SubTrait child, drillUp
    WHERE drillUp.ParentId = child.Id 
)


INSERT @CHECKDROPDOWN 
SELECT TextMode, ParentId FROM SubTrait WHERE Id = @SUBTRAIT_ID

SET @TEXTMODE = (SELECT MAX(TextMode) FROM @CHECKDROPDOWN);
SET @PARENTID = (SELECT MAX(ParentId) FROM @CHECKDROPDOWN);

IF (@TEXTMODE != 'DropDown')
BEGIN
    SET @SUBTRAIT_ID = @PARENTID
END 

SET @RET = (
       SELECT FullName
       FROM drillUp du
       WHERE depth =(SELECT MAX(depth) FROM drillUp ) - 1
     )
--  SET @RET = 'TESTING...'

RETURN (@RET)

END;
GO


PRINT dbo.BuildName(77)
  • 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-05T16:46:36+00:00Added an answer on June 5, 2026 at 4:46 pm

    CTE’s are only valid to be used on the statement that follows his definition. So you should move your SET @RET to be next the definition of drillup:

    CREATE FUNCTION BuildName(@SUBTRAIT_ID int)
    RETURNS nvarchar(MAX)
    AS
    BEGIN
    DECLARE @RET nvarchar(MAX)
    DECLARE @TEXTMODE nvarchar(50)
    DECLARE @PARENTID int
    DECLARE @CHECKDROPDOWN TABLE
    (
        TextMode nvarchar(MAX),
        ParentId int
    );
    
    INSERT @CHECKDROPDOWN 
    SELECT TextMode, ParentId FROM SubTrait WHERE Id = 74
    
    SET @TEXTMODE = (SELECT MAX(TextMode) FROM @CHECKDROPDOWN);
    SET @PARENTID = (SELECT MAX(ParentId) FROM @CHECKDROPDOWN);
    
    IF (@TEXTMODE != 'DropDown')
    BEGIN
        SET @SUBTRAIT_ID = @PARENTID
    END 
    
    WITH drillUp(FullName, ParentId, depth) 
    AS
    (
        SELECT  
            SubValue,
            ParentId,
            0           
        FROM SubTrait
        WHERE Id = @SUBTRAIT_ID 
        UNION ALL
        SELECT                  
            child.SubValue + ' -> ' + drillUp.FullName,
            child.ParentId,
            drillUp.depth + 1
        FROM SubTrait child, drillUp
        WHERE drillUp.ParentId = child.Id 
    )
    SELECT @RET = (
           SELECT FullName
           FROM drillUp du
           WHERE depth =(SELECT MAX(depth) FROM drillUp ) - 1
         )
    --  SET @RET = 'TESTING...'
    
    
    RETURN (@RET)
    
    END;
    GO
    
    
    PRINT dbo.BuildName(77)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a SQL statement that gives me the top X records
I am trying to make a query to an SQL database and I can't
I am trying to make a webpage that connects to my SQL database and
I'm trying to write a stored procedure in SQL that will : Make a
I'm trying to make a PHP (5) object that can iterate through its properties,
I'm trying to make a Teradata SQL query that will return the n -th
I'm just trying to make a simple function that will return all the data
I'm trying to make a select statement that joins results from three in-line table
I trying to make a sql script that will randomize the city, state, and
I'm new to using MVC and I'm trying to make a model that uses

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.