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. Stored procedure query

Stored procedure query

Scheduled Pinned Locked Moved Database
database
4 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.
  • M Offline
    M Offline
    mehrdadov
    wrote on last edited by
    #1

    Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,

    Agh

    B L J 3 Replies Last reply
    0
    • M mehrdadov

      Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,

      Agh

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      here is the way how to build dynamic query in T-SQL

      declare @query as nvarchar(max)
      set @query = 'select * from mytable '

      if(@ParameterValueIS = 1)
      begin
      set @query = @query +' where id=1'
      end

      if(@ParameterValueIS = 2)
      begin
      set @query = @query +' where id=2'
      end

      exec (@query)


      I Love T-SQL "VB.NET is developed with C#.NET" If my post helps you kindly save my time by voting my post.

      1 Reply Last reply
      0
      • M mehrdadov

        Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,

        Agh

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

        mehrdadov wrote:

        I'm looking for a solution that let me depend on the situations make my select query with or without joins.

        Let me rephrase that, to make sure I understood you correctly; you're looking for something simpeler than a join-statement?

        Bastard Programmer from Hell :suss:

        1 Reply Last reply
        0
        • M mehrdadov

          Hi, I have a function like: if(@a>1) begin select * from t1 join t2 on t1.id=t2.id else select * from t1 end this is simple but when I have many situation and more tables to join it grows a lot. I'm looking for a solution that let me depend on the situations make my select query with or without joins. All the bests,

          Agh

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          mehrdadov wrote:

          this is simple but when I have many situation and more tables to join it grows a lot.

          Why are you doing this? As noted in the other reply one can create a query dynamically. A client app could do this as well. But you really shouldn't have "many" of these. It should normally be limited to a very few cases.

          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