如何从单个存储过程中的多个表中获取数据?

时间:2013-05-31 14:53:28

标签: c# asp.net mysql visual-studio-2010

我有三个表table1,table2和table3。以下是我的存储过程。执行存储过程后,我得到了以下输出。

alter PROCEDURE test

    @SDate datetime,
    @EDate datetime

As

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    Select count(quoteid) as TotalQuote, sum(totalamount) as QuoteAmount from dbo.QuoteBase
    where CreatedOn BETWEEN @SDate AND @EDate

    select  count(salesorderid)as TotalOrders, sum(totalamount) as OrderAmount from dbo.SalesOrderBase Where
    CreatedOn BETWEEN @SDate AND @EDate

    select count(opportunityid)as TotalSales  from dbo.OpportunityBase Where
    CreatedOn BETWEEN @SDate AND @EDate


GO

输出 image

但我不想单独显示查询结果。

我如何完成这项工作,以便查询结果将在一行中?

1 个答案:

答案 0 :(得分:1)

SELECT * FROM (Select count(quoteid) as TotalQuote, sum(totalamount) as QuoteAmount from     dbo.QuoteBase where CreatedOn BETWEEN @SDate AND @EDate)
CROSS JOIN (select  count(salesorderid)as TotalOrders, sum(totalamount) as OrderAmount from dbo.SalesOrderBase Where CreatedOn BETWEEN @SDate AND @EDate)
CROSS JOIN (select count(opportunityid)as TotalSales  from dbo.OpportunityBase Where
CreatedOn BETWEEN @SDate AND @EDate)

或者更容易,

做一个:

SELECT * FROM table1, table2, table3

其中table1,2和3是您的选择。

希望有所帮助