[Vb.Net + Sql Server 2008] Scope_Identity ed Insert

lunedì 11 ottobre 2010 - 16.40
Tag Elenco Tags  VB.NET  |  Visual Studio 2008  |  SQL Server 2008

nikkysixx Profilo | Junior Member

Salve a tutti,finora ho sempre utilizzato @@identity per ricavare l ultimo id inserito,ma leggendo in piu di un forum che è l approccio sbagliato ,sto cercando di capire come utilizzare scope_identity

La mia procedura di inserimento memorizza il risultato dello scope nella variabile di output id_user (l unico parametro è il nome di un utente ad esempio)
ALTER PROCEDURE [dbo].[insert_user] -- Add the parameters for the stored procedure here @nome nvarchar(255), @id_user INT OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here INSERT INTO anagrafica(nome,email) values (@nome) select SCOPE_IDENTITY() SET @id_user = CAST(SCOPE_IDENTITY() AS INT) END

Il codice in vb è invece

Dim conn As SqlConnection = New SqlConnection(conn_string) conn.Open() Dim cmd As SqlCommand = _ New SqlCommand("insert_user", conn) cmd.Parameters.Add(New SqlParameter("@nome", TextBox1.Text)) cmd.CommandType = CommandType.StoredProcedure Try cmd.ExecuteNonQuery() MsgBox(id_user) Catch ex As Exception MessageBox.Show(ex.Message) End Try

Ma ottengo errore,perchè la stored procedure vuole come parametro di input anche @id_user
Come posso modificare il codice per ottenere il valore corretto di id_user?

E sopratutto,non sto considerando il caso in cui l id non esista...come posso trattare questo caso?

Grazie per qualsiasi consiglio utile ;)

alexmed Profilo | Guru

Ciao
Al volo, se non ricordo male:

SQL

ALTER PROCEDURE [dbo].[insert_user] -- Add the parameters for the stored procedure here @nome nvarchar(255), @id_user INT OUTPUT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here INSERT INTO anagrafica(nome,email) values (@nome) select SCOPE_IDENTITY() As Ultimo_ID RETURN END

VB

Dim conn As SqlConnection = New SqlConnection(conn_string) conn.Open() Dim cmd As SqlCommand = _ New SqlCommand("insert_user", conn) cmd.Parameters.Add(New SqlParameter("@nome", TextBox1.Text)) cmd.CommandType = CommandType.StoredProcedure Try Dim id As Integer = cmd.ExecuteScalar() MsgBox(id) Catch ex As Exception MessageBox.Show(ex.Message) End Try

Prova un pò ...

Ciao

alexmed

nikkysixx Profilo | Junior Member

Innanzi tutto grazie,
Compilando ed eseguendo , l'errore che ottengo è il seguente

LA PROCEDURA O FUNZIONE INSERT_USER PREVEDE IL PARAMETRO @ID_USER CHE NON E'STATO SPECIFICATO


alexmed Profilo | Guru

In effetti!!! non lo usi quindi tanto vale non dichiararlo.

SQL

ALTER PROCEDURE [dbo].[insert_user] -- Add the parameters for the stored procedure here @nome nvarchar(255), AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here INSERT INTO anagrafica(nome,email) values (@nome) select SCOPE_IDENTITY() As Ultimo_ID RETURN END

Ciao

alexmed
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5