次の例に示すように、1つのシーケンスを複数のテーブルに使用できます。
CREATE SEQUENCE dbo.MyTableID
START WITH 1
INCREMENT BY 1
NO CACHE
;
GO
CREATE TABLE dbo.[MyTable1]
(
[ID] [bigint] PRIMARY KEY NOT NULL DEFAULT (NEXT VALUE FOR dbo.MyTableID),
[Title1] [nvarchar](64) NOT NULL
);
CREATE TABLE dbo.[MyTable2]
(
[ID] [bigint] PRIMARY KEY NOT NULL DEFAULT (NEXT VALUE FOR dbo.MyTableID),
[Title2] [nvarchar](64) NOT NULL
);
--Insert 2 rows
insert into [MyTable1] (title1)
select 'title11'
insert into [MyTable1] (title1)
select 'title12';
insert into [MyTable2] (title2)
select 'title21'
insert into [MyTable2] (title2)
select 'title22';
select f1.*, 'Into MyTable1' Tb from [MyTable1] f1
union all
select f1.*, 'Into MyTable2' Tb from [MyTable2] f1
出力:
+----+---------+---------------+
| ID | Title1 | Tb |
+----+---------+---------------+
| 1 | title11 | Into MyTable1 |
| 2 | title12 | Into MyTable1 |
| 3 | title21 | Into MyTable2 |
| 4 | title22 | Into MyTable2 |
+----+---------+---------------+
sqlfiddle
cache
この場合に使用しても安全ですか?接続リンクで、Microsoftはを使用するように指示していますno cache
。