SELECT FROM存储过程?

时间:2010-09-16 20:51:59

标签: sql sql-server-2008

如果我在SQL Server 2008中有一个存储过程,我知道我可以从管理工作室运行它,如下所示:

exec rpt_myproc @include_all = 1, @start_date = '1/1/2010'

但我正在使用一个没有返回任何结果的即席查询工具。所以我让它给我运行它的SQL,然后返回:

SELECT DISTINCT TOP 100000
[dbo].[rpt_myproc].[company_name] AS 'company name',
[dbo].[rpt_myproc].[order_number] AS 'order number]
FROM [dbo].[rpt_myproc]
WHERE 
([dbo].[rpt_myproc].[PARAM_start_date] IN ('1/1/2010'))
AND ([dbo].[rpt_myproc].[PARAM_include_all] IN ('1'))

我不熟悉这种语法。这甚至可能吗? ad-hoc工具没有失败,但它可能吞噬了这个错误。然后,也许它只是给我一个速记,它将使用翻译到适当的语法。但如果是这样的话,为什么会以这种形式给我呢?

我似乎无法让SQL在Management Studio中执行,所以我想知道这样的事情是否可行?

4 个答案:

答案 0 :(得分:1)

我知道这已经超过3年了,但万一其他人正在寻找这个问题的答案。我不得不处理这个报告平台Izenda,并且发现存储过程的处理方式不同于" sql"图标。以下是选择sp作为数据源时发生的情况

  1. 动态sql是build
  2. 它创建了两个临时表,其中包含sp返回的所有列
  3. 第一个临时表填充了存储过程的结果
  4. 使用结果加上输入参数的值填充第二个临时表。
  5. 创建一个查询这两个临时表的语句
  6. 请注意,如果您没有为其提供参数,则会使用默认值为空字符串''这很可能不会返回任何数据。

    在我看来,处理存储过程的可怕想法是我们计划放弃其他报告解决方案的一个很好的理由。

答案 1 :(得分:0)

您可以将存储过程的第一个结果集插入临时表:

SELECT  *
INTO    #YourProc
FROM    OPENROWSET('SQLNCLI', 
            'server=SERVERNAME\INSTANCENAME;trusted_connection=yes',
            'set fmtonly off; exec rpt_myproc')

有三种方法可以执行此操作,请参阅this blog post。如果事先知道输出,则可以在没有远程查询的情况下完成。

答案 2 :(得分:0)

您使用的是什么工具?您应该能够指定查询类型(即SQL或存储过程等)

之前没有使用过该工具,但快速谷歌提出了这个例子(不确定它是否会对你有帮助)

Using a stored procedure in 5.x

This example uses a stored procedure to populate a table before report design or execution. As shown in the comments, the table StoredProcResults must already exist. Every time a report is created or viewed this stored procedure will update the results of the StoredProcResults table. For 6.x follow these instructions but treat the SP as a regular datasource.
// Customize a report on the fly prior to execution on a per user basis

public override void PreExecuteReportSet(Izenda.AdHoc.ReportSet reportSet){    
    /*this sample uses the adventure works database    Here is the definition of the table and     stored procedure created for this report.     
        CREATE TABLE [dbo].[StoredProcResults](    
            [ProductID] [int] NOT NULL,    
            [OrderQuantity] [int] NOT NULL,    
            [Total] [int] NOT NULL,    
            [DueDate] [smalldatetime] NOT NULL    
        ) ON [PRIMARY]    

        CREATE PROCEDURE DoCustomAction (
            @date1 as smalldatetime,
            @date2 as smalldatetime    
        )   AS    
        BEGIN    
            insert into StoredProcResults    
            select ProductID,OrderQty,LineTotal,ModifiedDate    
            from Sales.SalesOrderDetail    
            where ModifiedDate >= @date1 and ModifiedDate <= @date2    
        END    
    */    

    string currentReportName =    HttpContext.Current.Request.QueryString["rn"];    
    if (currentReportName == "StoredProcExample")    {
        SqlConnection myConnection = new SqlConnection(Izenda.AdHoc.AdHocSettings.SqlServerConnectionString);        
        SqlCommand myCommand = new SqlCommand("DoCustomAction", myConnection);        
        // Mark the Command as a SPROC        
        myCommand.CommandType = System.Data.CommandType.StoredProcedure;        
        // Add Parameters to SPROC        
        SqlParameter parameterdate1 = new SqlParameter("@date1", System.Data.SqlDbType.SmallDateTime);        
        parameterdate1.Value = "1/1/2003";        
        myCommand.Parameters.Add(parameterdate1);        
        SqlParameter parameterdate2 = new SqlParameter("@date2", System.Data.SqlDbType.SmallDateTime);        
        parameterdate2.Value = "12/31/2003";        
        myCommand.Parameters.Add(parameterdate2);        

        try{            
            myConnection.Open();            
            myCommand.ExecuteNonQuery();        
        }
        finally{            
            myConnection.Close();        
        }    
    }
}

答案 3 :(得分:0)

你确定它是一个特色吗?我从来没有听过或看过从sproc中直接选择的用法。

已经看到的工作和功能与您的代码似乎正常工作的是表值函数,它们是函数,可以获取参数并返回“SELECT FROM able”表就像这样(实质上是给你一个'参数化'视图)。