
This is for an Idea...!
Change the code as you required
create table Person
(
p_id int IDENTITY(1,1) not null primary key,
Fname varchar(50),
Lname varchar(50),
address varchar(50)
)
go
create table orders
(
o_id int IDENTITY(1,1) not null primary key,
oname varchar(50),
Qty int,
p_id int foreign key references Person(p_id),
prize Int
)
go
Create Proc Usp_Orders_Insert
(
@Param_Fname Varchar(50),
@Param_Lname Varchar(50),
@Param_Address Varchar(50),
@Param_oname Varchar(50),
@Param_Qty Int,
@Param_prize Int
)
As
Begin
Set Nocount On
Declare @PersonID Int
Begin Try
Begin Tran
Insert Person(Fname, Lname, [address]) Values(@Param_Fname, @Param_Lname, @Param_Address)
Select @PersonID = @@Identity
Insert orders(oname, Qty, p_id, prize) Values(@Param_oname, @Param_Qty, @PersonID, @Param_prize)
Commit
End Try
Begin Catch
Declare @Message Varchar(Max)
Select @Message = 'Error : ' + ERROR_MESSAGE() + CHAR(10) + 'Line No: '+ Cast(ERROR_LINE() as Varchar(10))
Raiserror(@Message,16,1)
Rollback
End Catch
End
Go
Exec Usp_Orders_Insert 'Gow','Net','Chennai','Notbook',2,100
Go
Note:
- "Person.p_id" and "orders.o_id" as an IDENTITY
(or)
Change the procedure as you required!
Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions
Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator