4

「ID」列であるIDフィールドを持つ挿入ステートメントを使用できるかどうか、および割り当てられた値を同じレコードの別のフィールドに同じ挿入ステートメントで挿入できるかどうかを尋ねられました。

これは可能ですか(SQL Server 2008r2)?

ありがとう。

4

6 に答える 6

9

実際にはこれを行うことはできません。列に使用される実際の値は、が完了しIDENTITYたときにのみ固定および設定されるためです。INSERT

ただし、トリガーなどを使用できます

CREATE TRIGGER trg_YourTableInsertID ON dbo.YourTable 
AFTER INSERT
AS 
UPDATE dbo.YourTable
SET dbo.YourTable.OtherID = i.ID
FROM dbo.YourTable t2
INNER JOIN INSERTED i ON i.ID = t2.ID

これは、行が挿入された直後に発生し、挿入された行OtherIDの列の値に列を設定しIDENTITYます。しかし、厳密に言えば、同じステートメント内ではなく、元のステートメントの直後です。

于 2011-11-17T17:47:37.510 に答える
1

これを行うには、テーブルに計算列を作成します。

 DECLARE    @QQ TABLE (ID INT IDENTITY(1,1), Computed AS ID PERSISTED, Letter VARCHAR (1))

INSERT INTO @QQ (Letter)
VALUES ('h'),
('e'),
('l'),
('l'),
('o')

SELECT  *
FROM    @QQ

1   1   h

2   2   e

3   3   l

4   4   l

5   5   o
于 2011-11-17T18:46:56.063 に答える
1

チェックした回答について:

実際にはこれを行うことはできません。IDENTITY 列に使用される実際の値は、INSERT が完了したときにのみ固定および設定されるためです。

marc_s私は、あなたは実際には正しくないと思います。はい、彼は出来ます!)))

解決方法は次のIDENT_CURRENT()とおりです。

CREATE TABLE TemporaryTable(
    Id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    FkId int NOT NULL 
)

ALTER TABLE TemporaryTable
    ADD CONSTRAINT [Fk_const] FOREIGN KEY (FkId) REFERENCES [TemporaryTable] ([Id])

INSERT INTO TemporaryTable (FkId) VALUES  (IDENT_CURRENT('[TemporaryTable]'))
INSERT INTO TemporaryTable (FkId) VALUES  (IDENT_CURRENT('[TemporaryTable]'))
INSERT INTO TemporaryTable (FkId) VALUES  (IDENT_CURRENT('[TemporaryTable]'))
INSERT INTO TemporaryTable (FkId) VALUES  (IDENT_CURRENT('[TemporaryTable]'))

UPDATE TemporaryTable 
   SET [FkId] = 3
 WHERE Id = 2

SELECT * FROM TemporaryTable

DROP TABLE TemporaryTable

さらに、IDENT_CURRENT()asを使用することもでき、たとえばDEFAULT CONSTRAINTの代わりに機能します。SCOPE_IDENTITY()これを試して:

CREATE TABLE TemporaryTable(
    Id int PRIMARY KEY IDENTITY(1,1) NOT NULL,
    FkId int NOT NULL DEFAULT IDENT_CURRENT('[TemporaryTable]')
)

ALTER TABLE TemporaryTable
    ADD CONSTRAINT [Fk_const] FOREIGN KEY (FkId) REFERENCES [TemporaryTable] ([Id])

INSERT INTO TemporaryTable (FkId) VALUES (DEFAULT)
INSERT INTO TemporaryTable (FkId) VALUES (DEFAULT)
INSERT INTO TemporaryTable (FkId) VALUES (DEFAULT)
INSERT INTO TemporaryTable (FkId) VALUES (DEFAULT)

UPDATE TemporaryTable 
   SET [FkId] = 3
 WHERE Id = 2

SELECT * FROM TemporaryTable

DROP TABLE TemporaryTable
于 2015-01-22T13:19:54.363 に答える
0

You can do both.

To insert rows with a column "identity", you need to set identity_insert off.

Note that you still can't duplicate values!

You can see the command here. Be aware to set identity_insert on afterwards.

To create a table with the same record, you simply need to:

  • create new column;
  • insert it with null value or other thing;
  • update that column after inserts with the value of the identity column.

If you need to insert the value at the same time, you can use the @@identity global variable. It'll give you the last inserted. So I think you need to do a @@identity + 1. In this case it can give wrong values because the @@identity is for all tables. So it'll count if the insert occurs in another table with identity.

Another solution is to get the max id and add one :) and you get the needed value!

于 2011-11-17T17:41:50.550 に答える