Sql server 从应用程序中同时插入SQL和匹配的审核

Sql server 从应用程序中同时插入SQL和匹配的审核,sql-server,tsql,Sql Server,Tsql,我有一个应用程序可以在表[p_R]中插入行,表的第一个字段是[PrimaryKey]。 现在我必须添加另一个表,[Actions],其中包含字段[PrimaryKey]、[P_R_PK]、[User]、[ActionTime] 当应用程序向[p_R]插入一行时,我不知道主密钥是什么,但我必须同时插入到[Actions],其中[p_R_PK]中的值是我刚刚添加到[p_R]的主密钥。如何将此PrimaryKey值获取到[P_R_PK] 作为参考,我将vb.net windows窗体与SQL Serv

我有一个应用程序可以在表[p_R]中插入行,表的第一个字段是[PrimaryKey]。 现在我必须添加另一个表,[Actions],其中包含字段[PrimaryKey]、[P_R_PK]、[User]、[ActionTime]

当应用程序向[p_R]插入一行时,我不知道主密钥是什么,但我必须同时插入到[Actions],其中[p_R_PK]中的值是我刚刚添加到[p_R]的主密钥。如何将此PrimaryKey值获取到[P_R_PK]


作为参考,我将vb.net windows窗体与SQL Server数据库一起使用。

您可以通过在插入后使用
选择SCOPE\u IDENTITY()
来检索它

例如:

DECLARE @T table (id int PRIMARY KEY IDENTITY, value int)

INSERT INTO @T (value) VALUES (2)

SELECT SCOPE_IDENTITY() new_pk

<>我也会考虑在一个事务中的一个存储过程中完成这一切。如果要插入多个表,事务将允许您在出现任何错误时回滚。

如果您使用存储过程将记录添加到[p\R],则可以在第一个表中调用包含主键的另一个存储过程。例如:

CREATE PROC AddToP_R
@field1 varchar(10),
@field2...10
AS
BEGIN
declare @pk int --primary key that's created upon inserting

--insert into [P_R]
INSERT INTO [P_R]
VALUES (@field1,...)

--set the var we created to be the primary key
SET @pk = SCOPE_IDENTITY()

--call second proc
EXEC Second_Proc @pk
END
CREATE PROC AddWrapper
@fieldsforfirstproc...,
@fieldsforsecondproc...
AS
BEGIN
declare @outputVar int --primary key
EXEC firstproc @fieldsforfirstproc..., @outputvar output --adds the record to the first table and returns @outputvar as the primary key

EXEC secondproc @fieldsforsecondproc..., @outputvar --adds the record to the second table using @output var
END
如果在第二个存储过程中需要其他字段,请将它们包括在第一个过程参数列表中

另一种方法是创建一个包装器存储过程,该过程同时调用另外两个。要使其工作,您需要在第一个过程中使用一个输出变量来返回主键。例如:

CREATE PROC AddToP_R
@field1 varchar(10),
@field2...10
AS
BEGIN
declare @pk int --primary key that's created upon inserting

--insert into [P_R]
INSERT INTO [P_R]
VALUES (@field1,...)

--set the var we created to be the primary key
SET @pk = SCOPE_IDENTITY()

--call second proc
EXEC Second_Proc @pk
END
CREATE PROC AddWrapper
@fieldsforfirstproc...,
@fieldsforsecondproc...
AS
BEGIN
declare @outputVar int --primary key
EXEC firstproc @fieldsforfirstproc..., @outputvar output --adds the record to the first table and returns @outputvar as the primary key

EXEC secondproc @fieldsforsecondproc..., @outputvar --adds the record to the second table using @output var
END
我更喜欢第二个选项,因为它删除了第一个过程中不需要的逻辑。然而,第一个过程与我之前展示的略有不同

CREATE PROC AddToP_R
@field1 varchar(10),
@field2...10,
@pk int OUTPUT --primary key that's created upon inserting
AS
BEGIN
--insert into [P_R]
INSERT INTO [P_R]
VALUES (@field1,...)

--set the var we created to be the primary key
SET @pk = SCOPE_IDENTITY()
END

就个人而言,如果通过VB执行此命令,我将执行一个存储过程命令(通过设置命令类型),并将所有必要的值/数据作为参数传入)。如果您愿意,您可以通过初始命令将新的PK返回到代码中。在您提供的代码中,表名是在哪里输入的?我使用了一个表变量,因此您不需要插入到
@T
,而是插入到表中-
插入到[TableName]
。所有这些都可以通过OLEDB连接发送吗?我以前只使用过简单的单行字符串。我不太清楚。我不明白为什么不,尽管我不熟悉OLEDB连接。我猜是的。我个人使用的SqLConnection也继承自DbConnection。我现在要下班了,但我回家后会四处看看,除非你那时已经找到解决办法。这可能会有帮助。。。存储过程通常存储在数据库中。您正在使用SQL Management studio吗?如果是这样,您可以在那里创建它们,并从VB项目中调用它们。但是是的!希望我帮了一些忙!肯定是的,我确实想在那里创建它们,但我似乎没有MS的页面所说的相同的工具,所以我现在把它放在次要位置。不过,今天我学到了一些新东西:)