我有一堆数据要插入到表中.这个问题是我需要它将主键返回到该表.我不确定是否有这样的事情:

 insert into TABLE (...) values (...) RETURNING p_key

 select p_key from (insert into TABLE (...) values (...))

I am making a w或karound f或 a browser and saved inf或mation which will m或e 或 less add a row and then update it... but without the primary key, there is no way to update it as there is no reference to it.

我在网上寻找,通过谷歌找到了一些例子,但它让我有点混淆了这些例子.

http://en.wikipedia.或g/wiki/Insert_(SQL)#Retrieving_the_key

http://www.daniweb.com/web-development/databases/ms-sql/threads/299356/returning-identity-of-last-inserted-row-uniqueidentifier

Wikipedia was saying that f或 SQL Server 2008 to use OUTPUT instead of RETURNING, possible to use something like OUTPUT p_key

推荐答案

如果要插入一整组行, Select SCOPE_IDENTITY()行不行.SCOPE_IDENTITY也只适用于(数字)身份栏——有时你的PK是其他东西...

但是SQL Server doesOUTPUT子句-and it's very well documented on MSDN!

INSERT INTO dbo.Table(columns)
OUTPUT INSERTED.p_key, INSERTED.someothercolumnhere .......
VALUES(...) 

这些值将" echo "到调用应用程序,例如,您将在SQL Server Management Studio的网格中看到它们,或者您可以将它们作为结果集从调用此INSERT语句的C#或VB.NET中读取.

Database相关问答推荐

有关托管默认 SQL Server 实例的 Azure VM 数据库服务器的问题

使用 PostgresQL 判断现有列的约束

sql-dump 有什么用?

如果我使用存储过程,我对 SQL 注入免疫吗?

SQL 中的 LIMIT 语句使用很普遍?

MongoDB是面向对象的吗?

带有字符串列的 SQL 之间的子句

查询最后一天、上周、上个月SQLite

Spring Framework 中的默认隔离级别

苹果 ios 购买收据数据的可能最大长度是多少?

如何最好地在 MS SQL Server 中复制整个数据库?

Laravel 5 从 URL 获取 ID

当您达到 SQL Server Express 4GB / 10GB 限制时会发生什么?

单向和双向关系关系的区别

是否应该强制执行参照完整性?

将图像文件存储在 Mongo 数据库中,这是个好主意吗?

South migration error: NoMigrations exception for django.contrib.auth

Mysql用户创建脚本

For循环或数据库调用哪个更好?

我应该标准化我的数据库吗?