473,757 Members | 8,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4025
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.co m> wrote in message
news:Oa******** ******@TK2MSFTN GP11.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.co m> wrote in message
news:Oa******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
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.co m> wrote in message
news:Oa******** ******@TK2MSFTN GP11.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.co m> wrote in message
news:et******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
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.co m> wrote in message
news:Oa******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
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.co m> wrote in message
news:et******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
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.co m> wrote in message
news:Oa******** ******@TK2MSFTN GP11.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.co m> wrote in message
news:ei******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
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.co m> wrote in message
news:et******** ******@TK2MSFTN GP11.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.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP12.phx.gb l...
> 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.co m> wrote in message
> news:Oa******** ******@TK2MSFTN GP11.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
22145
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 application server can talk to the database. I've determined the failure occurs when the the following statement is executed: cstmt.execute(); (due to the failure of println statements placed afterwards). I get the following error after trying to...
0
6703
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 Visual Basic .NET version of this article, see 308049. For a Microsoft Visual C++ .NET version of this article, see 310071. For a Microsoft Visual J# .NET version of this article, see 320627. This article refers to the following Microsoft .NET...
3
2805
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" when I launched the IBM Distributed Debugger via D:\IBMDebug>idebug.exe -qdaemon -quiport=8000,8001 First, a bit of background. I am running DB2 V7.2 with Fixpack 9 applied on Windows XP Professional (all critical service applied). I've written...
4
3190
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 Throwable as an OUT parameter and what datatype should I use in the CREATE PROCEDURE and DROP PROCEDURE statements? Here's what I tried: - the method signature for the stored procedure included: Throwable throwable
8
7944
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 created a temporary database with a tables space. Verified that DECLARE GLOBAL TEMPORARY TABLE TEMP (A INTEGER); INSERT INTO SESSION.TEMP VALUES(10); SELECT A FROM SESSION.TEMP; works from a query tool.
2
5458
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
2653
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 to call them from an ASP.Net page Every modern database system has a stored procedure language. SQL Server is no different and has a relatively sophisticated and easy to use system. This article will not attempt to go into depth in explaining...
3
3476
by: kd | last post by:
Hi All, How to debug a stored procedure? Thanks, kd
7
3469
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 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
2
4108
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
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.