2010-09-16 68 views
0

如果我有在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')) 

我不熟悉那个语法。这甚至有可能吗?临时工具没有失败,但它可能正在吞咽该错误。然后,也许它只是给我一个速记,它会在晚些时候使用翻译到正确的语法。但如果是这样,为什么它会以这种形式给我?

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

回答

0

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

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

没有比3的方式来做到这一点,看到this blog post。如果您事先知道输出,则可以在没有远程查询的情况下执行此操作。

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();   
     }  
    } 
} 
+0

它被称为Izenda AdHoc报告。它执行一些SQL生成和制图,但陪审团仍然没有确定它的实际价值。 – LoveMeSomeCode 2010-09-17 16:54:04

0

你确定它是一个sproc吗?我从来没有听说过或看到过使用从sproc直接选择的方法。

有什么看到,工作和功能完全一样的代码似乎工作是表值函数,这是功能,即可以带参数,并返回一个“SELECT FROM能够”表就像这样(本质上给你一个'参数化'的看法)。

1

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

  1. 动态SQL是建立
  2. ,它创建的所有列的两个临时表,你的SP将返回
  3. 第一个临时表是人口稠密与您的存储过程的结果
  4. 第二个临时表将填充结果加上输入参数的值。
  5. 一个语句创建一个查询这两个临时表

请注意,如果你不给它一个参数,它会与空字符串“”的默认值,这将很可能不返回数据执行。

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