本文介绍了使用参数化插入sql server - 获取新的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是新手使用sql server和参数化sql。我希望能够返回已插入列的值。

这是我的陈述


strSqlInsetrtTrack = _

" INSERT INTO TRACK(CASE_NUM,CERT_NUM,CLMT_NUM,BROKER_NAME," + _

" CONTRACT_SEQ,TRACK_COMMENTS,DATE_OVER_SPEC,LAST_TOUCHED)" + _

" VALUES(''" + strCase +"'',''" + strCert +"''," + _

"''" + strClmt +"'',@ BrokerName,''" + strContractSeq +

"'',@ TrackComments," + _

"''" + txtDateOverSpec.Text +"'',GETDATE())" + _

" SET @TrackId = TRACK_ID"


然后我分配参数,我想我可以获得TRACK_ID

来自新插入的行,这是一个自动枚举的字段。

无论如何,这里是参数:


使用comInsertTrack

.Parameters.Add(" @ BrokerName",SqlDbType.Char,50)

.Parameters.Add(" @ TrackComments",SqlDbType.VarChar,500)

.Parameters(" @ BrokerName")。Value = txtBrokerName.Text

.Parameters(" @ TrackComments")。Value = txtTrackComments.Text

.Parameters .Add(" @ TrackId",SqlDbType.Int)

.Parameters(" @ TrackId")。Direction = ParameterDirection.ReturnValue

.ExecuteNonQuery()


strTrackid = CType(.Parameters(" @ TrackId")。Value,String)


结束


我甚至不确定这是否是返回值的意图

enum执行者。 。 。如果它可以工作真的很酷:)


错误是:


SqlException:必须声明变量''@TrackId' '


在.ExecuteNonQuery()行。


感谢您的帮助。祝你有美好的一天!


瑞恩

Hi all, I am new to using sql server and parameterized sql. I am
hoping to be returned the value of a column that has been inserted.
Here is my statement

strSqlInsetrtTrack = _
"INSERT INTO TRACK (CASE_NUM, CERT_NUM, CLMT_NUM, BROKER_NAME, " + _
"CONTRACT_SEQ, TRACK_COMMENTS, DATE_OVER_SPEC, LAST_TOUCHED) " + _
"VALUES (''" + strCase + "'',''" + strCert + "''," + _
"''" + strClmt + "'',@BrokerName,''" + strContractSeq +
"'',@TrackComments," + _
"''" + txtDateOverSpec.Text + "'',GETDATE()) " + _
"SET @TrackId = TRACK_ID"

Then I assign the parameters and I thought I could obtain the TRACK_ID
from the newly inserted row, which is an auto-enumerated field.
Anyway, here are the parameters:

With comInsertTrack
.Parameters.Add("@BrokerName", SqlDbType.Char, 50)
.Parameters.Add("@TrackComments", SqlDbType.VarChar, 500)
.Parameters("@BrokerName").Value = txtBrokerName.Text
.Parameters("@TrackComments").Value = txtTrackComments.Text
.Parameters.Add("@TrackId", SqlDbType.Int)
.Parameters("@TrackId").Direction = ParameterDirection.ReturnValue
.ExecuteNonQuery()

strTrackid = CType(.Parameters("@TrackId").Value, String)

End With

I''m not even sure this is waht was intended by the returnvalue
enumerator . . . it would be really cool if it would work :)

The errror is:

SqlException: Must declare the variable ''@TrackId''

On the .ExecuteNonQuery() line.

Thank you for any assistance. Have a great day!

Ryan

推荐答案









这意味着将局部变量@Identity设置为系统变量@@ Identity,

等于当前登录下的最新自动增量值

到SQL Server 。



It meant set the local variable @Identity to the system variable @@Identity,
which equates to the most recent autoincrement value under the current login
to SQL Server.


这篇关于使用参数化插入sql server - 获取新的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 04:58