1

我有一个表项目,它应该在表 LanguageText 中有相应的记录。LanguageText 的 ID(身份)注册在我的 Items 表的 Items.LanguageTextId 字段中。

我想要完成的是在 Items.LanguageTextId 中为 null 的所有记录的 LanguageText 和 Items 之间的合并,并在 LanguageText 中插入这些 Item 记录的项目名称/文本,并使用新插入的 LanguageText 记录中的 ID 值更新 LanguageTextId (SCOPE_IDENTITY( )?)

插入工作正常:

MERGE [dbo].[LanguageText] AS target
USING (SELECT [Items].* from [dbo].Items ) AS source 
ON (TARGET.Id = SOURCE.LanguageTextId) 
WHEN NOT MATCHED By Target THEN 
    INSERT   
       ([Text])
 VALUES
       (source.[ItemName]);

结尾

但我不知道如何更新我的 items.Languagetextid,我可以做些什么: OUTPUT $action, INSERTED.ID 吗?或者有没有更好的方法来完成这个?

提前致谢,

麦克风

4

3 回答 3

7

您可以通过合并..输出到表变量然后更新来做到这一点。

SQL小提琴

MS SQL Server 2008 架构设置

create table Items
(
  ItemsId int identity primary key,
  ItemName nvarchar(50) not null,
  ItemsLanguageTextId int null
);

create table ItemsLanguageText
(
  ItemsLanguageTextId int identity primary key,
  Text nvarchar(50) not null
);

insert into Items values('Name 1', null);
insert into Items values('Name 2', null);
insert into Items values('Name 3', null);

查询 1

declare @T table
(
  ItemsId int,
  ItemsLanguageTextId int
);

merge ItemsLanguageText as T
using (
        select ItemsId, ItemName
        from Items
        where ItemsLanguageTextId is null
      ) as S
on 0 = 1
when not matched then
  insert (Text) values (S.ItemName)
output S.ItemsId, inserted.ItemsLanguageTextId
  into @T;

update Items
set ItemsLanguageTextId = T.ItemsLanguageTextId
from @T as T
where T.ItemsId = Items.ItemsId;

结果

查询 2

select * from Items;

结果

| ITEMSID | ITEMNAME | ITEMSLANGUAGETEXTID |
--------------------------------------------
|       1 |   Name 1 |                  13 |
|       2 |   Name 2 |                  14 |
|       3 |   Name 3 |                  15 |

查询 3

select * from ItemsLanguageText;

结果

| ITEMSLANGUAGETEXTID |   TEXT |
--------------------------------
|                  13 | Name 1 |
|                  14 | Name 2 |
|                  15 | Name 3 |
于 2012-11-07T18:14:34.083 回答
2

我认为您正在寻找的是添加 WHEN MATCHED。

例如

WHEN MATCHED THEN UPDATE SET.... etc etc
WHEN NOT MATCHED THEN INSERT ... etc etc

您可以查看此 MERGE 交易文档以获取有关如何正确使用 MERGE 的详细信息。 http://technet.microsoft.com/en-us/library/bb510625.aspx

于 2012-11-07T11:57:06.030 回答
0

当涉及到 SQL 时,我仍然是一个菜鸟......我猜它不能通过合并来完成,所以我尝试了一个带有游标的存储过程,该游标循环遍历记录并执行我想要它做的事情:

CREATE PROCEDURE [dbo].[PM_CreateItemsLanguageTexts]
AS
DECLARE @ITEMNAME NVARCHAR(50);
DECLARE @ITEMID BIGINT;

BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--Declare Cursor so we can iterate each record
Declare c Cursor For Select Id, ItemName From dbo.Items WHERE NameId is null 
Open c 
--Fetch First
Fetch next From c into @ITEMID, @ITEMNAME

--As long as we are 'on a roll' go with the flow..
While @@Fetch_Status=0 
Begin

   PRINT @ITEMNAME;
   PRINT @ITEMID;

   --FIRST INSERT A NEW TEXT RECORD IN ItemsLauguageText
   INSERT dbo.ItemsLauguageText(Text) VALUES (@ITEMNAME);    

   --UPDATE THE ITEMS RECORD NAMEID FIELD WITH THE LATEST IDENTITY / ID VALUE
    UPDATE dbo.Items SET NameID = CAST(SCOPE_IDENTITY() AS bigint)  WHERE dbo.Items.Id = @ITEMID; 

Fetch next From c into @ITEMID,@ITEMNAME
End
End

CLOSE C;
DEALLOCATE C;
于 2012-11-07T13:49:24.887 回答