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. parameter with the In function

parameter with the In function

Scheduled Pinned Locked Moved Database
databasetutorialquestion
12 Posts 6 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.
  • T Tamimi Code

    hi can we user parameters with the In function ?? example: select * from tbl_Name where id in(par) // par is a parameter pass to a stored procedure i want to do this because i dont know how many ids i will get. thank you

    When you get mad...THINK twice that the only advice Tamimi - Code

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

    Example: select * from tbl_Name where id in(1,2,3,4)


    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

    T 1 Reply Last reply
    0
    • B Blue_Boy

      Example: select * from tbl_Name where id in(1,2,3,4)


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      T Offline
      T Offline
      Tamimi Code
      wrote on last edited by
      #3

      thank you.. i know this :) my question was how to do that with a parameter

      When you get mad...THINK twice that the only advice Tamimi - Code

      B 1 Reply Last reply
      0
      • T Tamimi Code

        thank you.. i know this :) my question was how to do that with a parameter

        When you get mad...THINK twice that the only advice Tamimi - Code

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

        Then you have to generate that numbers from select query.


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

        T 1 Reply Last reply
        0
        • T Tamimi Code

          hi can we user parameters with the In function ?? example: select * from tbl_Name where id in(par) // par is a parameter pass to a stored procedure i want to do this because i dont know how many ids i will get. thank you

          When you get mad...THINK twice that the only advice Tamimi - Code

          J Offline
          J Offline
          John Gathogo
          wrote on last edited by
          #5

          I guess your parameter might be comprising of a delimited string, say, "1,2,4,8" or even "{Guid1},{Guid2},...". If such is your requirement, you can always use some little tweaks. Like Declare a temporary table to store the Ids, like declare @Ids table(Id int) Then, use some string functions to split the string and insert the ids into the table. (Like, CHARINDEX, SUBSTRING, etc) After that, you can then use the temporary table on your WHERE ... IN ... clause Like select * from tbl_Name where id in (select Id from @Ids)

          T D 2 Replies Last reply
          0
          • B Blue_Boy

            Then you have to generate that numbers from select query.


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

            T Offline
            T Offline
            Tamimi Code
            wrote on last edited by
            #6

            mmmmmm suppose you have a check box list that contains 50 items(id) and the user can select as many as he wish from the list.. then how you can pass the selected ids to a stored procedure ?? giving that you don't know how many ids you will pass?? got me??

            When you get mad...THINK twice that the only advice Tamimi - Code

            J 1 Reply Last reply
            0
            • J John Gathogo

              I guess your parameter might be comprising of a delimited string, say, "1,2,4,8" or even "{Guid1},{Guid2},...". If such is your requirement, you can always use some little tweaks. Like Declare a temporary table to store the Ids, like declare @Ids table(Id int) Then, use some string functions to split the string and insert the ids into the table. (Like, CHARINDEX, SUBSTRING, etc) After that, you can then use the temporary table on your WHERE ... IN ... clause Like select * from tbl_Name where id in (select Id from @Ids)

              T Offline
              T Offline
              Tamimi Code
              wrote on last edited by
              #7

              thank you this is nice. i will give it a try. or simply :-D string strIds = "1,2,3,4"; SqlCommand com="select * from tbl_name where id in(" + strIds + ")";

              When you get mad...THINK twice that the only advice Tamimi - Code

              1 Reply Last reply
              0
              • T Tamimi Code

                hi can we user parameters with the In function ?? example: select * from tbl_Name where id in(par) // par is a parameter pass to a stored procedure i want to do this because i dont know how many ids i will get. thank you

                When you get mad...THINK twice that the only advice Tamimi - Code

                G Offline
                G Offline
                Goutam Patra
                wrote on last edited by
                #8

                Use Table valued parameter[^] and try google for more example.

                T 1 Reply Last reply
                0
                • G Goutam Patra

                  Use Table valued parameter[^] and try google for more example.

                  T Offline
                  T Offline
                  Tamimi Code
                  wrote on last edited by
                  #9

                  this is new to me :) thank you... will read about it

                  When you get mad...THINK twice that the only advice Tamimi - Code

                  1 Reply Last reply
                  0
                  • J John Gathogo

                    I guess your parameter might be comprising of a delimited string, say, "1,2,4,8" or even "{Guid1},{Guid2},...". If such is your requirement, you can always use some little tweaks. Like Declare a temporary table to store the Ids, like declare @Ids table(Id int) Then, use some string functions to split the string and insert the ids into the table. (Like, CHARINDEX, SUBSTRING, etc) After that, you can then use the temporary table on your WHERE ... IN ... clause Like select * from tbl_Name where id in (select Id from @Ids)

                    D Offline
                    D Offline
                    David Mujica
                    wrote on last edited by
                    #10

                    Note: I'm not sure about MS-SQL, but in Oracle there is a limit to the number of values you can have in an "IN" clause. Back in Oracle 9, I believe the max was 1000. for example: select * from myTable where ID IN (1,2,3,....,1000,1001) would fail because there was more than 1000 values listed. I'm not sure if this is still the case, but you might want to consider it in your design. Good luck. :thumbsup:

                    J 1 Reply Last reply
                    0
                    • T Tamimi Code

                      mmmmmm suppose you have a check box list that contains 50 items(id) and the user can select as many as he wish from the list.. then how you can pass the selected ids to a stored procedure ?? giving that you don't know how many ids you will pass?? got me??

                      When you get mad...THINK twice that the only advice Tamimi - Code

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

                      Tamimi - Code wrote:

                      then how you can pass the selected ids to a stored procedure ??

                      Presuming that you really did mean stored procedure then... First step, determine based on to create the stored procedure in the language supported by the database. Options that I have used for variable argument lists. - Arrays - Varchar with values as a comma separated values in that list. - Proc with up to X args and of which can be null. - Several procs each one with an increasing number of parameters: first has 5, second has 10, etc. - dynamic SQL, run in a proc (only suitable for situations where input is known to be secure.) Once you do in fact have a proc then you write code which populates the parameters dependent on the type of proc that actually exists. Conversely without a proc, and just using SQL, one creates the SQL from scratch using code (for loops, string concatenation, etc) with the appropriate number of bind variables. Then one populates the bind variables. Then you run it. To my mind the last option is easier than any solution with procs.

                      1 Reply Last reply
                      0
                      • D David Mujica

                        Note: I'm not sure about MS-SQL, but in Oracle there is a limit to the number of values you can have in an "IN" clause. Back in Oracle 9, I believe the max was 1000. for example: select * from myTable where ID IN (1,2,3,....,1000,1001) would fail because there was more than 1000 values listed. I'm not sure if this is still the case, but you might want to consider it in your design. Good luck. :thumbsup:

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

                        David Mujica wrote:

                        Back in Oracle 9, I believe the max was 1000.

                        And at least in 10 as well. http://forums.oracle.com/forums/thread.jspa?threadID=428758[^]

                        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