繁体   English   中英

从存储过程中选择?

[英]SELECT FROM stored procedure?

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

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,所以我想知道是否有可能这样做?

我了解这已经超过3年了,但是如果其他人正在寻找这个问题的答案。 我不得不处理此报告平台Izenda,并发现存储过程的处理方式与“ sql”图标的输出方式有所不同。 当您选择sp作为数据源时,会发生以下情况

  1. 建立动态SQL
  2. 它创建两个临时表,其中包含sp返回的所有列
  3. 用存储过程的结果填充第一个临时表
  4. 第二个临时表中将填充结果以及输入参数的值。
  5. 创建一条查询这两个临时表的语句

请注意,如果不输入参数,它将以默认值空字符串''执行,这很可能不会返回任何数据。

我认为,处理存储过程的想法很糟糕,这是我们计划将其删除以用于其他报告解决方案的一个很好的理由。

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

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

有3种方法可以执行此操作,请参阅此博客文章 如果您事先知道输出,则可以在不进行远程查询的情况下进行。

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

以前没有使用过该工具,但是一个快速的Google提出了这个示例(不确定是否对您有帮助)

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 FROM能力”表(本质上为您提供了“参数化” ”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM