2010-09-16 66 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. 一個語句創建一個查詢這兩個臨時表

請注意,如果你不給它一個參數,它會與空字符串「」的默認值,這將很可能不返回數據執行。

在我看來,處理存儲過程的可怕想法是我們計劃放棄其他報告解決方案的一個很好的理由。