我在SQL合并上有一个OUTPUT子句,我已将其识别为导致此错误的原因:
列名或提供的值数与表定义不匹配
我正在输出一个包含单个字段的临时表,而且我为什么会收到此错误而感到沮丧。
CREATE TABLE #PermissionChanges(
ChangeType NVARCHAR(10)
)
MERGE INTO [dbo].[PermissionClient] AS Target
USING (VALUES (@permissionId, @clientId, 1))
AS Source ([PermissionId], [ClientId], [Enabled])
ON (Target.[ClientId] = @clientId AND Target.[PermissionId] = Source.[PermissionId])
WHEN MATCHED THEN
UPDATE SET [Enabled] = Source.[Enabled]
WHEN NOT MATCHED THEN
INSERT ([PermissionId], [ClientId], [Enabled])
VALUES (@permissionId, @clientId , Source.[Enabled])
OUTPUT
$ACTION ChangeType
INTO #PermissionChanges;
如果我将OUTPUT更改为:
OUTPUT $ACTION;
脚本成功执行,我得到:
$ACTION
-------
UPDATE
答案 0 :(得分:0)
检查现有的表结构,如下代码:
USE tempdb
GO
-- Clean up
IF OBJECT_ID('tempdb.dbo.#PermissionChanges','u') is not null
BEGIN
DROP TABLE #results
END
CREATE TABLE #PermissionChanges(
ChangeType NVARCHAR(10)
)
GO
-- Clean up
IF OBJECT_ID('tempdb.[dbo].[PermissionClient]','u') is not null
BEGIN
DROP TABLE #results
END
CREATE TABLE [dbo].[PermissionClient]
([PermissionId] int
, [ClientId] int
, [Enabled] int
)
GO
DECLARE @clientId int
SET @clientId=1
DECLARE @permissionId int
SET @permissionId=1
MERGE INTO [dbo].[PermissionClient] AS Target
USING (VALUES (@permissionId, @clientId, 1))
AS Source ([PermissionId], [ClientId], [Enabled])
ON (Target.[ClientId] = @clientId AND Target.[PermissionId] = Source.[PermissionId])
WHEN MATCHED THEN
UPDATE SET [Enabled] = Source.[Enabled]
WHEN NOT MATCHED THEN
INSERT ([PermissionId], [ClientId], [Enabled])
VALUES (@permissionId, @clientId , Source.[Enabled])
OUTPUT
$ACTION ChangeType
INTO #PermissionChanges;
SELECT * FROM #PermissionChanges
返回
ChangeType
INSERT
答案 1 :(得分:0)
尝试从OUTPUT子句中删除ChangeType:
MERGE INTO [dbo].[PermissionClient] AS Target
USING (VALUES (@permissionId, @clientId, 1))
AS Source ([PermissionId], [ClientId], [Enabled])
ON (Target.[ClientId] = @clientId AND Target.[PermissionId] = Source.[PermissionId])
WHEN MATCHED THEN
UPDATE SET [Enabled] = Source.[Enabled]
WHEN NOT MATCHED THEN
INSERT ([PermissionId], [ClientId], [Enabled])
VALUES (@permissionId, @clientId , Source.[Enabled])
OUTPUT
$ACTION
INTO #PermissionChanges;
答案 2 :(得分:0)
我遇到了同样的问题,发现指定了我想-- MySQL v8
show create table foo;
>> CREATE TABLE `foo` (
`id` int NOT NULL AUTO_INCREMENT,
`field_1` varchar(255) NOT NULL,
`field_2` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `foo_field_1_field_2` (`field_1`,`field_2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
show create table foo2;
>> CREATE TABLE `foo2` (
`id` int NOT NULL AUTO_INCREMENT,
`field_1` varchar(255) NOT NULL,
`field_2` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `field_1` (`field_1`,`field_2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
OUTPUT
进入的列名解决了该问题,我在 some 中也看到了这一点。 MERGE documentation。
$Action
但是,我在没有显式列名的情况下再次尝试了,令我惊讶的是,查询成功了。
我的问题是,有一次我确实使用临时表运行查询,该临时表的列未在OUTPUT $ACTION ChangeType INTO #PermissionChanges(ChangeType)
语句中表示,并且该错误一直存在,直到我明确删除临时表并仅用OUTPUT
的一列。当允许查询无错误执行时,基本上就是脚本末尾的情况。