views:

85

answers:

3

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'

But I'm using an ad-hoc query tool that wasn't returning any results. So I asked it to give me the SQL it was running and it returns this:

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'))

I'm not familiar with that syntax. Is that even possible? The ad-hoc tool isn't failing, but it may be swallowing that error. Then again, maybe it's just giving me a shorthand which it will use translate to the proper syntax later. But if so, why would it give it to me in this form?

I can't seem to get that SQL to execute in Management Studio, so I was wondering if something like that were possible?

A: 

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.

Andomar
A: 

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();        
        }    
    }
}
clyc
Its called Izenda AdHoc reporting. It does some SQL generation and charting, but the jury's still out on how valuable it actually is.
LoveMeSomeCode
A: 

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).

eidylon