Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi i want to insert data on one button click in multiple tables

i have tables like

1) custdetails
custid,custname,custadd,proid


2)proddetails
proid,prodname,price

3)branddetails
proid,brandname

now i want to insert in this three table
in using a storeprocedure like


SQL
Create PROCEDURE [dbo].[InsertDetails_SP]
(
                @custid varchar(50),
                   @custname varchar(50),
                   @custadd varchar(200),
              @proid int identity (1,1) ,
               @prodname varchar (50),
               @price int ,
               @brandname varchar (100) ,
               
)
as
begin
set nocount on;
begin transaction
insert into custdetails
(
custid,custname,custadd,proid
)
values
(
@custid,@custname,@custadd,proid
)
Declare @proid int 
set @proid=SCOPE_IDENTITY()
If @@ERROR=0
Begin
insert into proddetails
(
proid,prodname,price
)
values
(
@proid,@prodname,@price
)
end
if @@Error=0
begin
insert into branddetails(
proid,brandname
)
values
(
@proid,@brandname
)
IF @@ERROR = 0
				BEGIN			 
					COMMIT TRANSACTION
				END
			ELSE
				BEGIN			 
					ROLLBACK TRANSACTION		
				END
		END
ELSE
		BEGIN
			ROLLBACK TRANSACTION
		END
 
END


But am getting error that Must declare scalar variable for @brandname


please some one could you help me resolve this error...
Posted
Updated 24-Jun-15 3:04am
v3
Comments
ZurdoDev 24-Jun-15 7:41am    
You don't declare @brandname anywhere which is why you get that error.
jaket-cp 24-Jun-15 8:20am    
remove the comma after @brandname varchar (100)
see if that works.
KansChweety 24-Jun-15 8:31am    
still have the same error must declare the scalar var fr brandname ..bt i declared above
Sreekanth Mothukuru 24-Jun-15 8:55am    
Is this your actual running stored procedure? I see there are lot of syntax errors.

1 solution

Hi,

I see several errors Fixed the error and here is the code.
few errors I see
- int identity passing as input parameter
- comma after the last input parameter
- proid in insert statement ( no @)
- declaration of @proid multiple times

You can now charge the sp without any issue, but you need to look into the scrip that you have written everything correctly.

check the line - set @proid=SCOPE_IDENTITY()
looks incorrect


SQL
Create PROCEDURE [dbo].[InsertDetails_SP]
(
              @custid varchar(50),
              @custname varchar(50),
              @custadd varchar(200),
              @proid int,
              @prodname varchar (50),
              @price int ,
              @brandname varchar (100)
)
as
begin
set nocount on;
begin transaction
insert into custdetails
(
custid,custname,custadd,proid
)
values
(
@custid,@custname,@custadd,@proid
)
--Declare @proid int 
set @proid=SCOPE_IDENTITY()
If @@ERROR=0
Begin
insert into proddetails
(
proid,prodname,price
)
values
(
@proid,@prodname,@price
)
end
if @@Error=0
begin
insert into branddetails(
proid,brandname
)
values
(
@proid,@brandname
)
IF @@ERROR = 0
				BEGIN			 
					COMMIT TRANSACTION
				END
			ELSE
				BEGIN			 
					ROLLBACK TRANSACTION		
				END
		END
ELSE
		BEGIN
			ROLLBACK TRANSACTION
		END
 
END


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900