簡體   English   中英

如何在SQL Sever Select語句中編寫if條件以創建存儲過程

[英]how to write the if condition in sql sever select statement to create the storedprocedure

我正在使用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

現在我在用戶表中傳遞空值,所以如何寫條件請幫助我任何人...謝謝

您可以嘗試這樣的事情

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'

要么

  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'

這個問題有點令人困惑,但是如果它以AgentID問題為中心,我將做出以下假設:

1)您表中的AgentID的數據類型為int

2)您在AgentID欄中也有NULL值

因此,當您將該變量作為字符串傳遞時(不應該這樣做,保持數據類型一致並傳遞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 

...

如果您的agentid是varchar類型的列,則應該可以執行以下操作...

... and ISNULL(US.AgentID,'') like ''' + ISULL(@UserAgentID,'') + ''' and

再次,條件語句和數據類型使我陷入困境(ID列是否需要“ LIKE”?),但希望這會引導您朝正確的方向發展。

高溫超導

戴夫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM