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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:04:06+00:00 2026-05-11T02:04:06+00:00

This is more of a syntax question I’m trying to write a store procedure

  • 0

This is more of a syntax question I’m trying to write a store procedure or function that I can embed into a query such as:

select * from MyBigProcOrFunction 

I’m trying to define a tabular function but I do not understand how to do it as I build tmp tables to work out the data before I finally have the return at the endtable. My mark up for my code is:

create function FnGetCompanyIdWithCategories() returns table as  return  ( select * into a #tempTable from stuff ' etc ' select companyid,Company_MarketSector from #tempTables 'the returning table data ) 

If I define a function, How do I return it as a table?

  • 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. 2026-05-11T02:04:06+00:00Added an answer on May 11, 2026 at 2:04 am

    You can’t access Temporary Tables from within a SQL Function. You will need to use table variables so essentially:

    ALTER FUNCTION FnGetCompanyIdWithCategories() RETURNS  @rtnTable TABLE  (     -- columns returned by the function     ID UNIQUEIDENTIFIER NOT NULL,     Name nvarchar(255) NOT NULL ) AS BEGIN DECLARE @TempTable table (id uniqueidentifier, name nvarchar(255)....)  insert into @myTable  select from your stuff  --This select returns data insert into @rtnTable SELECT ID, name FROM @mytable  return END 

    Edit

    Based on comments to this question here is my recommendation. You want to join the results of either a procedure or table-valued function in another query. I will show you how you can do it then you pick the one you prefer. I am going to be using sample code from one of my schemas, but you should be able to adapt it. Both are viable solutions first with a stored procedure.

    declare @table as table (id int, name nvarchar(50),templateid int,account nvarchar(50))  insert into @table execute industry_getall  select *  from @table  inner join [user]      on account=[user].loginname 

    In this case, you have to declare a temporary table or table variable to store the results of the procedure. Now Let’s look at how you would do this if you were using a UDF

    select * from fn_Industry_GetAll() inner join [user]      on account=[user].loginname 

    As you can see the UDF is a lot more concise easier to read, and probably performs a little bit better since you’re not using the secondary temporary table (performance is a complete guess on my part).

    If you’re going to be reusing your function/procedure in lots of other places, I think the UDF is your best choice. The only catch is you will have to stop using #Temp tables and use table variables. Unless you’re indexing your temp table, there should be no issue, and you will be using the tempDb less since table variables are kept in memory.

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

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe this could help: jQuery Quick Search May 11, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer How to kill a Thread is a difficult question. There… May 11, 2026 at 6:15 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure I understand why you need anything special.… May 11, 2026 at 6:15 pm

Related Questions

This is more of a syntax question I'm trying to write a store procedure
This is a follow-up to my question from yesterday . I have Scott Meyers'
I have the following snippet of Javascript, which is just attaching an onclick to
I have a jQuery .ajax call: $j.ajax({ type: POST, url: /Services/Cart.asmx/UpdateQuantity, data: data, contentType:
I decide to learn more about vim and its syntax highlighting. Using examples for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.