stored proc

arkiboys 9,696 Reputation points
2024-03-18T16:32:41.9133333+00:00

Hello,

I have a stored procedure which does the following:

1- filter tables and place data into #table1

2- filter tables and place data into #table2

...

Then at the end of the stored proc, select columns by joining these #tables.

I have several stored procedures which they all have the same steps above, i.e.

1

2

...

and the only difference between the stored procs is where the tables are joined

Question:

Instead of me doing copy/paste of the #tables in steps 1, 2, ...

how can I make this generic so that each stored procedure has mostly the join tables instead of all the creation of #tables, etc.?

Thank you

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,234 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,671 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,796 Reputation points
    2024-12-28T18:37:19.7233333+00:00

    If you use a CTE you would not need temp tables, and the optimizer could generate a better plan.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.