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

  • SEARCH
  • Home
  • 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 6132111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:40+00:00 2026-05-23T17:03:40+00:00

I am trying to write a function to support some (legacy) SQL code, function

  • 0

I am trying to write a function to support some (legacy) SQL code, function will be used as a part of a query:

SELECT
  q.*,
  fn_qncatxml(q.number, q.version) AS categories --XML aggregated category hierarchy
FROM
  [dbo].[qn] q

Unfortunately following approach does not work as SQL Server has some known problems with CTE expressions in a function (semicolon before WITH solution). Unfortunately I’m not sure how to apply this solution to a function returning scalar, like this:

CREATE FUNCTION [dbo].[fn_qncatxml](@qnnumber INT, @qnversion INT)
RETURNS XML
WITH RETURNS NULL ON NULL INPUT
BEGIN
  RETURN
  (
    WITH [categories] AS
    (
      SELECT [qn].[number], [qn].[version], [cat].[catnumber], [qncat].[itemnumber], [cat].[parent], [cat].[description], 0 AS [distance]
      FROM [qn]
      LEFT JOIN [qncat] ON [qncat].[qnnumber] = [qn].[number] AND [qncat].[qnversion] = [qn].[version]
      LEFT JOIN [cat] ON [cat].[catnumber] = [qncat].[catnumber] AND [cat].[status] = '1'
      WHERE [qn].[number] = @qnnumber AND [qn].[version] = @qnversion

      UNION ALL

      SELECT [categories].[number], [categories].[version], [cat].[catnumber], [categories].[itemnumber], [cat].[parent], [cat].[description], [categories].[distance] + 1 AS [distance]
      FROM [categories]
      JOIN [cat] ON [cat].[catnumber] = [categories].[parent]
      WHERE
        [cat].[status] = '1'
    )
    SELECT DISTINCT * FROM [categories] FOR XML PATH('')
  )
END

When I’ll strip a part between WITH and FOR XML PATH(”), give some valid arguments and execute this block, it works perfectly.

Has anyone a clue how to make this CTE function syntax work in my scenario ?

  • 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-05-23T17:03:41+00:00Added an answer on May 23, 2026 at 5:03 pm
    CREATE FUNCTION [dbo].[fn_qncatxml](@qnnumber INT, @qnversion INT)
    RETURNS XML
    WITH RETURNS NULL ON NULL INPUT
    BEGIN
      DECLARE @Ret xml;
    
      WITH [categories] AS
      (
        SELECT [qn].[number], [qn].[version], [cat].[catnumber], [qncat].[itemnumber], [cat].[parent], [cat].[description], 0 AS [distance]
        FROM [qn]
        LEFT JOIN [qncat] ON [qncat].[qnnumber] = [qn].[number] AND [qncat].[qnversion] = [qn].[version]
        LEFT JOIN [cat] ON [cat].[catnumber] = [qncat].[catnumber] AND [cat].[status] = '1'
        WHERE [qn].[number] = @qnnumber AND [qn].[version] = @qnversion
    
        UNION ALL
    
        SELECT [categories].[number], [categories].[version], [cat].[catnumber], [categories].[itemnumber], [cat].[parent], [cat].[description], [categories].[distance] + 1 AS [distance]
        FROM [categories]
        JOIN [cat] ON [cat].[catnumber] = [categories].[parent]
        WHERE
          [cat].[status] = '1'
      )
      SELECT @Ret = (SELECT DISTINCT * FROM [categories] FOR XML PATH(''))
    
      RETURN @Ret
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a function that will pull the name of a
i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that will let me red-shift or blue-shift a
I'm trying to write a little WordPress plugin to support some migrated content. The
I am trying to write a c# search function that supports % in SQL
Im trying to write a function for adding category: function addCategory() { $cname =
I'm trying to write a function that formats every (string) member/variable in an object,
I'm trying to write a function that is able to determine whether a string
I'm trying to write a function in assembly (but lets assume language agnostic for
I am trying to write a function in C++ that solves for X using

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.