Quantcast
Channel: VBForums - Database Development
Viewing all articles
Browse latest Browse all 2918

SQL Stored Procedure

$
0
0
Ok i have switched from Access to SQL and i have a quick question.

I'm creating a stored procedure to handle the the IDENTITY

Code:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[RETURNID]
        @FirstName varchar(50),
        @LastName varchar(50),
        @BirthDate datetime,
        @City varchar(50),
        @Country varchar(50),
        @id int output
AS
BEGIN
        SET NOCOUNT ON;
        INSERT INTO  Employees (FirstName, LastName, BirthDate, City, Country)
        VALUES (@FirstName, @LastName, @BirthDate, @City, @Country)
        SET @id=SCOPE_IDENTITY()
        RETURN  @id
END


Code:

'Without Output parameter

Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString

Dim con As New SqlConnection(strConnString)

Dim cmd As New SqlCommand()

cmd.CommandType = CommandType.StoredProcedure

cmd.CommandText = "RETURNID"

cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = txtFirstName.Text.Trim()

cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtLastName.Text.Trim()

cmd.Parameters.Add("@BirthDate", SqlDbType.DateTime).Value = txtBirthDate.Text.Trim()

cmd.Parameters.Add("@City", SqlDbType.VarChar).Value = txtCity.Text.Trim()

cmd.Parameters.Add("@Country", SqlDbType.VarChar).Value = txtCountry.Text.Trim()

cmd.Connection = con

Try

    con.Open()

    Dim obj As Object = cmd.ExecuteScalar()

    lblMessage.Text = "Record inserted successfully. ID = " & obj.ToString()

 Catch ex As Exception

    Throw ex

Finally 

    con.Close()

    con.Dispose()

End Try

OK i'm just wondering since the UniquieID will be carried over to multiple tables i only need the 1 stored procedure from the main primary key table? And in that procedure i only need to reference @ID and not (@firstname etc...)

Also in the vb.net code the parameters are of the whole table, in this instance can i just simply have the parameter of the identity?

Thanks for your help

Viewing all articles
Browse latest Browse all 2918

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>