Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Bulk Insert

Author  Topic 

touhidmustakhim
Starting Member

1 Post

Posted - 2010-06-15 : 13:46:25
I have a text file with content as follows

dept_1 = |ajay|mukesh|suresh|
dept_2 = |suresh|subash|chandrashaker|babu paul|

and so on
now i need an out put of
dept_name employee_name ------->column_name
dept_1 ajay
dept_1 mukesh
dept_1 suresh
dept_2 Suresh
dept_2 subash
dept_2 chandrashaker
dept_2 babu paul

please provide a solution for this problem

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-15 : 14:02:14
One way is to insert into a temp table and UNPIVOT like..
declare @t table (dept_name varchar(10), emp1 varchar(10), emp2 varchar(10), emp3 varchar(10), emp4 varchar(10))
insert @t
select 'dept_1','ajay','mukesh','suresh',null
union all select 'dept_2','suresh','subash','chandra','babu'


SELECT dept_name, Employee_Name
FROM
(SELECT * FROM @t) p
UNPIVOT
(Employee_Name FOR Employee IN
(emp1, emp2, emp3, emp4)
)AS unpvt
Go to Top of Page
   

- Advertisement -