简体   繁体   English

SQL插入相关表

[英]SQL insert into related tables

This seems to me to be the kind of issue that would crop up all the time with SQL/database development, but then I'm new to all this, so forgive my ignorance. 在我看来,这是一个随着SQL /数据库开发一直出现的问题,但后来我对这一切都不熟悉,所以请原谅我的无知。

I have 2 tables: 我有2张桌子:

CREATE TABLE [dbo].[Tracks](
    [TrackStringId] [bigint] NOT NULL,
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Time] [datetime] NOT NULL,
 CONSTRAINT [PK_Tracks] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
        IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,
        ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Tracks] CHECK CONSTRAINT [FK_Tracks_AudioStreams]
GO

ALTER TABLE [dbo].[Tracks]  WITH CHECK ADD  CONSTRAINT
[FK_Tracks_TrackStrings]     FOREIGN KEY([TrackStringId])
REFERENCES [dbo].[TrackStrings] ([Id])
GO

ALTER TABLE [dbo].[Tracks] CHECK CONSTRAINT [FK_Tracks_TrackStrings]
GO

and

CREATE TABLE [dbo].[TrackStrings](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [String] [nvarchar](512) NOT NULL,
 CONSTRAINT [PK_Strings] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
        IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,
        ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

I want to insert a new entry into the tracks table. 我想在track表中插入一个新条目。 This will also involve inserting a new entry in the trackstrings table, and ensuring that the foreign key column trackstringid in tracks points to the new entry in trackstrings. 这还将涉及在tracktrings表中插入一个新条目,并确保轨道中的外键列tracktringid指向tracktrings中的新条目。 What is the most efficient means of achieving this? 实现这一目标的最有效方法是什么?

First, insert into TrackStrings , omitting the primary key column from the column list. 首先,插入TrackStrings ,省略列列表中的主键列。 This invokes its IDENTITY column which generates a value automatically. 这将调用其IDENTITY列,该列自动生成值。

INSERT INTO [dbo].[TrackStrings] ([String]) 
  VALUES ('some string');

Second, insert into Tracks and specify as its TrackStringId the function SCOPE_IDENTITY() , which returns the most recent value generated by an IDENTITY column in your current scope . 其次,插入Tracks并将其作为TrackStringId指定为函数SCOPE_IDENTITY() ,该函数返回当前作用域中 IDENTITY列生成的最新值。

INSERT INTO [dbo].[Tracks] ([TrackStringId], [Time]) 
  VALUES (SCOPE_IDENTITY(), CURRENT_TIMESTAMP());

如果您正在使用SQL Server 2005或更高版本并在单个INSERT中插入大量记录,则可以在此处查看OUTPUTOUTPUT INTO选项以使用第二个插入中的标识,而无需“重新查找”获取所有IDENTITY值的行。

First insert into the primary table. 首先插入主表。

INSERT INTO trackstrings VALUES('myvalue')

Next get the identity. 接下来得到身份。 This method depends on whether you're are doing it all in 1 statement or a stored procedure or some other method. 此方法取决于您是在1语句还是存储过程或其他方法中完成所有操作。 I will assume 1 statement so I'll just insert with the identity special variable. 我将假设1个语句,所以我将插入身份特殊变量。

INSERT INTO tracks VALUES( @@IDENTITY, getdate() )

Something like that should do it depending on your exact scenario. 这样的事情应该根据你的具体情况来做。 The key is the @@IDENTITY variable. 关键是@@ IDENTITY变量。 It holds the last inserted identity value for the connection you are using. 它包含您正在使用的连接的最后插入标识值。 It is not table specific, it is simply the most recent identity inserted during the connections lifespan. 它不是特定于表的,它只是在连接生命周期中插入的最新标识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM