在 INSERT 或 UPDATE 语句中增加一个变量

问题描述 投票:0回答:0

我一直在尝试更新一些人口脚本,以避免手动分配 ID(减少拼写错误风险)并在条目顺序上传递更多内容。我一直在努力寻找满足我需求的示例,但我只是受阻而无法思考解决方案。 那么,我想做什么?基本上,我想知道是否有办法做这样的事情:

declare @id int = 1
declare @foreignId int = 10

SET IDENTITY_INSERT [TableA] ON

PRINT 'Seeding TableA';
MERGE TableA as Target
USING ( 
    SELECT * FROM (VALUES 
    (@id += 1, 10, @foreignId += 1, 'testinguri1'),
    (@id += 3, 11, @foreignId += 1, 'testinguri2'),
    -- ids 3 and 4 left open intentionally
    (@id += 1, 12, @foreignId += 1, 'testinguri5'),
    (@id += 1, 13, @foreignId += 1, 'testinguri6')
) AS s (
      [TableAID]
    , [TableBID]
    , [TableCID]
    , [URI]
    )
    ) AS Source
ON Target.TableAID = Source.[TableAID]
WHEN MATCHED THEN
    UPDATE SET    
          ,Target.TableBID = Source.TableBID
          ,Target.TableCID = Source.TableCID
          ,Target.URI = Source.URI
    
WHEN NOT MATCHED THEN
    INSERT (
            [TableAID]
            , [TableBID]
            , [TableCID]
            , [URI]
            )
    VALUES (
            Source.TableAID
          , Source.TableBID
          , Source.TableCID
          , Source.URI
          );
--OUTPUT $action,deleted.*,inserted.*;

SET IDENTITY_INSERT [TableA] OFF

下一个作为表条目:

| TableAID | TableBID | TableCID |             URI            |
| -------- | -------- | -------- | -------------------------- |
|     1    |    10    |    10    | 'testinguri1' |
|     2    |    11    |    11    | 'testinguri2' |
|     5    |    12    |    12    | 'testinguri5' |
|     6    |    13    |    13    | 'testinguri6' |

但很遗憾,上面的代码不起作用。我已经看到可以做类似的事情:

set @id = @id + 1

但这对我不起作用,我不想分割我的插入只是为了增加@id 值然后继续增加值。

我也看过一些使用 row_number() 的例子,并尝试了一些没有成功的事情,即:

declare @id int = 1
declare @foreignId int = 10

SET IDENTITY_INSERT [TableA] ON

PRINT 'Seeding TableA';
MERGE TableA as Target
USING ( 
    SELECT * FROM (VALUES 
    (1, 10, 1, 'testinguri1'),
    (3, 11, 1, 'testinguri2'),
    -- ids 3 and 4 left open intentionally
    (3, 12, 1, 'testinguri5'), -- now always adding +3
    (3, 13, 1, 'testinguri6')  -- else ids would decrease
) AS s (
      [TableAID]
    , [TableBID]
    , [TableCID]
    , [URI]
    )
    ) AS Source
ON Target.TableAID = row_number() over (order by Source.[TableAID]) + Source.[TableAID]
AND Target.TableBID = row_number() over (order by Source.[TableAID]) + Source.[TableBID]
WHEN MATCHED THEN
    UPDATE SET    
          ,Target.TableBID = Source.TableBID
          ,Target.TableCID = Source.TableCID
          ,Target.URI = Source.URI
    
WHEN NOT MATCHED THEN
    INSERT (
            [TableAID]
            , [TableBID]
            , [TableCID]
            , [URI]
            )
    VALUES (
            Source.TableAID
          , Source.TableBID
          , Source.TableCID
          , Source.URI
          );
--OUTPUT $action,deleted.*,inserted.*;

SET IDENTITY_INSERT [TableA] OFF

现在我没有得到语法错误,但我确实得到了编译错误

enter image description here

我的知识匮乏让我无所适从,有谁知道实现我想要的东西的方法吗?

sql sql-server variables
© www.soinside.com 2019 - 2024. All rights reserved.