我刚接触SQL编程。我正在尝试使用参数动态运行查询。
我可以在没有动态部分的情况下运行代码。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ALTER PROCEDURE [dbo].[spPurGrnPret]
-- (@cond int,@database NVARCHAR(MAX))
-- As
begin
----------------------Temporary Tables Droped---------------------
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPO' AND type = 'U')DROP TABLE #CPO
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPoNo' AND type = 'U')DROP TABLE #CPoNo
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPoGrn1' AND type = 'U')DROP TABLE #CPoGrn1
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPoNo' AND type = 'U')DROP TABLE #CPoNo
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPRet' AND type = 'U')DROP TABLE #CPRet
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPoGrn' AND type = 'U')DROP TABLE #CPoGrn
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CPoPnd' AND type = 'U')DROP TABLE #CPoPnd
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CTPoDel' AND type = 'U')DROP TABLE #CTPoDel
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#PurGrnPret' AND type = 'U')drop table #PurGrnPret
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#cPODet' AND type = 'U')drop table #cPODet
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#cRcvdGrnwise' AND type = 'U')drop table #cRcvdGrnwise
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#cTotRet' AND type = 'U')drop table #cTotRet
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CGRNPret' AND type = 'U')drop table #CGRNPret
IF EXISTS(SELECT name FROM sysobjects WHERE NAME='#CGRNPretSum' AND type = 'U')drop table #CGRNPretSum
DECLARE @database NVARCHAR(MAX)
SET @database = 'AAHOTX16'
DECLARE @SQL NVARCHAR(MAX)
declare @cond int
set @cond = 2
select @SQL = 'use ' + @database + ';
select ltrim(rtrim(Pur.yr))+''/''+ltrim(rtrim(str(Pur.po_no))) as PONO,convert(varchar(10), Pur.po_date, 103) as po_date,
Pur.vendor, convert(varchar(10), Pur.del_dt, 103) as del_dt, convert(varchar(10), Pur.remind_dt, 103) as remind_dt,
convert(varchar(10), Pur.cancel_dt, 103) as cancel_dt, pi.item_cat, pi.item_id, pi.SIZEZ, pi.color,
sum(pi.recd_qty - (pi.canc_qty * case when (pi.qty_conv >0 ) then pi.qty_conv else 1 End)) as qty,
pi.price as rate,Pur.l_currency,pi.item_unit as pur_unit,pi.ord_no as job_no, pi.fitem_id as style,
pi.item_cat+pi.item_id+pi.sizez+pi.color as item ,PUR.CMP
into #CPO from Pi inner join Pur on Pi.sl_no = Pur.sl_no
group by Pur.yr,Pur.po_no,Pur.po_date,Pur.vendor,Pur.del_dt,Pur.remind_dt, Pur.cancel_dt,pi.item_cat,pi.item_id, pi.SIZEZ,
pi.color,pi.ord_no,pi.fitem_id, pi.price,Pur.l_currency,pi.item_unit ,PUR.CMP
order by del_dt,pi.item_cat,pi.item_id,pi.SIZEZ,pi.color
select distinct pono into #CPoNo from #CPO where pono <> ''
select Grn.po_no, ltrim(rtrim(Grn.yr))+''/''+ltrim(rtrim(str(Grn.NOZ))) as grnno,
convert(varchar(10), Grn.DATEZ, 103) as Grndate,Grn.party_chln, Grn_c.item_cat, Grn_c.item_id,Grn_c.SIZEZ, Grn_c.color,
sum(Grn_c.receipt) as Grnqty,Grn_c.pur_unit,Grn_c.bill_no as grnbill,Grn_c.sl_no,grn_c.job_no,grn_c.style,
Grn_c.item_cat+Grn_c.item_id+Grn_c.sizez+Grn_c.color as item
INTO #CPoGrn1 from #CPoNo inner join Grn inner join Grn_c
on Grn.sl_no = Grn_c.sl_no on #CPoNo.pono = Grn.po_no
group by Grn_c.sl_no,Grn_c.item_cat,Grn_c.item_id,Grn_c.SIZEZ,Grn_c.color, grn_c.job_no,grn_c.style,Grn.PO_NO,
Grn.YR,Grn.NOZ,Grn.DATEZ,Grn.PARTY_CHLN,Grn_c.PUR_UNIT,Grn_c.BILL_NO
select ltrim(rtrim(Pret.yr))+''/''+ltrim(rtrim(str(Pret.NOZ))) as pretno, convert(varchar(10), Pret.DATEZ, 103) as Pretdate,
Pret.grn_no, Pret_c.item_cat, Pret_c.item_id, Pret_c.SIZEZ, Pret_c.color,
sum(Pret_c.issue) as pretqty,'' as pretbill,pret_c.job_no,pret_c.style,
Pret_c.item_cat+Pret_c.item_id+Pret_c.sizez+Pret_c.color as item
INTO #CPRet
from Pret inner join Pret_c
on Pret.sl_no = Pret_c.sl_no
where ltrim(rtrim(Pret.grn_no)) in (select distinct ltrim(rtrim(#CPoGrn1.grnno)) from #CPoGrn1)
group by Pret.yr,Pret.NOZ, Pret.DATEZ, Pret.grn_no, Pret_c.item_cat,
Pret_c.item_id, Pret_c.SIZEZ,pret_c.color,pret_c.job_no, pret_c.style
select #CPoGrn1.item_cat,#CPoGrn1.item_id,#CPoGrn1.SIZEZ,#CPoGrn1.color,po_no,grnno, grndate,party_chln,
sum(grnqty) as grnqty, pur_unit,grnbill, isnull(pretno,'')as pretno, isnull(pretdate,'') as pretdate,
sum(isnull(pretqty,0)) as pretqty,isnull(pretbill,'') as pretbill, #CPoGrn1.job_no,#CPoGrn1.style,#CPoGrn1.item ,#CPoGrn1.sl_no
INTO #CPoGrn from #CPoGrn1 left outer join #CPRet
on ltrim(rtrim(#CPoGrn1.grnno)) + #CPoGrn1.item + str(#CPoGrn1.job_no) + #CPoGrn1.style =
ltrim(rtrim(#CPRet.grn_no))+#CPRet.item+ str(#CPRet.job_no)+#CPRet.style
group by #CPoGrn1.po_no,#CPoGrn1.item_cat, #CPoGrn1.item_id, #CPoGrn1.SIZEZ,po_no,grnno,grndate,party_chln,grnqty,pur_unit,grnbill,
#CPoGrn1.sl_no,#CPoGrn1.color,#CPoGrn1.job_no,#CPoGrn1.style,#CPoGrn1.item ,pretno,pretdate,pretbill
select #CPO.*,isnull(#CPoGrn.grnno,'') as grnno, isnull(#CPoGrn.grndate,'') as grndate,
isnull(#CPoGrn.party_chln,'') as party_chln, isnull(grnqty,0) as grnqty, isnull(grnbill,'') as grnbill,
isnull(pretno,'') as pretno,isnull(pretdate,'') as pretdate, isnull(pretqty,0) as pretqty,
isnull(#CPoGrn.pretbill,'') as pretbill
into #CPoPnd from #CPO left outer join #CPoGrn
on ltrim(#CPO.pono)+ #CPO.item+str(#CPO.job_no) + #CPO.style =
ltrim(rtrim(#CPoGrn.po_no))+#CPoGrn.item + str(#CPoGrn.job_no)+#CPoGrn.style
select #CPoPnd.*,isnull(item.Item_name,'') as Item_name,
isnull(item.descript,'') as descript,ITEM.UNIT AS PUR_UNITS
INTO #CTPoDel from #CPoPnd left outer join item
on #CPoPnd.item_cat+#CPoPnd.item_id = item.item_cat + item.item_id
where (#CPoPnd.qty - (#CPoPnd.grnqty - #CPoPnd.pretqty)) > 0
order by del_dt,#CPoPnd.item_cat,vendor,pono,#CPoPnd.item_id,
#CPoPnd.SIZEZ, #CPoPnd.color
select DISTINCT #CTPoDel.*,sizemast.ord
into #PurGrnPret from #CTPoDel left outer join sizemast
on #CTPoDel.SIZEZ = sizemast.SIZEZ
order by ord
If (@cond =1)
Begin
select * from #PurGrnPret
end
Else
Begin
select PONO,po_date, vendor,del_dt ,remind_dt , cancel_dt,item_cat ,item_id , SIZEZ ,color ,l_currency ,pur_unit,
job_no ,Style ,item , Item_name, qty , rate, cmp
into #cPODet from #PurGrnPret
group by PONO,vendor,del_dt ,remind_dt , rate ,qty ,cancel_dt,item_cat ,item_id , SIZEZ ,color ,l_currency ,pur_unit,
job_no ,Style ,item , Item_name,cmp,po_date
order by del_dt, vendor, job_no, style
select PONO,grnno,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item, sum(grnqty) as totrcvd
into #cRcvdGrnwise from #PurGrnPret
group by PONO,grnno,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item
select grnno,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item, sum(pretqty) as totret
into #cTotRet from #PurGrnPret
group by grnno,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item
Select #cRcvdGrnwise.PONO,#cRcvdGrnwise.grnno,#cRcvdGrnwise.item_cat ,#cRcvdGrnwise.item_id , #cRcvdGrnwise.SIZEZ ,#cRcvdGrnwise.color,
#cRcvdGrnwise.job_no, #cRcvdGrnwise.Style ,#cRcvdGrnwise.item, #cRcvdGrnwise.totrcvd, #cTotRet.totret
into #CGRNPret from #cRcvdGrnwise inner join #cTotRet
on ltrim(#cRcvdGrnwise.grnno)+''+ltrim(#cRcvdGrnwise.item_cat)+''+ltrim(#cRcvdGrnwise.item_id)+''+ltrim(#cRcvdGrnwise.SIZEZ)+''+ltrim(#cRcvdGrnwise.color)+''+ltrim(#cRcvdGrnwise.job_no)+''+ltrim(#cRcvdGrnwise.Style) =
ltrim(#cTotRet.grnno)+''+ltrim(#cTotRet.item_cat)+''+ltrim(#cTotRet.item_id)+''+ltrim(#cTotRet.SIZEZ)+''+ltrim(#cTotRet.color)+''+ltrim(#cTotRet.job_no)+''+ltrim(#cTotRet.Style)
Select PONO,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item, sum(totrcvd) as totrcv, sum(totret) as totpret
into #CGRNPretSum from #CGRNPret
group by pono,item_cat ,item_id , SIZEZ ,color, job_no, Style ,item
Select #cPODet.*, totrcv, totpret
from #cPODet inner join #CGRNPretSum
on ltrim(#cPODet.pono)+''+ltrim(#cPODet.item_cat)+''+ltrim(#cPODet.item_id)+''+ltrim(#cPODet.SIZEZ)+''+ltrim(#cPODet.color)+''+ltrim(#cPODet.job_no)+''+ltrim(#cPODet.Style)+''+ltrim(#cPODet.item) =
ltrim(#CGRNPretSum.pono)+''+ltrim(#CGRNPretSum.item_cat)+''+ltrim(#CGRNPretSum.item_id)+''+ltrim(#CGRNPretSum.SIZEZ)+''+ltrim(#CGRNPretSum.color)+''+ltrim(#CGRNPretSum.job_no)+''+ltrim(#CGRNPretSum.Style)+''+ltrim(#CGRNPretSum.item)
end'
----------------------Temporary Tables Droped---------------------
EXEC sp_executesql @sql,
N' @cond INT , @database VARCHAR(10)',
@database, @cond
--exec Podel 'F','9/1/2013','9/30/2013','0'
END
GO
我得到这些错误:
第156条消息,第15级,状态1,第26行
关键字“ as”附近的语法不正确。第156条消息,级别15,状态1,第32行
关键字“ group”附近的语法不正确。
我不明白问题是什么,如何调试此错误?
任何帮助将不胜感激
预先感谢