Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Transact SQL conditional Select

Transact SQL conditional Select

Scheduled Pinned Locked Moved Database
databasehelptutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    butchzn
    wrote on last edited by
    #1

    Hi Everybody :) , How would i execute 2 select statements in 1 stored procedure using TRANSACT-SQL? The execution of the 2nd procedure is conditional and based on whether the 1st statement returns any values or not? Essentially as an example: select * from cupboard where cupColour = 'white' if cupboard.CupsFound = 0 then select * from cupboard end if return dataset Any help would be greatly appreciated...i will continue to search and read :)

    L P 2 Replies Last reply
    0
    • B butchzn

      Hi Everybody :) , How would i execute 2 select statements in 1 stored procedure using TRANSACT-SQL? The execution of the 2nd procedure is conditional and based on whether the 1st statement returns any values or not? Essentially as an example: select * from cupboard where cupColour = 'white' if cupboard.CupsFound = 0 then select * from cupboard end if return dataset Any help would be greatly appreciated...i will continue to search and read :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      butchzn wrote:

      How would i execute 2 select statements in 1 stored procedure using TRANSACT-SQL?

      One at a time :) Try an

      IF EXISTS()
      BEGIN

      END

      I are Troll :suss:

      B 1 Reply Last reply
      0
      • L Lost User

        butchzn wrote:

        How would i execute 2 select statements in 1 stored procedure using TRANSACT-SQL?

        One at a time :) Try an

        IF EXISTS()
        BEGIN

        END

        I are Troll :suss:

        B Offline
        B Offline
        butchzn
        wrote on last edited by
        #3

        Hi Thanks for bumping me in the right direction i have managed to solve it with your input! ALTER PROC [dbo].[FindCupByCupIdAndDescription] @CupID int, @CupDescription nvarchar(50) AS IF EXISTS(SELECT CupID FROM Cupboard WHERE CupId = @CupId) SELECT * FROM Cupboard WHERE CupId = @CupId ELSE SELECT * FROM Attic WHERE CupDescription LIKE '%' + @CupDescription + '%' this is a really simplified example but is essentially the logical illustration of what i was hoping to achieve using a result as a condition. :-D Thanks you.

        L M 2 Replies Last reply
        0
        • B butchzn

          Hi Thanks for bumping me in the right direction i have managed to solve it with your input! ALTER PROC [dbo].[FindCupByCupIdAndDescription] @CupID int, @CupDescription nvarchar(50) AS IF EXISTS(SELECT CupID FROM Cupboard WHERE CupId = @CupId) SELECT * FROM Cupboard WHERE CupId = @CupId ELSE SELECT * FROM Attic WHERE CupDescription LIKE '%' + @CupDescription + '%' this is a really simplified example but is essentially the logical illustration of what i was hoping to achieve using a result as a condition. :-D Thanks you.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You're welcome :)

          1 Reply Last reply
          0
          • B butchzn

            Hi Thanks for bumping me in the right direction i have managed to solve it with your input! ALTER PROC [dbo].[FindCupByCupIdAndDescription] @CupID int, @CupDescription nvarchar(50) AS IF EXISTS(SELECT CupID FROM Cupboard WHERE CupId = @CupId) SELECT * FROM Cupboard WHERE CupId = @CupId ELSE SELECT * FROM Attic WHERE CupDescription LIKE '%' + @CupDescription + '%' this is a really simplified example but is essentially the logical illustration of what i was hoping to achieve using a result as a condition. :-D Thanks you.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            Assuming that you want to use either ID or description and not both (does not make sense) then the following will do the job with a conditional where caluse.

            SELECT *
            FROM TableName
            WHERE (ISNULL(@CupID,0)=0 OR CupID = @CupID)
            AND (ISNULL(@CupDesc,'') = '' OR CupDesc = @Cupdesc)

            There are a number of variations on this, search for "conditional where clause" Caveat, string too many of these together (5+) and SQL Server gets very cranky

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • B butchzn

              Hi Everybody :) , How would i execute 2 select statements in 1 stored procedure using TRANSACT-SQL? The execution of the 2nd procedure is conditional and based on whether the 1st statement returns any values or not? Essentially as an example: select * from cupboard where cupColour = 'white' if cupboard.CupsFound = 0 then select * from cupboard end if return dataset Any help would be greatly appreciated...i will continue to search and read :)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              SELECT T2.* FROM (SELECT TOP(1) Color FROM Cabinet WHERE Color='Yellow') T1 INNER JOIN Cabinet T2 ON 0=0 :-D

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups