Sql server 我的存储过程有什么问题
我必须使用动态SQL解决方案创建一个存储过程,因为我需要包含几个嵌套if条件。在查询分析器中运行代码时,我收到以下错误Sql server 我的存储过程有什么问题,sql-server,stored-procedures,Sql Server,Stored Procedures,我必须使用动态SQL解决方案创建一个存储过程,因为我需要包含几个嵌套if条件。在查询分析器中运行代码时,我收到以下错误 Msg 156, Level 15, State 1, Line 13 Incorrect syntax near the keyword 'Procedure'. 我尝试创建的过程如下所示: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF EXISTS (SELECT * FROM sys.objects WHERE
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'Procedure'.
我尝试创建的过程如下所示:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[sp_Payments]')
AND type in (N'P', N'PC'))
Drop procedure sp_Payments
BEGIN
DECLARE @SQL varchar(max),
@categoryID smallint,
@startDate datetime,
@stopDate datetime,
@debtCode tinyint,
@izEscrow tinyint
Create Procedure sp_Payments
(@categoryID smallint,
@startDate datetime,
@stopDate datetime,
@debtCode tinyint,
@izEscrow tinyint
)
AS
BEGIN
Declare @Payments table(
paydate datetime,
principaldue float,
interestdue float,
debtid int,
debtname varchar(50),
debtnumber varchar(10),
fsrc varchar(40),
category varchar(40),
PayMonth tinyint,
PayYear int
)
SET @SQL = '
insert into @Payments
select dtl.paydate,
dtl.principaldue,
dtl.interestdue,
dtl.debtid,
dmf.debtname,
dmf.debtnumber,
fsrc.fsrc,
app.category,
month(dtl.paydate) as PayMonth,
case
when month(PayDate) <= 6 then year(PayDate)
else year(PayDate)+1
end "PayYear"
from debtdetail dtl
inner join masterfile dmf
on dtl.debtid = dmf.debtid
inner join categories app
on dmf.categoryid = app.categoryid
left outer join fsrc
on dmf.fsrcid = fsrc.fsrcid
left outer join debtissues di
on dmf.issueid = di.issueid
where dtl.debtid in
(select debtid from masterfile
where categoryid = @categoryID '
IF @debtCode > 0
SET @SQL = @SQL + '
AND codeid = @debtCode
'
SET @SQL = @SQL + '
)
AND di.iscontingent = 0
'
IF @stopDate = ''
SET @SQL = @SQL + '
and dtl.paydate >= @startDate
'
ELSE
SET @SQL = @SQL + '
and dtl.paydate between
@startDate AND @stopDate
'
IF @izEscrow = 0
SET @SQL = @SQL + '
and dtl.isescrow = 0
'
SET @SQL = @SQL + '
and (principaldue + interestdue) > 0 and dtl.active = 1
order by dtl.Paydate, dmf.DebtNumber '
EXEC @SQL
END
SELECT * from @Payments
RETURN
END
任何帮助都将不胜感激。提前感谢摆脱动态sql,然后执行以下操作:
AND (@debtCode = 0 OR codeId = @debtCode)
... and so on
这里的批之间需要GOs。还有其他问题。这是一个非常落后的设计。为什么要声明所有变量,然后创建具有相同名称的参数?查询分析器?你是说管理工作室吗?如果您在SQL Server 2000上运行此代码,则有许多事情会阻止它工作。请指定您正在使用的SQL Server版本。不够具体。JNK,我对我的存储过程不是很精通。谢谢你的意见。我知道查询在作为冷聚变组件的一部分调用时可以工作,但我们希望转移到存储过程进行优化。Aaron,我使用的是Management Studio Express,我的db版本是9.0.5057,我认为是mssql 2000。不,9.0.xxxx是SQL Server 2005。对于专业开发人员的网站来说,可能不适合使用这种语言。