如何将最后插入的 id(PK) 的值存储到主键是 VB.Net 中的 GUID 的变量中
How to store the value of last inserted id(PK) into a variable where the primary key is a GUID in VB.Net
我的问题与此处提出的问题有关 How to get last inserted id?
但是 scope_identity() 对我不起作用,因为在我的情况下 table 的主键值是一个 GUID 值。
我也看到这里的问题了
SQL Server - Return value after INSERT
但 link 没有解释我如何将值存储在变量中。我需要将值存储在可用于多次输入的变量中。
我正在将硬编码值插入多个 SQL 服务器 table。所有主键列都是 GUID。
table结构如下
这是我用来将数据插入调查的代码 table。
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim survey = Guid.NewGuid().ToString()
Dim SurveyTitle As String = "Diversity Monitoring Survey"
Dim SurveyDetail As String = ""
Core.DB.DoQuery("insert into survey(id,title, detail,employerid,userid) values(@id,@title, @detail, @eid, @uid);", Core.DB.SIP("title", SurveyTitle), Core.DB.SIP("detail", SurveyDetail), Core.DB.SIP("eid", LocalHelper.UserEmployerID()), Core.DB.SIP("uid", LocalHelper.UserID()), Core.DB.SIP("id", survey))
End Sub
DoQuery 在哪里
Shared Sub DoQuery(ByVal commandText As String, ByVal ParamArray params As SqlParameter())
Dim conn As SqlConnection = Nothing
Try
conn = GetOpenSqlConnection()
DoQuery(conn, commandText, params)
Finally
If conn IsNot Nothing Then conn.Dispose()
End Try
End Sub
现在我想检索刚刚插入的 SurveyId
值并将其存储到变量 strSurveyId
Dim strSurveyID As String
这样我就可以将该值插入 table SurveyQuestionCategory
:
Core.DB.DoQuery("insert into surveyquestioncategory(title, detail, surveyid) values(@title, @detail, @sid)", Core.DB.SIP("title", strSurveyQuestionCategoryTitle), Core.DB.SIP("detail", strSurveyQuestionCategoryDetail), Core.DB.SIP("sid", strSurveyID))
scope_identity()
在我的情况下不起作用,因为它是 GUID。
我试过了
SELECT * from [MPBlLiteDev].[dbo].[Survey] where id=SCOPE_IDENTITY()
但是它给我一个错误
操作数类型冲突:uniqueidentifier 与 numeric
不兼容
请提供代码。
SCOPE_IDENTITY() 只会 return 新生成的标识值(如果有的话),对于 GUID 值,您需要 table variable
和 OUTPUT
子句像这样插入语句......
DECLARE @NewIDs TABLE (ID UNIQUEIDENTIFIER)
insert into survey(id,title, detail,employerid,userid)
OUTPUT inserted.id INTO @NewIDs(ID)
values(@id,@title, @detail, @eid, @uid);
SELECT * FROM @NewIDs
请执行以下存储过程
Create proc SPInsertSurvey
(
@Title varchar(MAX),
@OutSurveyID UNIQUEIDENTIFIER output
)
as
DECLARE @Table TABLE (SurveyID UNIQUEIDENTIFIER)
begin
insert into survey(Title)
Output inserted.SurveyID into @Table values (@Title)
set @OutSurveyID =(select SurveyID from @Table)
end
您可以使用以下语法执行它
DECLARE @return_value int,
@OutSurveyID uniqueidentifier
EXEC @return_value = [dbo].[SPInsertSurvey]
@Title = N'''S1''',
@OutSurveyID = @OutSurveyID OUTPUT
SELECT @OutSurveyID as N'@OutSurveyID'
SELECT 'Return Value' = @return_value
希望这会有所帮助
我的问题与此处提出的问题有关 How to get last inserted id?
但是 scope_identity() 对我不起作用,因为在我的情况下 table 的主键值是一个 GUID 值。
我也看到这里的问题了 SQL Server - Return value after INSERT
但 link 没有解释我如何将值存储在变量中。我需要将值存储在可用于多次输入的变量中。
我正在将硬编码值插入多个 SQL 服务器 table。所有主键列都是 GUID。
table结构如下
这是我用来将数据插入调查的代码 table。
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim survey = Guid.NewGuid().ToString()
Dim SurveyTitle As String = "Diversity Monitoring Survey"
Dim SurveyDetail As String = ""
Core.DB.DoQuery("insert into survey(id,title, detail,employerid,userid) values(@id,@title, @detail, @eid, @uid);", Core.DB.SIP("title", SurveyTitle), Core.DB.SIP("detail", SurveyDetail), Core.DB.SIP("eid", LocalHelper.UserEmployerID()), Core.DB.SIP("uid", LocalHelper.UserID()), Core.DB.SIP("id", survey))
End Sub
DoQuery 在哪里
Shared Sub DoQuery(ByVal commandText As String, ByVal ParamArray params As SqlParameter())
Dim conn As SqlConnection = Nothing
Try
conn = GetOpenSqlConnection()
DoQuery(conn, commandText, params)
Finally
If conn IsNot Nothing Then conn.Dispose()
End Try
End Sub
现在我想检索刚刚插入的 SurveyId
值并将其存储到变量 strSurveyId
Dim strSurveyID As String
这样我就可以将该值插入 table SurveyQuestionCategory
:
Core.DB.DoQuery("insert into surveyquestioncategory(title, detail, surveyid) values(@title, @detail, @sid)", Core.DB.SIP("title", strSurveyQuestionCategoryTitle), Core.DB.SIP("detail", strSurveyQuestionCategoryDetail), Core.DB.SIP("sid", strSurveyID))
scope_identity()
在我的情况下不起作用,因为它是 GUID。
我试过了
SELECT * from [MPBlLiteDev].[dbo].[Survey] where id=SCOPE_IDENTITY()
但是它给我一个错误 操作数类型冲突:uniqueidentifier 与 numeric
不兼容请提供代码。
SCOPE_IDENTITY() 只会 return 新生成的标识值(如果有的话),对于 GUID 值,您需要 table variable
和 OUTPUT
子句像这样插入语句......
DECLARE @NewIDs TABLE (ID UNIQUEIDENTIFIER)
insert into survey(id,title, detail,employerid,userid)
OUTPUT inserted.id INTO @NewIDs(ID)
values(@id,@title, @detail, @eid, @uid);
SELECT * FROM @NewIDs
请执行以下存储过程
Create proc SPInsertSurvey
(
@Title varchar(MAX),
@OutSurveyID UNIQUEIDENTIFIER output
)
as
DECLARE @Table TABLE (SurveyID UNIQUEIDENTIFIER)
begin
insert into survey(Title)
Output inserted.SurveyID into @Table values (@Title)
set @OutSurveyID =(select SurveyID from @Table)
end
您可以使用以下语法执行它
DECLARE @return_value int,
@OutSurveyID uniqueidentifier
EXEC @return_value = [dbo].[SPInsertSurvey]
@Title = N'''S1''',
@OutSurveyID = @OutSurveyID OUTPUT
SELECT @OutSurveyID as N'@OutSurveyID'
SELECT 'Return Value' = @return_value
希望这会有所帮助