Results 1 to 9 of 9

Thread: Quick question on stored procedures.

  1. #1

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214

    Quick question on stored procedures.

    Hi All,

    I have a sp which I would like it to search on a certain table and then with it's results do anothr search on another table.

    so I have something like this.

    select ID from Table1

    select fName from Table2 WHERE ID = Table1ID

    see what I mean?
    Gavin Mannion

    Jackofalltrades.

    "Some people are alive simply because it is against the law to kill them." - Anonymous

    //TODO: Insert smart comment here

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Although you haven't actually posed a question, I think what you are trying to get at is this;

    SELECT * FROM Table2 WHERE Table2.ID IN (SELECT ID FROM Table1)

    This will give you all the records from table2 where there is an equivalent ID in Table1. Is that what you meant?
    You can add a WHERE caluse the the second SELECT statement if necessary.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Originally posted by Buzby
    Although you haven't actually posed a question, I think what you are trying to get at is this;

    SELECT * FROM Table2 WHERE Table2.ID IN (SELECT ID FROM Table1)

    This will give you all the records from table2 where there is an equivalent ID in Table1. Is that what you meant?
    You can add a WHERE caluse the the second SELECT statement if necessary.
    I did post a question, you have half answered it.

    Here is the full question.

    How do I use the results of my first query in the stroed procedure in the second query of my stored procedure.
    Gavin Mannion

    Jackofalltrades.

    "Some people are alive simply because it is against the law to kill them." - Anonymous

    //TODO: Insert smart comment here

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Right - do you mean set up a kinda subset of Table1, and base a query on that..? If you do;

    SELECT * FROM
    (SELECT * FROM Table1 WHERE ..... ) AS TempTable
    WHERE .......

    this will select some records from Table1 (based on the first WHERE clause), and then the outer query will select certain records from that subset of records, based on the 2nd WHERE clause.

    Is that what you mean?
    If not - please explain further.. we'll get there in the end.
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Or do you mean;

    DECLARE @Table1ID INTEGER

    SELECT @Table1ID = ID FROM Table1 WHERE .......
    SELECT * FROM Table2 WHERE ID = @Table1ID

    this stores the ID from table1 in @Table1ID, and then you can use it search for stuff in Table2

    Is that it?
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  6. #6

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Okay here is my exact problem ...

    I have the following sql statement

    SELECT LoginName FROM ssbt_ClinicStaff WHERE StaffID = @StaffID

    I am then calling a second stored procedure

    sp_DropLogin '?I need the loginname from the previous one?'

    how would I solve this problem?

    Thanks
    Gavin Mannion

    Jackofalltrades.

    "Some people are alive simply because it is against the law to kill them." - Anonymous

    //TODO: Insert smart comment here

  7. #7
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    DECLARE @Table1ID INTEGER

    SELECT @Table1ID = ID FROM Table1 WHERE .......
    SELECT * FROM Table2 WHERE ID = @Table1ID
    But then, if that's all you want then you can do this;

    SELECT Table2.* FROM Table2 INNER JOIN Table1 ON Table2.ID = Table1.ID
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  8. #8

    Thread Starter
    Addicted Member Gavin_Mannion's Avatar
    Join Date
    Aug 2001
    Location
    London, UK
    Posts
    214
    Originally posted by Buzby
    Or do you mean;

    DECLARE @Table1ID INTEGER

    SELECT @Table1ID = ID FROM Table1 WHERE .......
    SELECT * FROM Table2 WHERE ID = @Table1ID

    this stores the ID from table1 in @Table1ID, and then you can use it search for stuff in Table2

    Is that it?
    aha I think it is
    Gavin Mannion

    Jackofalltrades.

    "Some people are alive simply because it is against the law to kill them." - Anonymous

    //TODO: Insert smart comment here

  9. #9
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Well you just pass the @LoginName to the SP;

    DECLARE @LoginNAme VARCHAR(20)

    SELECT @LoginName = LoginName FROM .....

    EXEC sp_DropLogin @LoginName



    You set up sp_DropLogin like this;

    CREATE PROCEDURE sp_DropLogin

    @LoginName VARCHAR(20)

    AS

    ....
    and continue...
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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