Recursive CTE Error: Types do not match
Recursive CTE Error: Types do not match
我有这个 CTE
WITH items AS (
SELECT 1 AS lvl,
i.[No_] [Parent Item No_],
i.[No_],
i.[Description],
CAST(N'' AS NVARCHAR(20)) /* COLLATE Latin1_General_100_CS_AS */
FROM Item i
LEFT JOIN KitComponent bc ON bc.[Parent Item No_] = i.[No_]
LEFT JOIN Item ci ON ci.[No_] = bc.[No_]
GROUP BY i.[No_], i.[Description], i.[Unit Price]
UNION ALL
SELECT i.lvl + 1,
i.[No_],
i2.[No_],
i2.[Description],
CAST(bc.[Variant Code] AS NVARCHAR(20))
FROM KitComponent bc
JOIN items i ON i.[No_] = bc.[Parent Item No_]
JOIN Item i2 ON i2.[No_] = bc.[No_]
)
SELECT * FROM items WHERE [Parent Item No_] = '4000540001'
它在一台服务器上工作,没有指定排序规则的注释部分。然后我将整个东西复制到另一个 window,其中同一数据库的复制副本位于另一台服务器上并出现此错误。
Types don't match between the anchor and the recursive part in column "Variant Code" of recursive query "items".
我最初也没有那些 CAST
功能,它在服务器 #1 上工作得很好。最后,我将 collate 命令放在 CTE 的顶部,然后它在两台机器上都可以使用。
我通过SELECT @@VERSION
查看版本
Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64)
Oct 20 2015 15:36:27
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(Hypervisor)
Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64)
Oct 20 2015 15:36:27
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(Hypervisor)
而且它们是相同的,所以我认为这一定是一些不同的选项设置。
有谁知道我在哪里查看那个选项?
来自 WITH 文档:
The data type of a column in the recursive member must be the same as the data type of the corresponding column in the anchor member.
因此它应该具有相同的数据类型、长度、排序规则......
在你的例子中:
WITH items AS (
SELECT 1 AS lvl,
i.[No_] [Parent Item No_],
i.[No_],
i.[Description],
CAST(N'' AS NVARCHAR(20)) -- default DB collation
FROM Item i
LEFT JOIN KitComponent bc ON bc.[Parent Item No_] = i.[No_]
LEFT JOIN Item ci ON ci.[No_] = bc.[No_]
GROUP BY i.[No_], i.[Description], i.[Unit Price]
UNION ALL
SELECT i.lvl + 1,
i.[No_],
i2.[No_],
i2.[Description],
CAST(bc.[Variant Code] AS NVARCHAR(20)) -- column collation
FROM KitComponent bc
JOIN items i ON i.[No_] = bc.[Parent Item No_]
JOIN Item i2 ON i2.[No_] = bc.[No_]
)
SELECT * FROM items WHERE [Parent Item No_] = '4000540001';
检查
SELECT DATABASEPROPERTYEX(DB_NAME(), 'Collation')
和:
SELECT COLLATION_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'KitComponent'
AND column_name = 'Variant Code'
我有这个 CTE
WITH items AS (
SELECT 1 AS lvl,
i.[No_] [Parent Item No_],
i.[No_],
i.[Description],
CAST(N'' AS NVARCHAR(20)) /* COLLATE Latin1_General_100_CS_AS */
FROM Item i
LEFT JOIN KitComponent bc ON bc.[Parent Item No_] = i.[No_]
LEFT JOIN Item ci ON ci.[No_] = bc.[No_]
GROUP BY i.[No_], i.[Description], i.[Unit Price]
UNION ALL
SELECT i.lvl + 1,
i.[No_],
i2.[No_],
i2.[Description],
CAST(bc.[Variant Code] AS NVARCHAR(20))
FROM KitComponent bc
JOIN items i ON i.[No_] = bc.[Parent Item No_]
JOIN Item i2 ON i2.[No_] = bc.[No_]
)
SELECT * FROM items WHERE [Parent Item No_] = '4000540001'
它在一台服务器上工作,没有指定排序规则的注释部分。然后我将整个东西复制到另一个 window,其中同一数据库的复制副本位于另一台服务器上并出现此错误。
Types don't match between the anchor and the recursive part in column "Variant Code" of recursive query "items".
我最初也没有那些 CAST
功能,它在服务器 #1 上工作得很好。最后,我将 collate 命令放在 CTE 的顶部,然后它在两台机器上都可以使用。
我通过SELECT @@VERSION
Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64)
Oct 20 2015 15:36:27
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(Hypervisor)
Microsoft SQL Server 2012 (SP3) (KB3072779) - 11.0.6020.0 (X64)
Oct 20 2015 15:36:27
Copyright (c) Microsoft Corporation
Standard Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(Hypervisor)
而且它们是相同的,所以我认为这一定是一些不同的选项设置。
有谁知道我在哪里查看那个选项?
来自 WITH 文档:
The data type of a column in the recursive member must be the same as the data type of the corresponding column in the anchor member.
因此它应该具有相同的数据类型、长度、排序规则......
在你的例子中:
WITH items AS (
SELECT 1 AS lvl,
i.[No_] [Parent Item No_],
i.[No_],
i.[Description],
CAST(N'' AS NVARCHAR(20)) -- default DB collation
FROM Item i
LEFT JOIN KitComponent bc ON bc.[Parent Item No_] = i.[No_]
LEFT JOIN Item ci ON ci.[No_] = bc.[No_]
GROUP BY i.[No_], i.[Description], i.[Unit Price]
UNION ALL
SELECT i.lvl + 1,
i.[No_],
i2.[No_],
i2.[Description],
CAST(bc.[Variant Code] AS NVARCHAR(20)) -- column collation
FROM KitComponent bc
JOIN items i ON i.[No_] = bc.[Parent Item No_]
JOIN Item i2 ON i2.[No_] = bc.[No_]
)
SELECT * FROM items WHERE [Parent Item No_] = '4000540001';
检查
SELECT DATABASEPROPERTYEX(DB_NAME(), 'Collation')
和:
SELECT COLLATION_NAME,*
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'KitComponent'
AND column_name = 'Variant Code'