Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Stored Proc Question

  • 22-07-2010 07:54AM
    #1
    Closed Accounts Posts: 1,759 ✭✭✭


    Hi,

    If I have a stored proc which outputs say an integer.
    How do I use the output of that as an input parameter into another query.

    for e.g

    select * from table_1 where table_1.mycolumn
    = "Result from ...execute stored procedure"


    Many thanks


Comments

  • Closed Accounts Posts: 162 ✭✭totoal


    Dr.Silly wrote: »
    Hi,

    If I have a stored proc which outputs say an integer.
    How do I use the output of that as an input parameter into another query.

    for e.g

    select * from table_1 where table_1.mycolumn
    = "Result from ...execute stored procedure"


    Many thanks

    Guessing you would run the stored proc and store value in a variable and then use the variable in the Where clause...
    or am I missing out on something?


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    I don't know if you can put it in the actual query and it might be different for other databases. I use MS SQL and would do the following...
    declare @proc_return_value int
    exec @proc_return_value = MyProc
    select * from SomeTable where SomeColumn = @proc_return_value
    

    I don't think you can call a stored proc inline like you've described.


  • Registered Users, Registered Users 2 Posts: 169 ✭✭DonnieBrasco


    a function would do the business


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    you could also use the output parameter


  • Registered Users, Registered Users 2 Posts: 147 ✭✭BeautifulLoser


    You could do something like:
    1. Create a temp table for the result of the sproc
    2. Exec the stored procedure into your temp table
    3. Inner join from your table to the temp table.
    CREATE TABLE #Temp
    (
     SprocResult int
    )
    
    INSERT #Temp
    Exec MySproc 
    
    Select 
    *
    From 
    table_1
    Inner Join #Temp on #Temp.SprocResult = table_1.mycolumn
    

    If your stored procedure only returns a single int it might be best rewritten as a User Defined Function (as suggested)
    Declare @MyInt as int
    
    Set @MyInt = dbo.YourUserDefinedFunctionName()
    
    Select 
    *
    From 
    table_1
    Where
    table_1.mycolumn = @MyInt
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    BeautifulLoser,

    If he makes a user defined function, then
    select * from SomeTable where SomeColumn = Somefunction();
    

    will work.


  • Closed Accounts Posts: 1,759 ✭✭✭Dr.Silly


    thanks chaps


  • Registered Users, Registered Users 2 Posts: 147 ✭✭BeautifulLoser


    fasty wrote: »
    BeautifulLoser,

    If he makes a user defined function, then
    select * from SomeTable where SomeColumn = Somefunction();
    

    will work.

    This will definitely work but I thought that having a udf in the Where clause might be less efficient..I could very well be wrong..


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    Beautiful user why go to the expense of creating a temporary table?


  • Registered Users, Registered Users 2 Posts: 2,800 ✭✭✭voxpop


    Functions can be pretty bad performance wise. In a large select they will be called for each row the select returns.

    Better setting the value to a variable and then using that.


    Edit: Actually found this article ->sql article - suggests that using a table-valued UDF or a scalar UDF would avoid most of the performance drawbacks


  • Advertisement
Advertisement