SELECT FROM stored procedure?

后端 未结 4 1732
我寻月下人不归
我寻月下人不归 2020-12-22 12:22

If I have a stored proc in SQL Server 2008, I know I can run it from management studio like so:

exec rpt_myproc @include_all = 1, @start_date = \'1/1/2010\'
         


        
相关标签:
4条回答
  • 2020-12-22 12:46

    Are you sure it is a sproc? I've never heard or seen a usage of doing a direct select from a sproc.

    What I have seen that works and functions exactly as your code seems to be working is table-valued functions, which are functions, that can take parameters and return a "SELECT FROMable" table just like this (in essence giving you a 'parameterized' view).

    0 讨论(0)
  • 2020-12-22 13:00

    What tool are you using? You should be able to specify the query type (i.e. SQL, or stored proc, etc)

    Haven't used that tool before but a quick google came up with this example (not sure if it will help you)

    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 讨论(0)
  • 2020-12-22 13:05

    You can insert the first result set of a stored procedure into a temporary table:

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

    There's like 3 ways to do this, see this blog post. If you know the output beforehand, you can do it without the remote query.

    0 讨论(0)
  • 2020-12-22 13:12

    I understand that this is more than 3 years old, but in case anybody else is looking for an answer to this question. I had to deal with this reporting platform, Izenda, and have found that stored procedures are treated differently than the output from the "sql" icon. Here is what happens when you select sp as data source

    1. A dynamic sql is build
    2. It creates a two temporary tables with all of the columns that your sp is returning
    3. The first temp table is populated with the result from your stored procedure
    4. The second temp table is populated with the result plus the value of your input parameter.
    5. A statement is created that queries these two temporary tables

    Please note that if you don't feed it a parameter it will execute with a default value of empty string '' which will most likely return no data.

    In my opinion, horrible idea to handle stored procs which is a good reason why we are planning to drop them for some other reporting solution.

    0 讨论(0)
提交回复
热议问题