gpt4 book ai didi

sql - T-SQL 触发器触发 "Column name or number of supplied values does not match table definition"错误

转载 作者:行者123 更新时间:2023-12-04 14:27:21 25 4
gpt4 key购买 nike

这是我无法解决的问题,我查看了 无处不在 .也许这里有人会知道!

我有一个名为 dandb_raw 的表,特别包含三列:dunsId (PK)、name 和 searchName。我还有一个作用于这个表的触发器:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dandb_raw_searchNames]
ON [dandb_raw]
FOR INSERT, UPDATE
AS

SET NOCOUNT ON

select dunsId, name into #magic from inserted

UPDATE dandb
SET dandb.searchName = company_generateSearchName(dandb.name)
FROM (select dunsId, name from #magic) i
INNER JOIN dandb_raw dandb
on i.dunsId = dandb.dunsId


--Add new search matches
SELECT c.companyId, dandb.dunsId
INTO #newMatches
FROM dandb_raw dandb
INNER JOIN (select dunsId, name from #magic) a
on a.dunsId = dandb.dunsId
INNER JOIN companies c
ON dandb.searchName = c.searchBrand
--avoid url matches that are potentially wrong
AND (lower(dandb.url) = lower(c.url)
OR dandb.url = ''
OR c.url = ''
OR c.url is null)


INSERT INTO #newMatches (companyId, dunsId)
SELECT c.companyId, max(dandb.dunsId) dunsId
FROM dandb_raw dandb
INNER JOIN
(
select
case when charindex('/',url) <> 0 then left(url, charindex('/',url)-1)
else url
end urlMatch, * from companies
) c
ON dandb.url = c.urlMatch
where subsidiaryOf = 1 and isReported = 1 and dandb.url <> ''
and c.companyId not in (select companyId from #newMatches)
group by companyId
having count(dandb.dunsId) = 1

UPDATE cd
SET cd.dunsId = nm.dunsId
FROM companies_dandb cd
INNER JOIN #newMatches nm
ON cd.companyId = nm.companyId
GO

触发器导致插入失败:
insert into  [dandb_raw](dunsId, name)
select 3442355, 'harper'
union all
select 34425355, 'har 466per'
update [dandb_raw] set name ='grap6767e'

出现此错误:
Msg 213, Level 16, State 1, Procedure companies_contactInfo_updateTerritories, Line 20
Insert Error: Column name or number of supplied values does not match table definition.

最奇怪的是,触发器中的每个语句都是独立工作的。如果您尝试将插入移动到临时表中,则几乎就像插入是一个一次性表,它会感染临时表。

那么是什么导致触发器失败呢?怎么能阻止呢?

最佳答案

我认为 David 和 Cervo 联合起来解决了这个问题。

我很确定发生的部分原因是我们在多个触发器中使用了#newMatches。当一个触发器更改某些行时,它会触发另一个触发器,该触发器将尝试使用连接范围的#newMatches。

结果,它会尝试找到已经存在的具有不同模式的表,然后死亡,并生成上面的消息。一项支持的证据:插入是否使用堆栈样式范围(嵌套触发器有自己的插入?)

仍在猜测 - 至少现在事情似乎正在奏效!

关于sql - T-SQL 触发器触发 "Column name or number of supplied values does not match table definition"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/95218/

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