hi! I want to create a procedure which execute given below query using dynamic sql. i am getting some error i.e. "Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'GROUP'."
my query which i want to run by procedure is:
;with cte as (select *,t.opening+Recieve+returnback-Issue as balance from(SELECT p.trandate,p.voucherno,p.itemno,p.itemname,isnull(O.opening,0) opening,SUM(isnull(p.recieve,0))over(partition by p.itemno order by trandate ) Recieve,SUM(isnull(p.issue,0))over(partition by p.itemno order by trandate ) Issue,isnull(p.returnback,0) Returnback,ROW_NUMBER() OVER (Partition BY p.itemno Order by p.itemno,p.trandate) Row_Num ,O.opening as openingoriginal FROM (SELECT p.trandate,p.voucherno,p.itemno,p.itemname, SUM(isnull(p.recieve,0)) Recieve,SUM(isnull(p.issue,0)) Issue,SUM(isnull(p.returnback,0)) Returnback FROM ledgertable p GROUP BY p.itemno,p.ITEMNAME,p.trandate,p.voucherno) p LEFT JOIN openingstock20172018 O ON O.itemno = p.itemno) t)select a.trandate,a.voucherno,a.itemno,a.itemname,case when b.balance is null then a.openingoriginal else b.balance end as opening,c.recieve,c.issue,c.returnback,a.balance from cte a left join cte b on a.itemno=b.itemno and a.Row_Num=b.Row_Num+1 join (SELECT p.trandate,p.voucherno,p.itemno,p.itemname, SUM(isnull(p.recieve,0)) Recieve,SUM(isnull(p.issue,0)) Issue,SUM(isnull(p.returnback,0)) Returnback FROM ledgertable p GROUP BY p.itemno,p.ITEMNAME,p.trandate,p.voucherno) c on c.itemno=a.itemno and a.trandate=c.trandate
and procedure which i have created is given below:
CREATE PROCEDURE GetLedger(@optb as varchar(50))
AS
BEGIN
declare @openingtable as varchar(1000)
declare @query varchar(max)
set @openingtable=@optb
set @query=N';with cte as (select *,t.opening+Recieve+returnback-Issue as balance from(SELECT p.trandate,p.voucherno,p.itemno,p.itemname,isnull(O.opening,0) opening,SUM(isnull(p.recieve,0))over(partition by p.itemno order by trandate ) Recieve,SUM(isnull(p.issue,0))over(partition by p.itemno order by trandate ) Issue,isnull(p.returnback,0) Returnback,ROW_NUMBER() OVER (Partition BY p.itemno Order by p.itemno,p.trandate) Row_Num ,O.opening as openingoriginal FROM (SELECT p.trandate,p.voucherno,p.itemno,p.itemname, SUM(isnull(p.recieve,0)) Recieve,SUM(isnull(p.issue,0)) Issue,SUM(isnull(p.returnback,0)) Returnback FROM ledgertable p where trandate between GROUP BY p.itemno,p.ITEMNAME,p.trandate,p.voucherno) p LEFT JOIN'+@openingtable+'O ON O.itemno = p.itemno) t)select a.trandate,a.voucherno,a.itemno,a.itemname,case when b.balance is null then a.openingoriginal else b.balance end as opening,c.recieve,c.issue,c.returnback,a.balance from cte a left join cte b on a.itemno=b.itemno and a.Row_Num=b.Row_Num+1 join (SELECT p.trandate,p.voucherno,p.itemno,p.itemname, SUM(isnull(p.recieve,0)) Recieve,SUM(isnull(p.issue,0)) Issue,SUM(isnull(p.returnback,0)) Returnback FROM ledgertable p GROUP BY p.itemno,p.ITEMNAME,p.trandate,p.voucherno) c on c.itemno=a.itemno and a.trandate=c.trandate'
execute(@query)
END
kindly help me out.