sql server在不匹配时合并多个插入
我在查询中使用 MERGE,并且在 WHEN NOT MATCHED THEN 子句上进行 INSERT,但随后我想获取插入的行标识并对其他表进行另一个 INSERT。现在的查询是:
ALTER PROCEDURE [dbo].[BulkMergeOffers]
@data ImportDataType READONLY
AS
SET NOCOUNT ON;
DECLARE @cid int = 0
MERGE dbo.oferta AS target
USING @data AS source
ON (target.nr_oferty = source.nr_oferty)
WHEN NOT MATCHED THEN
INSERT (nr_oferty,rynek,typ_transakcji, typ_nieruchomosci,cena,powierzchnia, rok_budowy, wojewodztwo, miasto, dzielnica, ulica, opis, wspolrzedne, film, zrodlo, KontaktStore, data, forma_wlasnosci, stan_techniczny, liczba_pokoi, liczba_peter, pietro, material, kuchnia, pow_dzialki, typ_dzialki, woda,gaz, prad,sila, przeznaczenie,lokal_dane)
VALUES (source.nr_oferty,source.rynek,source.typ_transakcji, source.typ_nieruchomosci,source.cena,source.powierzchnia, source.rok_budowy, source.wojewodztwo, miasto, source.dzielnica, source.ulica, source.opis, source.wspolrzedne, source.film, source.zrodlo, source.KontaktStore, source.data, source.forma_wlasnosci, source.stan_techniczny, source.liczba_pokoi, source.liczba_peter, source.pietro, source.material, source.kuchnia, source.pow_dzialki, source.typ_dzialki, source.woda,source.gaz, source.prad,source.sila, source.przeznaczenie,source.lokal_dane);
所以正如你所看到的,我需要根据源数据将一些值插入到目标表中,然后我需要获取插入标识并将其插入到另一个表中,但也基于一些源数据,所以类似的事情,就在第一次插入之后:
SET @cid = SCOPE_IDENTITY();
if source.photo is not null
begin
insert into dbo.photos(offerID, file) values (@cid, source.photo);
end
但我无法组装它,无法再访问源代码,而且 if 语句显示错误:
“多部分标识符 来源.照片无法绑定"
但它确实存在。为了清楚起见,ImportDataType 是一个表值参数。
请帮助
I'm using MERGE in my query and i'm making INSERT on clause WHEN NOT MATCHED THEN, but then i would like to get the inserted row identity and make another INSERT to some other table. Query for now is:
ALTER PROCEDURE [dbo].[BulkMergeOffers]
@data ImportDataType READONLY
AS
SET NOCOUNT ON;
DECLARE @cid int = 0
MERGE dbo.oferta AS target
USING @data AS source
ON (target.nr_oferty = source.nr_oferty)
WHEN NOT MATCHED THEN
INSERT (nr_oferty,rynek,typ_transakcji, typ_nieruchomosci,cena,powierzchnia, rok_budowy, wojewodztwo, miasto, dzielnica, ulica, opis, wspolrzedne, film, zrodlo, KontaktStore, data, forma_wlasnosci, stan_techniczny, liczba_pokoi, liczba_peter, pietro, material, kuchnia, pow_dzialki, typ_dzialki, woda,gaz, prad,sila, przeznaczenie,lokal_dane)
VALUES (source.nr_oferty,source.rynek,source.typ_transakcji, source.typ_nieruchomosci,source.cena,source.powierzchnia, source.rok_budowy, source.wojewodztwo, miasto, source.dzielnica, source.ulica, source.opis, source.wspolrzedne, source.film, source.zrodlo, source.KontaktStore, source.data, source.forma_wlasnosci, source.stan_techniczny, source.liczba_pokoi, source.liczba_peter, source.pietro, source.material, source.kuchnia, source.pow_dzialki, source.typ_dzialki, source.woda,source.gaz, source.prad,source.sila, source.przeznaczenie,source.lokal_dane);
So as you see i need to insert some values to the target table based on source data, then i need to take the insert identity and insert it into another table but also based on some source data, so something like that, just after the first insert:
SET @cid = SCOPE_IDENTITY();
if source.photo is not null
begin
insert into dbo.photos(offerID, file) values (@cid, source.photo);
end
But i can't assemble it, a have no access to the source no more, also if statement show error :
"the multi-part identifier
source.photo can not be bound"
but it is there. Just for clarity ImportDataType is a table-valued parameter.
Please HELP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的查询中不需要
MERGE
语句的WHEN MATCHED
部分,则没有真正的理由使用MERGE
。您可以将INSERT
与外部联接或NOT EXISTS
语句一起使用。无论哪种情况,您都可以使用 OUTPUT 子句检索插入的标识值并将其传递给第二个查询。
我扩展了你的例子:
If you don't need the
WHEN MATCHED
part of theMERGE
statement in your query, there's no real reason to useMERGE
. You could useINSERT
with an outer join orNOT EXISTS
statement.In either case, you can use the
OUTPUT
clause to retrieve the inserted identity value an pass it on to a second query.I've extended your example: