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. Passing an array of integers to a stored procedure

Passing an array of integers to a stored procedure

Scheduled Pinned Locked Moved Database
databasecomdata-structurestoolshelp
6 Posts 3 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.
  • L Offline
    L Offline
    Luis Alonso Ramos
    wrote on last edited by
    #1

    Hello, I currently have a stored procedure that I call multiple times with an ID to return a record. However, in my program, I sometimes need to return several records (for which I have all the IDs) at the same time. Is there a way I could pass an array of all IDs so I get all the necessary records with just one round-trip to the database? [EDIT -- Something like this would be ideal:

    CREATE PROCEDURE Whatever
    (
        @param int_array
    )
    AS
    SELECT * FROM Table WHERE ID IN @param
    

    ] Any help would be appreciated! Thanks! -- LuisR


    Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

    The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005 -- modified at 13:08 Thursday 22nd September, 2005

    O 1 Reply Last reply
    0
    • L Luis Alonso Ramos

      Hello, I currently have a stored procedure that I call multiple times with an ID to return a record. However, in my program, I sometimes need to return several records (for which I have all the IDs) at the same time. Is there a way I could pass an array of all IDs so I get all the necessary records with just one round-trip to the database? [EDIT -- Something like this would be ideal:

      CREATE PROCEDURE Whatever
      (
          @param int_array
      )
      AS
      SELECT * FROM Table WHERE ID IN @param
      

      ] Any help would be appreciated! Thanks! -- LuisR


      Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

      The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005 -- modified at 13:08 Thursday 22nd September, 2005

      O Offline
      O Offline
      Orina DCosta
      wrote on last edited by
      #2

      Use the IN operator and pass the integer ids as a comma separated string. Build the entire SQL statement in your application and execute the statement itself instead of calling a stored procedure. SELECT * FROM Table WHERE ID IN (param1,param2,...) Hope this helps... Regards, Orina DCosta http://orina.org

      L 1 Reply Last reply
      0
      • O Orina DCosta

        Use the IN operator and pass the integer ids as a comma separated string. Build the entire SQL statement in your application and execute the statement itself instead of calling a stored procedure. SELECT * FROM Table WHERE ID IN (param1,param2,...) Hope this helps... Regards, Orina DCosta http://orina.org

        L Offline
        L Offline
        Luis Alonso Ramos
        wrote on last edited by
        #3

        Yes, I have thought of that. But the stored procedure is not just a single SELECT, so it is not as easy. Any other ideas? Thanks anyway, -- LuisR


        Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

        The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

        C 1 Reply Last reply
        0
        • L Luis Alonso Ramos

          Yes, I have thought of that. But the stored procedure is not just a single SELECT, so it is not as easy. Any other ideas? Thanks anyway, -- LuisR


          Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

          The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

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

          I prefer to use OpenXML rather than a comma delimited list. I'm not sure if that helps though - I suspect if you can't use one, you can't use the other ? OpenXML does translate nicely from XML to a table though, so it may be a better idea. Christian Graus - Microsoft MVP - C++

          L 1 Reply Last reply
          0
          • C Christian Graus

            I prefer to use OpenXML rather than a comma delimited list. I'm not sure if that helps though - I suspect if you can't use one, you can't use the other ? OpenXML does translate nicely from XML to a table though, so it may be a better idea. Christian Graus - Microsoft MVP - C++

            L Offline
            L Offline
            Luis Alonso Ramos
            wrote on last edited by
            #5

            Hello Christian! Are you talking about the OPENXML[^] T-SQL Function? If yes, I think that could help. I could build a little XML table with all the IDs and get all of the at once. -- LuisR


            Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

            The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

            C 1 Reply Last reply
            0
            • L Luis Alonso Ramos

              Hello Christian! Are you talking about the OPENXML[^] T-SQL Function? If yes, I think that could help. I could build a little XML table with all the IDs and get all of the at once. -- LuisR


              Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!

              The amount of sleep the average person needs is five more minutes. -- Vikram A Punathambekar, Aug. 11, 2005

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

              Yes, I am. That stuff totally rocks. Christian Graus - Microsoft MVP - C++

              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