Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Returning values from SQL Server
Message
De
03/08/2004 10:03:08
 
 
À
03/08/2004 09:36:30
Jim Rieck
Quicken Loans/Rock Financial/Title Sourc
Livonia, Michigan, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Bases de données
Divers
Thread ID:
00930321
Message ID:
00930339
Vues:
23
Jim,

You need to use OUTPUT parameters in your SP. Is your auto-incrementing field your PK? If so, your SP would look like this:
ALTER PROCEDURE InsertTaskHeader
(
	@task_no int,
	@project_id int,
	@lkup_id int,
	@descrip text,
	@start_date datetime,
	@status_id int,
	@userid char(10),
        @taskhdr_id OUTPUT
)
AS
INSERT INTO task_header(task_no, project_id, lkup_id, descrip, start_date, status_id, timestamp_created, userid_created)
VALUES (@task_no, @project_id, @lkup_id, @descrip, @start_date, @status_id, getdate(), @userid)

SELECT @taskhdr_id = SCOPE_IDENTITY()

   RETURN
and in your code, to call the SP, you'd need to set the parameter's direction, like this:
'Webform calling the stored procedure
    Private Sub setInsertTaskHeaderParameters()
        With Me.daTaskHeader.InsertCommand
            Me.getMaxTaskNo()
            .Parameters("@task_no").Value = txtTaskno.Text
            .Parameters("@project_id").Value =  cboProjects.SelectedItem.Value
            .Parameters("@lkup_id").Value = daStatus.SelectCommand.Parameters("@lkup_id").Value
            .Parameters("@descrip").Value = Me.txtDescrip.Text
            .Parameters("@start_date").Value = CType(Me.txtStartDate.Text, DateTime)
            .Parameters("@status_id").Value = Me.cboStatus.SelectedItem.Value
            .Parameters("@userid").Value = Session("username")

            ' I think I got the syntax right here
            .Parameters.Add("@taskhdr_id")
            .Parameters.Direction = ParameterDirection.InputOutput;
        End With
    End Sub
HTH,
~~Bonnie


>All,
>
> I have a stored procedure that I am calling from a webform. The stored procedure is updating a table called Task_header. The task_header table has a automatically incramenting field called taskhdr_id. I want the stored procedure to return the value of the taskhdr_id field to my webform when a record is inserted. How do I go about doing this? The code that I have is below.
>
>
>'Webform calling the stored procedure
>    Private Sub setInsertTaskHeaderParameters()
>        With Me.daTaskHeader.InsertCommand
>            Me.getMaxTaskNo()
>            .Parameters("@task_no").Value = txtTaskno.Text
>            .Parameters("@project_id").Value =  cboProjects.SelectedItem.Value
>            .Parameters("@lkup_id").Value = daStatus.SelectCommand.Parameters("@lkup_id").Value
>            .Parameters("@descrip").Value = Me.txtDescrip.Text
>            .Parameters("@start_date").Value = CType(Me.txtStartDate.Text, DateTime)
>            .Parameters("@status_id").Value = Me.cboStatus.SelectedItem.Value
>            .Parameters("@userid").Value = Session("username")
>        End With
>    End Sub
>
>'Stored Procedure
>ALTER PROCEDURE InsertTaskHeader
>(
>	@task_no int,
>	@project_id int,
>	@lkup_id int,
>	@descrip text,
>	@start_date datetime,
>	@status_id int,
>	@userid char(10)
>)
>AS
>INSERT INTO task_header(task_no, project_id, lkup_id, descrip, start_date, status_id, timestamp_created, userid_created)
>VALUES (@task_no, @project_id, @lkup_id, @descrip, @start_date, @status_id, getdate(), @userid)
>   RETURN
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform