473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stored Procedure

How do I implement a stored procedure to insert a new member in a database
then return the primary key of that member back to the application to be use
in another table?
Nov 17 '05 #1
6 4003
You could use this SQL code:

SET NOCOUNT ON;
BEGIN TRAN;
INSERT...;
SELECT @@identity AS newID;
COMMIT TRAN;
SET NOCOUNT OFF;

/john

"Leon Shaw" <vn*****@msn.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
How do I implement a stored procedure to insert a new member in a database
then return the primary key of that member back to the application to be use in another table?

Nov 17 '05 #2
Have your SQL Server query Select @@Identity after it does the insert.
Then you can get the value back like this:
newId = (int)MyCommand.ExecuteScalar();

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Leon Shaw" <vn*****@msn.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
How do I implement a stored procedure to insert a new member in a database
then return the primary key of that member back to the application to be use in another table?

Nov 17 '05 #3
Will This Work? And how do I get this MemberId in a variable in code and
pass it to another table?
CREATE PROCEDURE Add_Member
(
@FirstName varchar(50)
@LastName varchar(50)
@etc varchar(50)
)
AS
INSERT INTO Member
(
FirstName,
LastName,
etc
)
VALUES
(
@FirstName
@LastName
@etc
)
SELECT @MemberID = @@IDENTITY

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Have your SQL Server query Select @@Identity after it does the insert.
Then you can get the value back like this:
newId = (int)MyCommand.ExecuteScalar();

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Leon Shaw" <vn*****@msn.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
How do I implement a stored procedure to insert a new member in a database then return the primary key of that member back to the application to be

use
in another table?


Nov 17 '05 #4
No, don't select the identity value into a private stored procedure variable
or you'll never be able to get to it.
Instead the final line should look like this:
SELECT @@IDENTITY

Alternately you could define @MemberID as an output parameter for your
sproc. Then use ADO.NET parameter objects. After you execute the query,
check that parameter and it should be filled with the identity value.
Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp
http://msdn.microsoft.com/library/de...isualbasic.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net

"Leon Shaw" <vn*****@msn.com> wrote in message
news:et**************@TK2MSFTNGP11.phx.gbl...
Will This Work? And how do I get this MemberId in a variable in code and
pass it to another table?
CREATE PROCEDURE Add_Member
(
@FirstName varchar(50)
@LastName varchar(50)
@etc varchar(50)
)
AS
INSERT INTO Member
(
FirstName,
LastName,
etc
)
VALUES
(
@FirstName
@LastName
@etc
)
SELECT @MemberID = @@IDENTITY

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Have your SQL Server query Select @@Identity after it does the insert.
Then you can get the value back like this:
newId = (int)MyCommand.ExecuteScalar();

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Leon Shaw" <vn*****@msn.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
How do I implement a stored procedure to insert a new member in a database then return the primary key of that member back to the application to
be use
in another table?



Nov 17 '05 #5
So how do I get that identity column inside a variable to be pass to another
table that reference that member?
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
No, don't select the identity value into a private stored procedure variable or you'll never be able to get to it.
Instead the final line should look like this:
SELECT @@IDENTITY

Alternately you could define @MemberID as an output parameter for your
sproc. Then use ADO.NET parameter objects. After you execute the query,
check that parameter and it should be filled with the identity value.
Here's more info:
http://msdn.microsoft.com/library/de...classtopic.asp http://msdn.microsoft.com/library/de...isualbasic.asp
--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net

"Leon Shaw" <vn*****@msn.com> wrote in message
news:et**************@TK2MSFTNGP11.phx.gbl...
Will This Work? And how do I get this MemberId in a variable in code and
pass it to another table?
CREATE PROCEDURE Add_Member
(
@FirstName varchar(50)
@LastName varchar(50)
@etc varchar(50)
)
AS
INSERT INTO Member
(
FirstName,
LastName,
etc
)
VALUES
(
@FirstName
@LastName
@etc
)
SELECT @MemberID = @@IDENTITY

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Have your SQL Server query Select @@Identity after it does the insert.
Then you can get the value back like this:
newId = (int)MyCommand.ExecuteScalar();

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Leon Shaw" <vn*****@msn.com> wrote in message
news:Oa**************@TK2MSFTNGP11.phx.gbl...
> How do I implement a stored procedure to insert a new member in a database
> then return the primary key of that member back to the application
to be use
> in another table?
>
>



Nov 17 '05 #6
In the example I gave the identity of the record you just inserted will be
in the newId variable. Then you can pass it wherever you want, perhaps
putting it into a SQLParameter object to call some other query.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
"Leon Shaw" <vn*****@msn.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
So how do I get that identity column inside a variable to be pass to another table that reference that member?
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
No, don't select the identity value into a private stored procedure

variable
or you'll never be able to get to it.
Instead the final line should look like this:
SELECT @@IDENTITY

Alternately you could define @MemberID as an output parameter for your
sproc. Then use ADO.NET parameter objects. After you execute the query, check that parameter and it should be filled with the identity value.
Here's more info:

http://msdn.microsoft.com/library/de...classtopic.asp

http://msdn.microsoft.com/library/de...isualbasic.asp

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net

"Leon Shaw" <vn*****@msn.com> wrote in message
news:et**************@TK2MSFTNGP11.phx.gbl...
Will This Work? And how do I get this MemberId in a variable in code and pass it to another table?
CREATE PROCEDURE Add_Member
(
@FirstName varchar(50)
@LastName varchar(50)
@etc varchar(50)
)
AS
INSERT INTO Member
(
FirstName,
LastName,
etc
)
VALUES
(
@FirstName
@LastName
@etc
)
SELECT @MemberID = @@IDENTITY

"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
> Have your SQL Server query Select @@Identity after it does the insert. > Then you can get the value back like this:
> newId = (int)MyCommand.ExecuteScalar();
>
> --
> I hope this helps,
> Steve C. Orr, MCSD
> http://Steve.Orr.net
>
>
> "Leon Shaw" <vn*****@msn.com> wrote in message
> news:Oa**************@TK2MSFTNGP11.phx.gbl...
> > How do I implement a stored procedure to insert a new member in a
database
> > then return the primary key of that member back to the application

to
be
> use
> > in another table?
> >
> >
>
>



Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
3
by: kd | last post by:
Hi All, How to debug a stored procedure? Thanks, kd
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.