i am writing one stored procedure using sql server2008(dynamic sql), now my problem is i am passing 4 values to stored procedure in that UserAgentID varchar(50) pass the value or empty, now i can pass any value like(1233) based on that display the values and i am passing empty like('') now based on the field null values displayed but i am taking table Agentid is Integer datatype so i am passing IS NULL how to write the condition base on that , i am trying like this
ALTER Procedure [dbo].[usp_GetSearch123]
(
@SearchValue varchar(100),
@SearchBy varchar(250),
@DbName varchar(50),
@UserAgentID varchar(50)
)
AS
Begin
Declare @cmd varchar(5000)
select @cmd =
'Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.SubmissionNumber,QT.UnderWriter as UnderWriter,LC.Name as LineCode,QT.DBAName as DBAName
from Agent_Quote QT,Users US,' + @DbName + '..StatusCode SC, ' + @DbName+'..Linecode LC
where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and SC.StatusCode!=''C'' and US.UserID=QT.CreatedBy and US.AgentID like'''+@UserAgentID+''' and
(QT.Name like ''' + @SearchValue + '%'' or QT.DBAName like ''' + @SearchValue +'%'')
order by QT.Name,QuoteNumber desc'
print @cmd
exec(@cmd)
end
now i am passing empty in user table null values there so how to write the condition pls help me any one... thank u hemanth
You can try something like this
IF ISNULL(@UserAgentID, -1) = -1
select @cmd =
'Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.SubmissionNumber,
QT.UnderWriter as UnderWriter,LC.Name as LineCode,QT.DBAName as DBAName
from Agent_Quote QT,Users US,' + @DbName + '..StatusCode SC, ' + @DbName+'..Linecode LC
where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and
SC.StatusCode!=''C'' and US.UserID=QT.CreatedBy and
US.AgentID is nulll and
(QT.Name like ''' + @SearchValue + '%'' or QT.DBAName like ''' + @SearchValue +'%'')
order by QT.Name,QuoteNumber desc'
else
select @cmd =
'Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.SubmissionNumber,
QT.UnderWriter as UnderWriter,LC.Name as LineCode,QT.DBAName as DBAName
from Agent_Quote QT,Users US,' + @DbName + '..StatusCode SC, ' + @DbName+'..Linecode LC
where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and
SC.StatusCode!=''C'' and US.UserID=QT.CreatedBy and
US.AgentID like'''+@UserAgentID+''' and
(QT.Name like ''' + @SearchValue + '%'' or QT.DBAName like ''' + @SearchValue +'%'')
order by QT.Name,QuoteNumber desc'
or
select @cmd =
'Select QT.Name,SC.Name as Status,QT.QuoteNumber,QT.SubmissionNumber,
QT.UnderWriter as UnderWriter,LC.Name as LineCode,QT.DBAName as DBAName
from Agent_Quote QT,Users US,' + @DbName + '..StatusCode SC, ' + @DbName+'..Linecode LC
where QT.StatusCode = SC.StatusCode And QT.LineCode = LC.LineCode and
SC.StatusCode!=''C'' and US.UserID=QT.CreatedBy and
((@UserAgentID is null AND US.AgentID is null) OR US.AgentID like'''+@UserAgentID+''') and
(QT.Name like ''' + @SearchValue + '%'' or QT.DBAName like ''' + @SearchValue +'%'')
order by QT.Name,QuoteNumber desc'
The question is a bit confusing but if it is centered on the AgentID issue, I'm going to make the following assumptions:
1) AgentID in your table has data type of int
2) You also have NULL values in the AgentID column
So with you passing in that variable as a string (which you should not do, keep data types consistant and pass in 0 or other non-valid id value) you could try doing your AgentID condition as follows:
--First, create int prameter and validate the @UserAgentID parameter
DECLARE @AgentID int
SELECT @AgentID = 0
IF ISNUMERIC(@UserAgentID)=1
BEGIN
SELECT @AgentID = CAST(@UserAgentID AS int)
END
--update your @UserAgentID condition statement as follows
... and ISNULL(US.AgentID,0) = '+CAST(@AgentID AS nvachar(10))+' and
...
if your agentid is a varchar type column then the following should work...
... and ISNULL(US.AgentID,'') like ''' + ISULL(@UserAgentID,'') + ''' and
Again, the conditional statement and data types are throwing me here (need a "LIKE" for an ID column?) but hopefully this leads you in the right direction.
HTH
Dave
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.