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. Concatenating Sql queries

Concatenating Sql queries

Scheduled Pinned Locked Moved Database
databasetutorialquestion
6 Posts 2 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.
  • F Offline
    F Offline
    falles01
    wrote on last edited by
    #1

    Hi, I have a long SQL querie and my TL has asked that I concatenate them so I am not repeating the same querie over and over again. The thing is, each querie is inside a new for loop so if I concatenate, it is not picking up the previous query. For example this is what I had before for each for loop except that the end where clause is different for each. for bla bla { ring sql = "select e.Firstname,e.Lastname,r.Role,r.Description,d.DivisionName,a.Applications,m.ManagerFirstname,m.ManagerLastname,t.ProgLanguagesDatabase" + " from employees e,Division d,Manager m,TechnicalSkills t,Role r,Applications a where " + " e.ManagerID = m.ManagerID My TL wants me to write for { sql1 = blabla } for bla { sql2 = sql1 + (Sql query) } Obviously it is telling me that sql2 does not exist. At the moment I have something like this after the queries. SqlDataAdapter myCommand = new SqlDataAdapter(sql, myConnection); and obviously I can't overload this each time with sql2, sql3 etc)??? Sianny aka Sharny

    C 1 Reply Last reply
    0
    • F falles01

      Hi, I have a long SQL querie and my TL has asked that I concatenate them so I am not repeating the same querie over and over again. The thing is, each querie is inside a new for loop so if I concatenate, it is not picking up the previous query. For example this is what I had before for each for loop except that the end where clause is different for each. for bla bla { ring sql = "select e.Firstname,e.Lastname,r.Role,r.Description,d.DivisionName,a.Applications,m.ManagerFirstname,m.ManagerLastname,t.ProgLanguagesDatabase" + " from employees e,Division d,Manager m,TechnicalSkills t,Role r,Applications a where " + " e.ManagerID = m.ManagerID My TL wants me to write for { sql1 = blabla } for bla { sql2 = sql1 + (Sql query) } Obviously it is telling me that sql2 does not exist. At the moment I have something like this after the queries. SqlDataAdapter myCommand = new SqlDataAdapter(sql, myConnection); and obviously I can't overload this each time with sql2, sql3 etc)??? Sianny aka Sharny

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Realistically, you should not be building SQL this way anyhow, you should be calling stored procs, from a data layer. I'm not sure I follow, you will have multiple queries in one call ? then your string needs to be defined outside the first loop, so you can add to it.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      F 2 Replies Last reply
      0
      • C Christian Graus

        Realistically, you should not be building SQL this way anyhow, you should be calling stored procs, from a data layer. I'm not sure I follow, you will have multiple queries in one call ? then your string needs to be defined outside the first loop, so you can add to it.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        F Offline
        F Offline
        falles01
        wrote on last edited by
        #3

        if I declare them outside the loops string sql1; string sql2; string sql3; etc then inside each for loop I have a sql query like for () { sql2 = sql1 + "where bla bla"; } for() { sql3 = sql1 +"where bla bla"; } Obviously sql1 and 2 and 3 etc are never assigned as they are defined outside the loops and it never picks up their value? This is what the error is telling me anyway. Thanks

        1 Reply Last reply
        0
        • C Christian Graus

          Realistically, you should not be building SQL this way anyhow, you should be calling stored procs, from a data layer. I'm not sure I follow, you will have multiple queries in one call ? then your string needs to be defined outside the first loop, so you can add to it.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          F Offline
          F Offline
          falles01
          wrote on last edited by
          #4

          ah...i think I worked it out now. I'm not sure if that is what you were trying to tell me anyway, but I have declared the common query outside all the loops like this string sql = "Select e.Firstname,e.Lastname,r.Role,r.Description,d.DivisionName,a.Applications,m.ManagerFirstname,m.ManagerLastname,t.ProgLanguagesDatabase" + " from employees e,Role r,Division d, Manager m, TechnicalSkills t,Applications"; then later i have used sql2 = sql + "where etc"; Is this the correct way? thanks Sharny

          C 1 Reply Last reply
          0
          • F falles01

            ah...i think I worked it out now. I'm not sure if that is what you were trying to tell me anyway, but I have declared the common query outside all the loops like this string sql = "Select e.Firstname,e.Lastname,r.Role,r.Description,d.DivisionName,a.Applications,m.ManagerFirstname,m.ManagerLastname,t.ProgLanguagesDatabase" + " from employees e,Role r,Division d, Manager m, TechnicalSkills t,Applications"; then later i have used sql2 = sql + "where etc"; Is this the correct way? thanks Sharny

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            I expect each loop would end up adding a query to the main sql string, but, it's hard to say, I'm not entirely sure why you're putting strings in from more than one loop, or what the end product is to be. But yes, having one string to hold all the SQl was my suggestion.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            F 1 Reply Last reply
            0
            • C Christian Graus

              I expect each loop would end up adding a query to the main sql string, but, it's hard to say, I'm not entirely sure why you're putting strings in from more than one loop, or what the end product is to be. But yes, having one string to hold all the SQl was my suggestion.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              F Offline
              F Offline
              falles01
              wrote on last edited by
              #6

              Thankyou. I need to have nested loops because I've got multiple user search options on a windows form. I have it working baced on what I said. I just had to do a bit more trial and error and I would've worked it out without asking. Thank you :)

              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