Author |
Topic |
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-06-05 : 03:34:39
|
I need to write an sp as belowExisting Data Emp_Id Emp_Name Emp_Age1001 Ravi 251002 Gopal 271003 Kumar 301004 Suresh 24New Data ResultsetColumn_Name Column_Value Is_NewEmp_Id 1001 1Emp_Name Ravi 0Emp_Age 25 0Emp_Id 1002 1Emp_Name Gopal 0Emp_Age 27 0Emp_Id 1003 1Emp_Name Kumar 0Emp_Age 30 0Emp_Id 1004 1Emp_Name Suresh 0Emp_Age 24 0Is new column has to be 1 when an new row is taken for insertingTHANKSSHANMUGARAJnshanmugaraj@gmail.com |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 03:38:45
|
[code]SELECT *FROM tableUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u[/code]I didnt understand the explanation for Is_Newwhat does below mean?Is new column has to be 1 when an new row is taken for insertinginserting where?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-05 : 03:43:28
|
SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM tableUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u--Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 03:49:20
|
quote: Originally posted by bandi SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM tableUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u--Chandu
Though it matches the output the explanation given is confusingThats why i refrained from posting it as I want to understand what exactly OP is looking forHopefully OP will come back with an explanation rather than being contended with the above solution which may not be exactly the intended one!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-06-05 : 04:55:05
|
Is new column has to be 1 when an new row is taken for display in the query window.I am not able to get result for the SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM tableUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u--ChanduTHANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-06-05 : 04:55:42
|
Error :Msg 8167, Level 16, State 1, Line 3The type of column "Emp_Name" conflicts with the type of other columns specified in the UNPIVOT list.THANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 04:56:15
|
quote: Originally posted by shanmugaraj Is new column has to be 1 when an new row is taken for display in the query window.I am not able to get result for the SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM tableUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u--ChanduTHANKSSHANMUGARAJnshanmugaraj@gmail.com
Please elaborate on what you mean by you're getting intended result with some sample data and outputOtherwise we cant make out what you're doing wrong------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 04:58:03
|
quote: Originally posted by shanmugaraj Error :Msg 8167, Level 16, State 1, Line 3The type of column "Emp_Name" conflicts with the type of other columns specified in the UNPIVOT list.THANKSSHANMUGARAJnshanmugaraj@gmail.com
ok...that makes sensehere you goSELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM (SELECT CAST([Emp_Id] AS varchar(10)) AS Emp_Id,[Emp_Name],CAST([Emp_Age] AS varchar(3)) AS Emp_AgeFROM table)tUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-05 : 05:03:33
|
Hi Shanmugaraj,the columns in the UNPIVOT should have same data type...@Visakh, age should also have length of 10quote: Originally posted by visakh16
quote: Originally posted by shanmugaraj Error :Msg 8167, Level 16, State 1, Line 3The type of column "Emp_Name" conflicts with the type of other columns specified in the UNPIVOT list.THANKSSHANMUGARAJnshanmugaraj@gmail.com
ok...that makes sensehere you goSELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM (SELECT CAST([Emp_Id] AS varchar(10)) AS Emp_Id,[Emp_Name],CAST([Emp_Age] AS varchar(10)) AS Emp_AgeFROM table)tUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))u ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
--Chandu |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 05:10:01
|
quote: Originally posted by bandi Hi Shanmugaraj,the columns in the UNPIVOT should have same data type...@Visakh, age should also have length of 10--Chandu
Yep trueit was an oversight------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
shanmugaraj
Posting Yak Master
219 Posts |
Posted - 2013-06-05 : 06:03:33
|
SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM (SELECT CAST([Emp_Id] AS varchar(10)) AS Emp_Id,[Emp_Name],CAST([Emp_Age] AS varchar(3)) AS Emp_AgeFROM table)tUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))uThis is working fine ... thanks visakhTHANKSSHANMUGARAJnshanmugaraj@gmail.com |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-06-05 : 06:05:52
|
quote: Originally posted by shanmugaraj SELECT *, CASE WHEN Column_Name = 'Emp_id' THEN 1 ELSE 0 END AS Is_NewFROM (SELECT CAST([Emp_Id] AS varchar(10)) AS Emp_Id,[Emp_Name],CAST([Emp_Age] AS varchar(3)) AS Emp_AgeFROM table)tUNPIVOT (Column_Value FOR Column_Name IN ([Emp_Id],[Emp_Name],[Emp_Age]))uThis is working fine ... thanks visakhTHANKSSHANMUGARAJnshanmugaraj@gmail.com
Welcomeyou still didnt answer the clarification on Is_New column logic------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-06-05 : 06:07:24
|
How come the above will work Shanmugaraj...See my earlier post (Posted - 06/05/2013 : 05:03:33)Emp_Age column length should also be same length of other two columns--Chandu |
 |
|
|