取消隐藏列时键入强制转换错误,为什么?

时间:2014-01-22 09:48:53

标签: sql tsql common-table-expression unpivot

我试图在mdsb.sys.database表中取消某些列的UNPIVOT但是在某些列上遇到了一些问题。在UNPIVOTing表之前,我尝试将所有列CAST中的所有列CAST到NVARCHAR(128),但它仍然无效。有人可以解释为什么以及如何解决这个问题?

;
WITH props AS (
SELECT 
    [name]
    ,CAST([collation_name] AS NVARCHAR(128)) AS [collation_name]
    ,CAST([is_auto_close_on] AS NVARCHAR(128)) AS [is_auto_close_on]
    ,CAST([is_auto_shrink_on] AS NVARCHAR(128)) AS [is_auto_shrink_on]
    ,CAST([is_auto_create_stats_on] AS NVARCHAR(128)) AS [is_auto_create_stats_on]
    ,CAST([is_auto_update_stats_async_on] AS NVARCHAR(128)) AS [is_auto_update_stats_async_on]
    ,CAST([is_auto_update_stats_on] AS NVARCHAR(128)) AS [is_auto_update_stats_on]
    ,CAST(CASE WHEN [is_parameterization_forced] = 0 THEN N'SIMPLE' ELSE N'FORCED' END AS NVARCHAR(128)) AS [is_parameterization_forced]
    ,CAST([is_trustworthy_on] AS NVARCHAR(128)) AS [is_trustworthy_on]
    ,CAST([compatibility_level] AS NVARCHAR(128)) AS [compatibility_level]
    ,CAST([page_verify_option_desc] AS NVARCHAR(128)) AS [page_verify_option_desc]
FROM sys.databases s
WHERE [name] = DB_NAME()
)
SELECT [name]
        ,[property]
        ,[value] 
FROM [props]
UNPIVOT
(
    [value]
    FOR [property] IN ([collation_name] 
                        ,[is_auto_close_on]
                        ,[is_auto_shrink_on]
                        ,[is_auto_create_stats_on]
                        ,[is_auto_update_stats_async_on]
                        ,[is_auto_update_stats_on]
                        ,[is_parameterization_forced]
                        ,[is_trustworthy_on]
                        ,[compatibility_level]
                        ,[page_verify_option_desc]
                        )
) AS u

错误:

Msg 8167, Level 16, State 1, Line 34
The type of column "page_verify_option_desc" conflicts with the type of other columns specified in the UNPIVOT list.

结果看起来应该是这样的,但是带有“page_verify_option_desc”行和“recovery_model_desc”。

name    property    value
master  collation_name  SQL_Latin1_General_CP1_CI_AS
master  is_auto_close_on    0
master  is_auto_shrink_on   0
master  is_auto_create_stats_on 1
master  is_auto_update_stats_async_on   0
master  is_auto_update_stats_on 1
master  is_parameterization_forced  SIMPLE
master  is_trustworthy_on   0
master  compatibility_level 110

1 个答案:

答案 0 :(得分:2)

我认为这实际上是一个整理问题

试试这个

  ;
WITH props AS (
SELECT 
    [name]
    ,CAST([collation_name] AS NVARCHAR(128)) AS [collation_name]
    ,CAST([is_auto_close_on] AS NVARCHAR(128)) AS [is_auto_close_on]
    ,CAST([is_auto_shrink_on] AS NVARCHAR(128)) AS [is_auto_shrink_on]
    ,CAST([is_auto_create_stats_on] AS NVARCHAR(128)) AS [is_auto_create_stats_on]
    ,CAST([is_auto_update_stats_async_on] AS NVARCHAR(128)) AS [is_auto_update_stats_async_on]
    ,CAST([is_auto_update_stats_on] AS NVARCHAR(128)) AS [is_auto_update_stats_on]
    ,CAST(CASE WHEN [is_parameterization_forced] = 0 THEN N'SIMPLE' ELSE N'FORCED' END AS NVARCHAR(128)) AS [is_parameterization_forced]
    ,CAST([is_trustworthy_on] AS NVARCHAR(128)) AS [is_trustworthy_on]
    ,CAST([compatibility_level] AS NVARCHAR(128)) AS [compatibility_level]
    ,CAST([page_verify_option_desc] collate database_default AS NVARCHAR(128)) AS [page_verify_option_desc]
FROM sys.databases s
WHERE [name] = DB_NAME()
)
SELECT [name]
        ,[property]
        ,[value] 
FROM [props]
UNPIVOT
(
    [value]
    FOR [property] IN ([collation_name] 
                        ,[is_auto_close_on]
                        ,[is_auto_shrink_on]
                        ,[is_auto_create_stats_on]
                        ,[is_auto_update_stats_async_on]
                        ,[is_auto_update_stats_on]
                        ,[is_parameterization_forced]
                        ,[is_trustworthy_on]
                        ,[compatibility_level]
                        ,[page_verify_option_desc]
                        )
) AS u