Results 1 to 4 of 4

Thread: SQL stored procedure question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    SQL stored procedure question

    Is it possible to have an entire sql select statement as the input variable to a stored procedure? I want the stored procedure to execute the select statement.

    ie.

    exec sp_SomeFunc 'select * from table1 where id=1'

    It may sound weird, but I have my reason for wanting to do it this way. Is this possible? if so, how do I implement this inside the stored procedure? Thanks.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: SQL stored procedure question

    If you want to do that, what is the point of having a stored procedure at all?

    I recommend explaining what you actually want to do (rather than how you want to do it), as there is likely to be a much better way to do what you want.

  3. #3
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: SQL stored procedure question

    It's certainly possible. You'd just pas the statement in as a parameter (I'd suggest a VarChar(Max)) and execute it as dynamic sql using sp_ExceuteSQL.

    I agree with Si, though. This has no advantages at all over simply executing the statement. It won't perform any better, it will require all the same table permissions and security, it is, in effect, exactly the same as having no stored procedure at all.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL stored procedure question

    Due to the nature of our platform, sometimes it is necessary to run dynamic sql statements... that said, i've never had the need to pass the entire query as a parameter, i'll try to pass the individual pieces over, and let the target sproc assemble the query and execute it. At anyrate, it's possible.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width