Friday, March 23, 2012

How to get the last item added

Not 100% sure how to do this so I would appreciate some directions. I have 2 tables, Systems and Contacts. A system can have 1 to infinite contacts and contact can have 1 to infinite systems. So I use a 3rd table Sys_Con to add the contact needed for each system.
Now my question is once I add the System and contact how do I know for sure which ContactID to add in my Sys_Con table? Because while I am doing this operation maybe someone can add an other contact. So is there a way for my store proc Add_Contact to return the contactID needed for my store proc Add_Sys_Con ?

TABLE Sys_Con
SystemID int
ContactID int

Table Contact
ContactID int Identity
ContactName
...

Table System
SystemID
...After your insert

SELECT @.@.IDENTITY AS 'Identity'

Here is the resource:@.@.IDENTITY

hope this helps,
sivilian|||Thanks for your reply,
how do I get the identity back from my insert sp of my contact? I dont seem to be able to make this work. I get the error: "Procedure 'Add_Cl_Contact' expects parameter '@.ContactId', which was not supplied." on the line "iContactID = cmdSelect.Parameters("ContactId").Value"

Here is what I have up to now:


ALTER procedure dbo.Add_Cl_Contact
(
@.ContactNamenvarchar(75),
@.Departmentnvarchar (50)=null,
@.Titlenvarchar(50)=null,
@.Phone1char(20)=null,
@.Phone2char(20)=null,
@.Phone3char(20)=null,
@.Ext1char(10)=null,
@.Ext2char(10)=null,
@.Ext3char(10)=null,
@.Faxnvarchar(50)=null,
@.Emailnvarchar(50)=null,
@.ContactIdintOUTPUT
)
AS
insert into Cl_Contacts (
ContactName,
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
Email
)
values (
@.ContactName,
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
@.Email
)
Select @.ContactId = @.@.IDENTITY

'******************************************************************
'******************************************************************
Public Function Add_Cl_Contact(ByVal strContactName As String, _
ByVal strDep As String, ByVal strEmail As String, ByVal strExt1 As String, ByVal strExt2 As String, _
ByVal strExt3 As String, ByVal strFax As String, ByVal strPhone1 As String, ByVal strPhone2 As String, _
ByVal strPhone3 As String, ByVal strTitle As String, ByRef iContactID As Integer, ByRef strError As String) As Boolean
'******************************************************************
Dim bSuccess As Boolean = True
Dim connect As New SqlConnection(strConnection)
Dim cmdSelect As New SqlCommand("Add_Cl_Contact", connect)
Dim paramReturnValue As SqlParameter

cmdSelect.CommandType = CommandType.StoredProcedure
'PARAM
cmdSelect.Parameters.Add("@.ContactName", strContactName)
cmdSelect.Parameters.Add("@.Department", strDep)
cmdSelect.Parameters.Add("@.Title", strTitle)
cmdSelect.Parameters.Add("@.Phone1", strPhone1)
cmdSelect.Parameters.Add("@.Phone2", strPhone2)
cmdSelect.Parameters.Add("@.Phone3", strPhone3)
cmdSelect.Parameters.Add("@.Ext1", strExt1)
cmdSelect.Parameters.Add("@.Ext2", strExt2)
cmdSelect.Parameters.Add("@.Ext3", strExt3)
cmdSelect.Parameters.Add("@.Fax", strFax)
cmdSelect.Parameters.Add("@.Email", strEmail)

paramReturnValue = cmdSelect.Parameters.Add("ContactId", SqlDbType.Int)
paramReturnValue.Direction = ParameterDirection.ReturnValue

Try
connect.Open()
cmdSelect.ExecuteNonQuery()
iContactID = cmdSelect.Parameters("ContactId").Value
connect.Close()
Catch ex As Exception
bSuccess = False
strError = ex.Message
Finally
If connect.State = ConnectionState.Open Then
connect.Close()
End If
End Try
Return bSuccess
End Function

|||I'd recommend using SCOPE_IDENTITY. check docs for more info about SCOPE_IDENTIY and @.@.IDENTITy..


insert into Cl_Contacts (
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
Email
)
values (
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
@.Email
)

SELECT @.contactid = SCOPE_IDENTITY()

hth

No comments:

Post a Comment