sql server - Stored procedure with dynamic query "Error" -
what wrong code getting error while executing application
msg 156, level 15, state 1, line 11 incorrect syntax near keyword 'and'.
and here code. appreciated.
alter procedure [dbo].[usp_reportlist] @paccounttype varchar(35)=null, @pfromdate datetime=null, @ptodate datetime=null, @paccountid int=null, @puserid int=null, @pteamid int=null begin set nocount on; declare @strsql nvarchar(4000) set @strsql =' select orderinfoid, borrowerfirstname, borrowerlastname, requestedurl, requests, customeruserid lenderid , o.requestipaddress originatingipaddress, o.requests status orderinfo o ' if(@paccounttype = 'lender') begin set @strsql += 'inner join [user] u on o.customeruserid = u.uid 1=1' end else if(@paccounttype = 'affiliate') begin set @strsql += 'inner join [user] u on o.affiliateid = u.uid 1=1' end if(@pfromdate != '') begin set @strsql += ' , o.requestdatetime >= ''' + convert(varchar(25),@pfromdate) + '''' end if(@ptodate != '') begin set @strsql += ' , o.requestdatetime <= ''' + convert(varchar(25),@ptodate) + '''' end if(@paccountid != '') begin set @strsql += ' , u.uid in ( select uid [user] accountid = ' + convert(varchar(10),@paccountid) + ') ' end if(@puserid !='') begin set @strsql += ' , u.userid = ' + convert(varchar(10),@puserid) + ' ' end if(@pteamid !='') begin set @strsql += ' , u.teamid = ' + convert(varchar(10),@pteamid) + ' ' end set @strsql += ' group 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'
here's profilers genereated query
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
getting error
msg 156, level 15, state 1, line 11 incorrect syntax near keyword 'and'.
can not able figure out actual problem
the generated statement using print (@strsql)
select orderinfoid, borrowerfirstname, borrowerlastname, requestedurl, requests, customeruserid lenderid , o.requestipaddress originatingipaddress, o.requests status orderinfo o , o.requestdatetime >= 'jun 1 2015 12:00am' , o.requestdatetime <= 'jun 1 2015 12:00am' , u.uid in ( select uid [user] accountid = 2) , u.userid = 5 , u.teamid = 5 group orderinfoid, borrowerfirstname, borrowerlastname, requestedurl, requests, customeruserid, o.requestipaddress, o.requests
i think want append parameters query, below. dates , strings need put them inside single quotes '2014-01-01'
if(@pfromdate != '') begin set @strsql += ' , o.requestdatetime >= ''' + @pfromdate + '''' end
note: i'm not positive have quoting correct make sure date inside single quotes. print out sql statement make sure looks correct before executing.
Comments
Post a Comment