[英]how to write the if condition in sql sever select statement to create the storedprocedure
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 我正在使用sql server2008(dynamic sql)编写一个存储过程,现在我的问题是我正在向存储过程传递4个值,因为UserAgentID varchar(50)传递了值或为空,现在我可以传递基于(1233)在该显示值上,我现在基于显示的字段null值传递空的like(''),但是我正在使用表Agentid是Integer数据类型,因此我正在传递IS NULL如何基于该条件写条件,我正在尝试像这样
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: 这个问题有点令人困惑,但是如果它以AgentID问题为中心,我将做出以下假设:
1) AgentID in your table has data type of int 1)您表中的AgentID的数据类型为int
2) You also have NULL values in the AgentID column 2)您在AgentID栏中也有NULL值
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: 因此,当您将该变量作为字符串传递时(不应该这样做,保持数据类型一致并传递0或其他无效的ID值),您可以尝试执行如下的AgentID条件:
--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... 如果您的agentid是varchar类型的列,则应该可以执行以下操作...
... 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. 再次,条件语句和数据类型使我陷入困境(ID列是否需要“ LIKE”?),但希望这会引导您朝正确的方向发展。
HTH 高温超导
Dave 戴夫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.