Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2006
    Posts
    55

    Unanswered: if statement inside a stored procedure!!!!

    hi all,
    i'm wondering if i can use one stored procedure in too cases, this is the senario:
    i have stored procedure for admin and another one for user, they have the same select everything is the same except that in admin SP i have where @admin = admin and for user i have where @user = user
    if there a way to use if and else to make this happen
    this is what i did so far:

    CREATE PROCEDURE [test] @admin INT, @user INT, @indexType INT as
    if @indexType = 1

    begin

    SELECT * FROM table WHERE something IN (SELECT * FROM anothertable where admin = @admin)
    end
    else
    begin
    SELECT * FROM table WHERE user = @user
    end
    GO

    any suggestion will be very helpful
    thanks
    Peace out
    Mr joka

  2. #2
    Join Date
    Oct 2006
    Posts
    55
    i did try it in query analyser but it seems that it read always the first query and not the second one.

    thanks
    Peace out
    Mr joka

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Try either of these two methods:

    Code:
    SELECT	*
    FROM	yourtable
    WHERE	user = @user
    	or something in (SELECT something FROM anothertable where admin = @admin)
    
    SELECT	*
    FROM	yourtable
    WHERE	user = @user
    UNION
    SELECT	yourtable.*
    FROM	yourtable
    	inner join anothertable on yourtable.something = anothertable.something
    WHERE	anothertable.admin = @admin)
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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