Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Stored Proc Question

  • 22-07-2010 6: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