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. Query using multiple columns in IN Clause

Query using multiple columns in IN Clause

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

    Hi Everyone, I am having one table which contains 5 columns. These five columns together contitute the primary key. I am having one more table which contains data for these primary key. I need to write a query which will bring the data from the second table for those primary keys which are available in first table. :doh: :doh: :doh: :doh:

    CodeManiac xxxxxxxxxx xxxxxxxxxx

    S B 2 Replies Last reply
    0
    • C CodalcoholicNBrainy

      Hi Everyone, I am having one table which contains 5 columns. These five columns together contitute the primary key. I am having one more table which contains data for these primary key. I need to write a query which will bring the data from the second table for those primary keys which are available in first table. :doh: :doh: :doh: :doh:

      CodeManiac xxxxxxxxxx xxxxxxxxxx

      S Offline
      S Offline
      Syed Mehroz Alam
      wrote on last edited by
      #2

      If I understand you correctly, you just need a simple inner join.

      Select ...
      From YourPrimaryTable
      InnerJoin YourDetailTable
      on YourPrimaryTable.Column1 = YourDetailTable.Column1
      and YourPrimaryTable.Column2 = YourDetailTable.Column2
      ...

      Hope that helps. Syed Mehroz Alam

      My Articles

      C 1 Reply Last reply
      0
      • S Syed Mehroz Alam

        If I understand you correctly, you just need a simple inner join.

        Select ...
        From YourPrimaryTable
        InnerJoin YourDetailTable
        on YourPrimaryTable.Column1 = YourDetailTable.Column1
        and YourPrimaryTable.Column2 = YourDetailTable.Column2
        ...

        Hope that helps. Syed Mehroz Alam

        My Articles

        C Offline
        C Offline
        CodalcoholicNBrainy
        wrote on last edited by
        #3

        Thanks for your reply. :-D :-D Can we do the same thing without taking join. I mean can we use IN clause for multiple columns?

        CodeManiac xxxxxxxxxx xxxxxxxxxx

        S 1 Reply Last reply
        0
        • C CodalcoholicNBrainy

          Thanks for your reply. :-D :-D Can we do the same thing without taking join. I mean can we use IN clause for multiple columns?

          CodeManiac xxxxxxxxxx xxxxxxxxxx

          S Offline
          S Offline
          Syed Mehroz Alam
          wrote on last edited by
          #4

          Confusing...I wonder what do you need. Can you explain using a simple example.

          My Articles

          C 1 Reply Last reply
          0
          • C CodalcoholicNBrainy

            Hi Everyone, I am having one table which contains 5 columns. These five columns together contitute the primary key. I am having one more table which contains data for these primary key. I need to write a query which will bring the data from the second table for those primary keys which are available in first table. :doh: :doh: :doh: :doh:

            CodeManiac xxxxxxxxxx xxxxxxxxxx

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

            select FirstTable.*,SecondTable.* from FirstTable,SecondTable where SecondTable.FirstTableColumn in (FirstTable.FirstColumn)


            I Love T-SQL "Don't torture yourself,let the life to do it for you."

            1 Reply Last reply
            0
            • S Syed Mehroz Alam

              Confusing...I wonder what do you need. Can you explain using a simple example.

              My Articles

              C Offline
              C Offline
              CodalcoholicNBrainy
              wrote on last edited by
              #6

              Sorry to confuse you. Let me explain you complete scenario. I am having two databases DB1 and DB2. DB1 has single table T1 and 15 columns namely C1, C2, C3 etc. DB2 has two table T1 and T2. T1 table is identical in both the databases. So T1 table has same 15 columns and same data in both the databases. T2 has 5 columns C1, C2, C3, C4,C5 which together constitute primary key. In T1 table of DB1 database, some of the records are modified. T2 table of database DB2 has the primary keys of all the modified records. I need to update table T1 of database DB2 for the modified records. I need a way to accomplish this. Hopefully I am clear now.

              CodeManiac xxxxxxxxxx xxxxxxxxxx

              S B P 3 Replies Last reply
              0
              • C CodalcoholicNBrainy

                Sorry to confuse you. Let me explain you complete scenario. I am having two databases DB1 and DB2. DB1 has single table T1 and 15 columns namely C1, C2, C3 etc. DB2 has two table T1 and T2. T1 table is identical in both the databases. So T1 table has same 15 columns and same data in both the databases. T2 has 5 columns C1, C2, C3, C4,C5 which together constitute primary key. In T1 table of DB1 database, some of the records are modified. T2 table of database DB2 has the primary keys of all the modified records. I need to update table T1 of database DB2 for the modified records. I need a way to accomplish this. Hopefully I am clear now.

                CodeManiac xxxxxxxxxx xxxxxxxxxx

                S Offline
                S Offline
                Syed Mehroz Alam
                wrote on last edited by
                #7

                I got it now. This seems a difficult issue. I suggest you to create some trigger on db1.T1 that updates the modified rows in db2. If you still want to use some batch update, the most inappropriate way will be to use a cursor loop. But wait, lets hope someone else in the forum finds a more efficient way. Good luck. Syed Mehroz Alam

                My Articles

                1 Reply Last reply
                0
                • C CodalcoholicNBrainy

                  Sorry to confuse you. Let me explain you complete scenario. I am having two databases DB1 and DB2. DB1 has single table T1 and 15 columns namely C1, C2, C3 etc. DB2 has two table T1 and T2. T1 table is identical in both the databases. So T1 table has same 15 columns and same data in both the databases. T2 has 5 columns C1, C2, C3, C4,C5 which together constitute primary key. In T1 table of DB1 database, some of the records are modified. T2 table of database DB2 has the primary keys of all the modified records. I need to update table T1 of database DB2 for the modified records. I need a way to accomplish this. Hopefully I am clear now.

                  CodeManiac xxxxxxxxxx xxxxxxxxxx

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

                  You have to create any trigger or you can make changes on T1 of DB2 something like query down below which you execute that both querys at same time: Update DB1.dbo.T1 set DB1.dbo.T1.column='value' where DB1.dbo.T1.colname ='conditionvaue' Update DB2.dbo.T1 set DB2.dbo.T1.column='value' where DB2.dbo.T1.colname ='conditionvaue'


                  I Love T-SQL "Don't torture yourself,let the life to do it for you."

                  1 Reply Last reply
                  0
                  • C CodalcoholicNBrainy

                    Sorry to confuse you. Let me explain you complete scenario. I am having two databases DB1 and DB2. DB1 has single table T1 and 15 columns namely C1, C2, C3 etc. DB2 has two table T1 and T2. T1 table is identical in both the databases. So T1 table has same 15 columns and same data in both the databases. T2 has 5 columns C1, C2, C3, C4,C5 which together constitute primary key. In T1 table of DB1 database, some of the records are modified. T2 table of database DB2 has the primary keys of all the modified records. I need to update table T1 of database DB2 for the modified records. I need a way to accomplish this. Hopefully I am clear now.

                    CodeManiac xxxxxxxxxx xxxxxxxxxx

                    P Online
                    P Online
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    Perhaps an inner join in the update?

                    UPDATE T1
                    SET ...
                    FROM T1
                    INNER JOIN T2
                    ON T1.blah=T2.blah
                    AND ...

                    C 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Perhaps an inner join in the update?

                      UPDATE T1
                      SET ...
                      FROM T1
                      INNER JOIN T2
                      ON T1.blah=T2.blah
                      AND ...

                      C Offline
                      C Offline
                      CodalcoholicNBrainy
                      wrote on last edited by
                      #10

                      Just one more thing that this process will happen in Batch.

                      CodeManiac xxxxxxxxxx xxxxxxxxxx

                      P 1 Reply Last reply
                      0
                      • C CodalcoholicNBrainy

                        Just one more thing that this process will happen in Batch.

                        CodeManiac xxxxxxxxxx xxxxxxxxxx

                        P Online
                        P Online
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        What does that mean?

                        C 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          What does that mean?

                          C Offline
                          C Offline
                          CodalcoholicNBrainy
                          wrote on last edited by
                          #12

                          I mean the proces will happen every night for the data that changed during day time.

                          CodeManiac xxxxxxxxxx xxxxxxxxxx

                          P 1 Reply Last reply
                          0
                          • C CodalcoholicNBrainy

                            I mean the proces will happen every night for the data that changed during day time.

                            CodeManiac xxxxxxxxxx xxxxxxxxxx

                            P Online
                            P Online
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            Shouldn't be a problem then. I always try to have the database engine do most of the work.

                            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