Sql 从存储过程中选择?

Sql 从存储过程中选择?,sql,sql-server-2008,Sql,Sql Server 2008,如果我在SQL Server 2008中有一个存储过程,我知道我可以从management studio运行它,如下所示: exec rpt_myproc @include_all = 1, @start_date = '1/1/2010' 但我使用的是一个不返回任何结果的特殊查询工具。所以我让它给我它正在运行的SQL,它会返回: SELECT DISTINCT TOP 100000 [dbo].[rpt_myproc].[company_name] AS 'company name', [d

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

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'))
我不熟悉这种语法。这可能吗?这个特别的工具并没有失败,但它可能正在吞咽这个错误。再说一遍,也许它只是给了我一个速记,稍后它会用它翻译成正确的语法。但如果是这样,为什么它会以这种形式给我呢


我似乎无法在Management Studio中执行该SQL,因此我想知道是否可以执行类似的操作?

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

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

有三种方法可以做到这一点,请参阅。如果您事先知道输出,则无需远程查询即可完成。

您使用的是什么工具?您应该能够指定查询类型,即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();        
        }    
    }
}

你确定这是一场狂欢吗?我从未听说或见过从存储过程中直接选择的用法


我所看到的与代码工作方式完全相同的函数是表值函数,这些函数可以获取参数并返回一个SELECT FROMable表,本质上就是这样,给你一个“参数化”视图。

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

构建了一个动态sql 它将创建一个包含sp返回的所有列的两个临时表 第一个临时表由存储过程的结果填充 第二个临时表将填充结果加上输入参数的值。 将创建查询这两个临时表的语句 请注意,如果不向其提供参数,它将使用默认值空字符串执行,该值很可能不会返回任何数据


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

它被称为Izenda临时报告。它进行了一些SQL生成和图表绘制,但它的实际价值还没有定论。