我的代码在执行应用程序时遇到错误有什么问题
Msg 156,Level 15,State 1,Line 11关键字附近的语法不正确 '和'
这是我的代码。任何帮助将不胜感激。
ALTER PROCEDURE [dbo].[Usp_ReportList]
@pAccountType varchar(35)=null,
@pFromDate datetime=null,
@pToDate datetime=null,
@pAccountId int=null,
@pUserId int=null,
@pTeamId int=null
AS
BEGIN
SET NOCOUNT ON;
Declare @strSQL AS NVarchar(4000)
SET @strSQL ='
SELECT
OrderInfoId,
BorrowerFirstName,
BorrowerLastName,
RequestedURL,
Requests,
CustomerUserID As LenderId ,
o.RequestIPAddress As OriginatingIPAddress,
o.Requests As Status
from orderInfo o '
if(@pAccountType = 'Lender')
BEGIN
SET @strSQL += 'inner join [User] u on o.CustomerUserId = u.UID where 1=1'
END
else if(@pAccountType = 'Affiliate')
BEGIN
SET @strSQL += 'inner join [User] u on o.AffiliateID = u.UID where 1=1'
END
if(@pFromDate != '')
BEGIN
SET @strSQL += ' and o.RequestDateTime >= ''' + CONVERT(VARCHAR(25),@pFromDate) + ''''
END
if(@pToDate != '')
BEGIN
SET @strSQL += ' and o.RequestDateTime <= ''' + CONVERT(VARCHAR(25),@pToDate) + ''''
END
if(@pAccountId != '')
BEGIN
SET @strSQL += ' and u.UID in ( select UID from [User] where AccountID = ' + CONVERT(VARCHAR(10),@pAccountId) + ') '
END
if(@pUserID !='')
BEGIN
SET @strSQL += ' and u.UserId = ' + CONVERT(VARCHAR(10),@pUserId) + ' '
END
if(@pTeamId !='')
BEGIN
SET @strSQL += ' and u.TeamId = ' + CONVERT(VARCHAR(10),@pTeamId) + ' '
END
SET @strSQL += '
GROUP BY
OrderInfoId,
BorrowerFirstName,
BorrowerLastName,
RequestedURL,
Requests,
CustomerUserID,
o.RequestIPAddress,
o.Requests'
EXEC (@strSQL)
--PRINT (@strSQL)
END
Exec Usp_ReportList 'Lender','2015-06-01 00:00:00','2015-06-02 00:00:00','2','1'
这是我的个人资料生成的查询
exec sp_executesql N'Exec Usp_ReportList @pAccountType,@pFromDate,@pToDate,@pAccountId,@pUserId,@pTeamId',N'@pAccountType nvarchar(1),@pFromDate datetime,@pToDate datetime,@pAccountId int,@pUserId int,@pTeamId int',@pAccountType=N'1',@pFromDate='2015-06-01 00:00:00',@pToDate='2015-06-02 00:00:00',@pAccountId=1,@pUserId=2,@pTeamId=2
收到错误
Msg 156,Level 15,State 1,Line 11关键字附近的语法不正确 &#39;和&#39;
无法弄清楚实际问题在哪里
使用PRINT(@strSQL)生成的语句
SELECT
OrderInfoId,
BorrowerFirstName,
BorrowerLastName,
RequestedURL,
Requests,
CustomerUserID As LenderId ,
o.RequestIPAddress As OriginatingIPAddress,
o.Requests As Status
from orderInfo o and o.RequestDateTime >= 'Jun 1 2015 12:00AM' and o.RequestDateTime <= 'Jun 1 2015 12:00AM' and u.UID in ( select UID from [User] where AccountID = 2) and u.UserId = 5 and u.TeamId = 5
GROUP BY
OrderInfoId,
BorrowerFirstName,
BorrowerLastName,
RequestedURL,
Requests,
CustomerUserID,
o.RequestIPAddress,
o.Requests
答案 0 :(得分:2)
我认为您希望将参数附加到查询中,如下所示。同样对于日期和字符串,您需要将它们放在单引号中,例如'2014-01-01'
IF(@pFromDate != '')
BEGIN
SET @strSQL += ' and o.RequestDateTime >= ''' + @pFromDate + ''''
END
注意:我不肯定我的报价是正确的,以确保您的日期在单引号内。在执行之前打印出您的SQL语句以确保它看起来正确。