gpt4 book ai didi

sql - 列与取消透视表列表中其他列的类型冲突

转载 作者:行者123 更新时间:2023-12-03 09:17:46 25 4
gpt4 key购买 nike

我将sys.[views]转换为键值对,以便与另一台服务器上的值进行比较以进行一致性测试。我遇到了一个问题,返回错误。

Msg 8167, Level 16, State 1, Line 51

The type of column "type" conflicts with the type of other columns specified in the UNPIVOT list.


查询:
SELECT
sourceUnpivoted.idServer,
sourceUnpivoted.sourceServerName,
sourceUnpivoted.name,
sourceUnpivoted.columnName,
sourceUnpivoted.columnValue
FROM (
SELECT
CAST('1' AS VARCHAR(255)) AS idServer,
CAST('thisOne' AS VARCHAR(255)) AS sourceServerName,
CAST('theDatabase' AS VARCHAR(255)) AS sourceDatabaseName,
CAST(name AS VARCHAR(255)) AS name,
CAST(object_id AS VARCHAR(255)) AS object_id,
CAST(principal_id AS VARCHAR(255)) AS principal_id,
CAST(schema_id AS VARCHAR(255)) AS schema_id,
CAST(parent_object_id AS VARCHAR(255)) AS parent_object_id,
CAST(type AS VARCHAR(255)) AS type,
CAST(type_desc AS VARCHAR(255)) AS type_desc,
CAST(create_date AS VARCHAR(255)) AS create_date,
CAST(lock_escalation_desc AS VARCHAR(255)) AS lock_escalation_desc
...
FROM noc_test.dbo.stage_sysTables
) AS databaseTables
UNPIVOT (
columnValue FOR columnName IN (
object_id,
principal_id,
schema_id,
parent_object_id,
type,
type_desc,
create_date,
lock_escalation_desc
)
) AS sourceUnpivoted
为什么这不像 [type][type_desc][lock_escalation_desc] ???
我也尝试过 CONVERT(VARCHAR(255),type) AS type

最佳答案

这实际上是整理问题。我可以通过更改以下行来解决它:

CAST([type] collate database_default AS VARCHAR(255)) AS [type], 
CAST(type_desc collate database_default AS VARCHAR(255)) AS type_desc,
CAST(create_date AS VARCHAR(255)) AS create_date,
CAST(lock_escalation_desc collate database_default AS VARCHAR(255)) AS lock_escalation_desc

特定的问题是 name整理为 Latin1_General_CI_AS,而您提到的其他3列则整理为 Latin1_General_CI_AS_KS_WS(至少,在我的机器上,我不确定使用不同默认排序规则的服务器/数据库上的情况) 。

关于sql - 列与取消透视表列表中其他列的类型冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com