English 中文(简体)
TSQL: union results from two selects (procedures?)
原标题:

I have two procedures - two huge sets of selects with several sub-select and unions. I need to union results from these procedures and I still need them to exists separately.

Something like that:

if @Param = 1 Then 
    PROCEDURE1
if @Param = 2 THEN 
    PROCEDURE2
if @Param = 3 Then
    PROCEDURE1 union PROCEDURE2

I read that it s impossible to have union on procedures and I can t use temporary tables.

Any idea?

最佳回答

You can convert the procedures to views.

OR

You can exec the procedures into a temp table, and then exec the other one into the same temp table:

create table #sometable (table definition here)

if @Param = 1 or @Param = 3 begin
    insert #sometable exec PROCEDURE1
end

if @Param = 2 or @Param = 3 begin
    insert #sometable exec PROCEDURE2
end

select * from #sometable
问题回答

You could either:

1) Insert the data into a temp table then SELECT from that:

--Define #t here, with correct schema to match results returned by each sproc
INSERT #t EXECUTE PROC1
INSERT #t EXECUTE PROC2
SELECT * FROM #t

2) Just return 2 resultsets from the sproc and let your calling code handle 2 resultsets

There are various ways to handle the situation:

  1. Table-Valued User Defined Functions (UDFs)
  2. Views
  3. Dynamic SQL
  4. Temp tables

The question lacks concrete details so it s hard to recommend one over another, much less provide a tailored answer. It s plausible that the logic could be performed within a single query.

Without the use of temp tables there are only two other ways that I can think of.

  1. Convert the s procs to views, if possible.

  2. Move the logic on which proc to call to your application (if one exists). Have it run each proc separately and combine the results.





相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签