具有动态查询的存储过程 "Error"
Stored procedure with dynamic query "Error"
我的代码在执行应用程序时出错是怎么回事
Msg 156, Level 15, State 1, Line 11 Incorrect syntax near the keyword
'and'.
这是我的代码。任何帮助将不胜感激。
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 Incorrect syntax near the keyword
'and'.
想不通实际问题出在哪里
使用 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
我认为您想将参数附加到查询中,如下所示。同样对于日期和字符串,您需要将它们放在单引号内,例如 '2014-01-01'
IF(@pFromDate != '')
BEGIN
SET @strSQL += ' and o.RequestDateTime >= ''' + @pFromDate + ''''
END
注意:我不确定我的报价是否正确,以确保您的日期在单引号内。打印出您的 SQL 语句以确保它在执行前看起来正确。
我的代码在执行应用程序时出错是怎么回事
Msg 156, Level 15, State 1, Line 11 Incorrect syntax near the keyword 'and'.
这是我的代码。任何帮助将不胜感激。
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 Incorrect syntax near the keyword 'and'.
想不通实际问题出在哪里
使用 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
我认为您想将参数附加到查询中,如下所示。同样对于日期和字符串,您需要将它们放在单引号内,例如 '2014-01-01'
IF(@pFromDate != '')
BEGIN
SET @strSQL += ' and o.RequestDateTime >= ''' + @pFromDate + ''''
END
注意:我不确定我的报价是否正确,以确保您的日期在单引号内。打印出您的 SQL 语句以确保它在执行前看起来正确。