簡體   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