When applying the UNPIVOT
function to data that is not normalized, SQL Server requires that the datatype and length be the same. I understand why the datatype must be the same but why does UNPIVOT require the length to be the same?
Let's say that I have the following sample data that I need to unpivot:
CREATE TABLE People
(
PersonId int,
Firstname varchar(50),
Lastname varchar(25)
)
INSERT INTO People VALUES (1, 'Jim', 'Smith');
INSERT INTO People VALUES (2, 'Jane', 'Jones');
INSERT INTO People VALUES (3, 'Bob', 'Unicorn');
If I attempt to UNPIVOT the Firstname
and Lastname
columns similar to:
select PersonId, ColumnName, Value
from People
unpivot
(
Value
FOR ColumnName in (FirstName, LastName)
) unpiv;
SQL Server generates the error:
Msg 8167, Level 16, State 1, Line 6
The type of column "Lastname" conflicts with the type of other columns specified in the UNPIVOT list.
In order to resolve the error, we must use a subquery to first cast the Lastname
column to have the same length as Firstname
:
select PersonId, ColumnName, Value
from
(
select personid,
firstname,
cast(lastname as varchar(50)) lastname
from People
) d
unpivot
(
Value FOR
ColumnName in (FirstName, LastName)
) unpiv;
Prior to UNPIVOT being introduced in SQL Server 2005, I would use a SELECT
with UNION ALL
to unpivot the firstname
/lastname
columns and the query would run without the need to convert the columns to the same length:
select personid, 'firstname' ColumnName, firstname value
from People
union all
select personid, 'LastName', LastName
from People;
See SQL Fiddle with Demo.
We are also able to successfully unpivot the data using CROSS APPLY
without having the same length on the datatype:
select PersonId, columnname, value
from People
cross apply
(
select 'firstname', firstname union all
select 'lastname', lastname
) c (columnname, value);
See SQL Fiddle with Demo.
I have read through MSDN but I didn't find anything explaining the reasoning for forcing the length on the datatype to be the same.
What is the logic behind requiring the same length when using UNPIVOT?