Click to See Complete Forum and Search --> : stored procedure


kohlimannu
January 4th, 2006, 06:11 AM
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

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)

exterminator
January 4th, 2006, 07:30 AM
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 (http://www.sqlteam.com/item.asp?ItemID=8003). And to get the last value that the identity column got you just do a @@IDENTITY (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_globals_50u1.asp) and you get the value. No need to do a select max(..).... Hope this helps. Regards.

dinesh123
January 4th, 2006, 07:38 AM
Or else

You can use a trigger to update the second table from inseted table

exterminator
January 4th, 2006, 07:55 AM
Or else

You can use a trigger to update the second table from inseted tableWhat do you mean? The OP only talks of one table. Regards.

erickwidya
January 4th, 2006, 09:32 PM
..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.

kohlimannu
January 4th, 2006, 11:42 PM
Thanx alot all of you for the reply's.