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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:10:18+00:00 2026-06-18T08:10:18+00:00

Create FUNCTION [dbo].[fn_GetStockDeatils]() RETURNS @Results TABLE ( PurchaseData nvarchar(50) NOT NULL ) AS BEGIN

  • 0
Create FUNCTION [dbo].[fn_GetStockDeatils]()
RETURNS @Results TABLE
(
PurchaseData nvarchar(50) NOT NULL 

)
AS
    BEGIN


    Declare @tableName varchar(25)
    Declare @PKKey numeric(18,0)

    DECLARE StockCursor CURSOR FOR Select tableName ,PKKey from INVM 
    OPEN StockCursor
    FETCH StockCursor INTO @tableName ,@PKKey 
       Begin

         Insert @Results Select PurchaseData from @tableName.PKKey =@PKKey 

       END      
    FETCH StockCursor INTO @tableName ,@PKKey 
    close StockCursor
    DEALLOCATE StockCursor


        Return  

    END

I have already written this but it does not working properly. @tableName contains the name of the table. Please help me if possible.

  • 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-18T08:10:19+00:00Added an answer on June 18, 2026 at 8:10 am

    Sounds like you need an dynamic SQL commands, but you cannot use dynamic SQL from a function. For that you have to create stored procedure.

    CREATE PROCEDURE [dbo].[GetStockDeatils]
    AS
    BEGIN
    DECLARE @tableName varchar(25),
            @PKKey numeric(18,0),
            @dsql nvarchar(max) = N''
    
    IF OBJECT_ID('tempdb.dbo.#Results') IS NOT NULL DROP TABLE dbo.#Results
    CREATE TABLE dbo.#Results(PurchaseData nvarchar(50) NOT NULL)
    
    DECLARE StockCursor CURSOR LOCAL FAST_FORWARD FOR 
    SELECT tableName ,PKKey 
    FROM INVM 
    OPEN StockCursor
    FETCH NEXT FROM StockCursor INTO @tableName, @PKKey
    WHILE @@FETCH_STATUS = 0
    BEGIN 
      SET @dsql = 'Insert dbo.#Results Select PurchaseData from ' + @tableName + ' WHERE PKKey = ' + CAST(@PKKey AS varchar(10))
      EXEC sp_executesql @dsql  
      FETCH NEXT FROM StockCursor INTO @tableName, @PKKey 
    END
    CLOSE StockCursor
    DEALLOCATE StockCursor
    
    SELECT *
    FROM dbo.#Results
    END
    
    EXEC [dbo].[GetStockDeatils]
    

    Demo on SQLFiddle

    Procedure creating view

    CREATE PROCEDURE [dbo].[GetStockDeatils]
    AS
    BEGIN
    DECLARE @dsql nvarchar(max) = N''
    IF OBJECT_ID('dbo.v_Results') IS NOT NULL DROP VIEW dbo.v_Results
    SELECT @dsql += 
      'UNION ALL SELECT PurchaseData FROM ' + tableName + 
      ' WHERE PKKey = ' + CAST(PKKey AS varchar(10))                
    FROM dbo.INVM
    SET @dsql = N'CREATE VIEW dbo.v_Results AS ' + STUFF(@dsql, 1, 10, '')
    EXEC sp_executesql @dsql
    END
    
    EXEC [dbo].[GetStockDeatils]
    
    SELECT *
    FROM dbo.v_Results
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Create FUNCTION [dbo].[GetAllChildSpaces] ( @CustomSpaceId varchar(40), @Userid int ) RETURNS NVARCHAR(MAX) AS BEGIN DECLARE
CREATE FUNCTION [dbo].[MSG_FilterAutoship] ( @ItemID VARCHAR(50) = NULL ) RETURNS @Autoship TABLE ( DistID
I have this function: CREATE FUNCTION [dbo].[udf_StripHTML] (@HTMLText VARCHAR(MAX)) RETURNS VARCHAR(MAX) AS BEGIN DECLARE
So I have this working properly: CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE AS RETURN
here my code- create function dbo.emptable() returns Table as return (select id, name, salary
I have simplified my function to the following: create function [dbo].[UserSuperTeams](@ProjectId int) returns table
CREATE function [dbo].[fn_GetDateOnly](@dateWithTime datetime) returns datetime WITH SCHEMABINDING as begin return DATEADD(DAY, DATEDIFF(DAY, 0,
why error? CREATE FUNCTION [dbo].[seth] (@IdUrl BIGINT) RETURNS TABLE AS RETURN(SELECT * FROM dbo.NetGraph
I have the following function: CREATE FUNCTION [dbo].[ListStockBySubCategory] ( @CategoryID varchar(10), @SubCategoryID varchar(10), @startRowIndex
A few examples to show, just incase: Inline Table Valued CREATE FUNCTION MyNS.GetUnshippedOrders() RETURNS

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.