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

The Archive Base Latest Questions

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

I want to call the Stored Procedure which returns a table in the Table

  • 0

I want to call the Stored Procedure which returns a table in the Table Valued Function. How to create the Table Valued Function?

For Example: “sp_GetTable” is the Stored procedure. It returns a table.
Here, I want to write one function called “fn_GetTable”

I want to get the same result which has given by Stored Procedure.

Here is my Stored Procedure:

ALTER PROC sp_GetTable
AS
BEGIN

------------ Creating TempTable(#MasterTable) ----------------------
    CREATE TABLE #MasterTable
    (ItemID INT, ItemName VARCHAR(100), Specifications VARCHAR(100), D1 VARCHAR(50),   D2 VARCHAR(50), D3 VARCHAR(50), 
        D1_Code VARCHAR(50), D2_Code VARCHAR(50), D3_Code VARCHAR(50))

------------ Creating TempTable(#TempItems) ----------------------

    CREATE TABLE #TempItems(ItemID INT, ItemName VARCHAR(100), Specifications VARCHAR(100), D1 VARCHAR(50), D2 VARCHAR(50), D3 VARCHAR(50),
                 D1_Code VARCHAR(50), D2_Code VARCHAR(50), D3_Code VARCHAR(50))
------------ Creating & Inserting TempTable(@Dim) ----------------------

    DECLARE @Dim TABLE (DCodes VARCHAR(100))
    INSERT INTO @Dim SELECT  D1_Code FROM MAS_SizeType WHERE  D1_Code <> ''
    INSERT INTO @Dim SELECT  D2_Code FROM MAS_SizeType WHERE  D2_Code <> ''
    INSERT INTO @Dim SELECT  D3_Code FROM MAS_SizeType WHERE  D3_Code <> ''

------------ Inserting data into TempTable(#MasterTable) ----------------------

    INSERT INTO #MasterTable
    SELECT     STR_Item.ItemID, STR_Item.ItemName, STR_Item_Specifications.SpecificationName, 
        STR_Item.D1, STR_Item.D2, STR_Item.D3, MAS_SizeType.D1_Code, MAS_SizeType.D2_Code, 
                  MAS_SizeType.D3_Code
    FROM         STR_Item INNER JOIN
                  MAS_SizeType ON STR_Item.SizeTypeID = MAS_SizeType.SizeTypeID INNER JOIN
                  STR_Item_Specifications ON STR_Item.SpecificationID = STR_Item_Specifications.SpecificationID

-------------------- Inserting Data into #TempItems Table ----------------------------
INSERT INTO #TempItems
SELECT 
      *
FROM #MasterTable

------------------Cursor for All dimensions details into single row for each items ----------------------

   DECLARE @DCode VARCHAR(MAX), @Cnt INT
   SET @Cnt = 0

   DECLARE ColAdd CURSOR FOR
   SELECT DISTINCT DCodes FROM @Dim
   OPEN ColAdd
   FETCH NEXT FROM ColAdd INTO @DCode
   WHILE (@@FETCH_STATUS = 0)
   BEGIN
  SET @Cnt = 1
  EXECUTE ('ALTER TABLE #TempItems ADD ' + @DCode + ' VARCHAR(50)')
  EXECUTE('UPDATE #TempItems SET [' + @DCode + '] = M.D1
                FROM #MasterTable M 
            INNER JOIN #TempItems T ON T.ItemID = M.ItemID AND M.D1_Code = ''' + @DCode + '''')
  EXECUTE('UPDATE #TempItems SET [' + @DCode + '] = M.D2
                FROM #MasterTable M 
            INNER JOIN #TempItems T ON T.ItemID = M.ItemID AND M.D2_Code = ''' + @DCode + '''')
  EXECUTE('UPDATE #TempItems SET [' + @DCode + '] = M.D3
                FROM #MasterTable M 
            INNER JOIN #TempItems T ON T.ItemID = M.ItemID AND M.D3_Code = ''' + @DCode + '''')


    FETCH NEXT FROM ColAdd INTO @DCode
    END
    CLOSE ColAdd
    DEALLOCATE ColAdd
    IF (@Cnt = 1)
    BEGIN

    SELECT  @DCode = STUFF(( SELECT  DISTINCT '],[' + ltrim(DCodes) 
                        FROM  @Dim 
                        ORDER BY '],[' + ltrim(DCodes)
                        FOR XML PATH('')

                                    ), 1, 2, '') + ']'

   EXECUTE ('SELECT ItemID, ItemName, Specifications, ' + @DCode + ' FROM #TempItems ORDER BY ItemName') 
   END

    DROP TABLE #TempItems
    DROP TABLE #MasterTable

END
  • 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-23T03:03:33+00:00Added an answer on May 23, 2026 at 3:03 am

    It’s possible to do this from within a Scalar Function, not possible to do from a Table Valued Function. Scalar-valued Functions return a single data-type value and cannot return tables either. Since you cannot select from a stored procedure (you need to EXEC) there is no place for this in a table-valued function.

    In general, if the stored procedure
    returns a, single, result set, define
    a table-valued function. If the stored
    procedure computes a scalar value,
    define a scalar function.

    From: Rewriting Stored Procedures as Functions

    Re-writing the Stored Procedure as a Table-Valued Function will yield you the most performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call a function (which calculates my version number) when my NSIS
How can I create an Oracle stored procedure which accepts a variable number of
I have sql stored procedure which pass and return some values: CREATE PROCEDURE [dbo].[GetMoney]
In my stored procedure I have to pass a table and column name which
In my C# application, I have a stored procedure which returns some kind of
I want to call a function from a .NET DLL (coded in C#) from
I want to call a c# function from my javascript function. I have a
A simple question about Stored Procedures. I have one stored procedure collecting a whole
I'm having issues when trying to call a MySQL (5.0.77) stored procedure with parameters,
I want to call only stored procedures on database and fill my C# object

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.