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. Get Names from Id

Get Names from Id

Scheduled Pinned Locked Moved Database
question
8 Posts 5 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.
  • S Offline
    S Offline
    SatyaKeerthi15
    wrote on last edited by
    #1

    I have two Tables LIKE Emp(EmpNo,EmpName,DeptNo),Dept(No,Name). Emp Table looks like: 1 xxx 1,2 2 yyy 2,3 3 zzz 1 Dept Table: 1 aaaa 2 bbbb 3 cccc My Output will be: 1 xxx aaaa,bbbb 2 yyy bbbb,cccc 3 zzz aaaa How can I do this in Sqlserver2005

    J J 2 Replies Last reply
    0
    • S SatyaKeerthi15

      I have two Tables LIKE Emp(EmpNo,EmpName,DeptNo),Dept(No,Name). Emp Table looks like: 1 xxx 1,2 2 yyy 2,3 3 zzz 1 Dept Table: 1 aaaa 2 bbbb 3 cccc My Output will be: 1 xxx aaaa,bbbb 2 yyy bbbb,cccc 3 zzz aaaa How can I do this in Sqlserver2005

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      Your database model is faulty IMAO. How about:

      Emp
      EmpID Empname
      1 xxx
      2 yyy
      3 ZZZ

      Dept
      DeptID Deptname
      1 aaaa
      2 bbbb
      3 cccc

      Emp_Dept
      EmpID DeptID
      1 1
      1 2
      2 2
      2 3
      3 1

      then you can use

      SELECT EmpName,DeptName
      FROM Emp e
      Join EmpDept ed
      ON e.EmpID = ed.EmpID
      JOIN Dept d
      ON ed.Deptid = d.DeptID

      And if you really need to get the deptnames as commaseparated values you can have a look in this thread[^] how to do that.

      "When did ignorance become a point of view" - Dilbert

      J 1 Reply Last reply
      0
      • J Jorgen Andersson

        Your database model is faulty IMAO. How about:

        Emp
        EmpID Empname
        1 xxx
        2 yyy
        3 ZZZ

        Dept
        DeptID Deptname
        1 aaaa
        2 bbbb
        3 cccc

        Emp_Dept
        EmpID DeptID
        1 1
        1 2
        2 2
        2 3
        3 1

        then you can use

        SELECT EmpName,DeptName
        FROM Emp e
        Join EmpDept ed
        ON e.EmpID = ed.EmpID
        JOIN Dept d
        ON ed.Deptid = d.DeptID

        And if you really need to get the deptnames as commaseparated values you can have a look in this thread[^] how to do that.

        "When did ignorance become a point of view" - Dilbert

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Ignore this response - it was wrong. Monday morning syndrome. Do you work in 2 departments? I admit that some corporate structures work in this way, but for the purpose of the OP's question it was unecessary to add a many-to-many relationship between employees and departments. His original structure of 1 department hads many employees, ie a one-to-many relationship (or to put this another way each employee works in just 1 department) would have worked just fine, with just one join in the query.

        modified on Monday, August 9, 2010 7:55 AM

        D J 2 Replies Last reply
        0
        • S SatyaKeerthi15

          I have two Tables LIKE Emp(EmpNo,EmpName,DeptNo),Dept(No,Name). Emp Table looks like: 1 xxx 1,2 2 yyy 2,3 3 zzz 1 Dept Table: 1 aaaa 2 bbbb 3 cccc My Output will be: 1 xxx aaaa,bbbb 2 yyy bbbb,cccc 3 zzz aaaa How can I do this in Sqlserver2005

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

          Ignore this answer. I mis-interpreted your source data. The above answer, although correct confuses your question somewhat. All you need is to join employee table to department table using the foreign/primary key between employee and department. SELECT EmpNo, EmpName, DeptName FROM Emp INNER JOIN Dept ON Emp.DeptNo=Dept.No

          modified on Monday, August 9, 2010 7:54 AM

          T 1 Reply Last reply
          0
          • J J4amieC

            Ignore this response - it was wrong. Monday morning syndrome. Do you work in 2 departments? I admit that some corporate structures work in this way, but for the purpose of the OP's question it was unecessary to add a many-to-many relationship between employees and departments. His original structure of 1 department hads many employees, ie a one-to-many relationship (or to put this another way each employee works in just 1 department) would have worked just fine, with just one join in the query.

            modified on Monday, August 9, 2010 7:55 AM

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            No, if you look at the example data in the OP, it is a many-to-many relationship. Employee xxx works in two departments, aaaa and bbbb. Department aaaa has two employees, xxx and zzz.

            J 1 Reply Last reply
            0
            • D David Skelly

              No, if you look at the example data in the OP, it is a many-to-many relationship. Employee xxx works in two departments, aaaa and bbbb. Department aaaa has two employees, xxx and zzz.

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              My bad, I interpreted as the nameof the first employee to be "xxx 1" - I now see the deptNo field is a comma-separated list X|

              1 Reply Last reply
              0
              • J J4amieC

                Ignore this response - it was wrong. Monday morning syndrome. Do you work in 2 departments? I admit that some corporate structures work in this way, but for the purpose of the OP's question it was unecessary to add a many-to-many relationship between employees and departments. His original structure of 1 department hads many employees, ie a one-to-many relationship (or to put this another way each employee works in just 1 department) would have worked just fine, with just one join in the query.

                modified on Monday, August 9, 2010 7:55 AM

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                J4amieC wrote:

                Monday morning syndrome.

                I know everything about that, except it usually happens to me any day of the week

                "When did ignorance become a point of view" - Dilbert

                1 Reply Last reply
                0
                • J J4amieC

                  Ignore this answer. I mis-interpreted your source data. The above answer, although correct confuses your question somewhat. All you need is to join employee table to department table using the foreign/primary key between employee and department. SELECT EmpNo, EmpName, DeptName FROM Emp INNER JOIN Dept ON Emp.DeptNo=Dept.No

                  modified on Monday, August 9, 2010 7:54 AM

                  T Offline
                  T Offline
                  tarun_j200
                  wrote on last edited by
                  #8

                  TRY THIS: SELECT Emp.EmployeeID, Emp.FirstName, STUFF((SELECT ',' + DeptName FROM Dept WHERE Dept.DeptID = Emp.DeptID FOR XML PATH('')),1,1,'') AS DeptNames FROM Employees Emp

                  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