I have 3 tables. Please check my below code. I want to insert @OP in OP of HSR_OP_Bal. @OP is a temporary column for IF Else output. Im getting following errors:
Incorrect syntax near '@OP'
Incorrect syntax near the keyword 'end'.
.
Stored procedure:
<pre>USE [Shoper9HO]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROC [dbo].[HSR]
AS
BEGIN
Declare @ShowroomCode varchar(50),
@Startdate date,
@Qty int,
@StockNo varchar(50),
@TransType int,
@Servicetag varchar(50)
BEGIN
DECLARE HSRCursor Cursor
STATIC FOR
select distinct a.Showroomcode,a.Stockno,Startdate,Servicetag, Qty ,Transtype
from ConsIMEItrndata a,
(select Showroomcode,startdate,stockno,OpeningQty from IMTable) b
where a.Showroomcode=b.Showroomcode
and a.Stockno=b.Stockno
OPEN HSRCursor
IF @@CURSOR_ROWS > 0
BEGIN
FETCH NEXT FROM HSRCur INTO @Showroomcode,@Stockno,@Startdate,@Servicetag,@Qty,@Transtype
WHILE @@FETCH_STATUS = 0
BEGIN
If ( @TransType=1100 or @Transtype=1300)
Declare @OP int
Begin
@OP=Qty*1
end
Begin
else @OP=Qty* -1
end
Insert into HSR_OP_Bal(Showroomcode,Stockno,Startdate,Servicetag,OP) values(@Showroomcode,@Stockno,@Startdate,@Servicetag,@OP)
end
end
CLOSE HSRCursor
DEALLOCATE HSRCursor
end
end
Please help with above error .
Also I want to write this below query to be included inside procedure as final output,please write complete procedure.
<pre>Select Showroomcode,Stockno,Startdate,Modelno,Servicetag,Sum( OP) as OpeningBal
from HSR_OP_Bal
where Startdate= Frontend selecteddate
Group by Showroomcode,Stockno,Startdate,Modelno,Servicetag
Having Sum(OP)>0
Thanku..