-
January 4th, 2006, 06:11 AM
#1
stored procedure
Hi
i need to insert a record from my asp.net frontend using stored procedure in Sql server database.
there is a table called Customer
fields are customerID,CustomerName
now through stored procedure i need to insert customername and customerID is must be autogenerated
like
CustomerID CustomerName
1 ABC
2 DEF
3 XYZ
is there any way to do this
iwas trying to do like this
Code:
create proc trest
(@fname varchar(50)="x",
@x int)
as
select max(id) as nid from register
set x=nid+1
insert into login (id,firstname)values(@x,@fname)
Keep Posting in this Forum
-
January 4th, 2006, 07:30 AM
#2
Re: stored procedure
Make the customer ID column an identity column. And now you would not need to worry about this ID and just need to insert the records with customer name. Here is a link that explains the stuff - Understanding the Identity columns. And to get the last value that the identity column got you just do a @@IDENTITY and you get the value. No need to do a select max(..).... Hope this helps. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 4th, 2006, 07:38 AM
#3
Re: stored procedure
Or else
You can use a trigger to update the second table from inseted table
-
January 4th, 2006, 07:55 AM
#4
Re: stored procedure
Originally Posted by dinesh123
Or else
You can use a trigger to update the second table from inseted table
What do you mean? The OP only talks of one table. Regards.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 4th, 2006, 09:32 PM
#5
Re: stored procedure
..And to get the last value that the identity column got you just do a @@IDENTITY and you get the value.
or use SCOPE_IDENTITY()
from BOL
Returns the last IDENTITY value inserted into an IDENTITY column in the same scope. A scope is a module -- a stored procedure, trigger, function, or batch. Thus, two statements are in the same scope if they are in the same stored procedure, function, or batch.
-
January 4th, 2006, 11:42 PM
#6
Re: stored procedure
Thanx alot all of you for the reply's.
Keep Posting in this Forum
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|