English 中文(简体)
国际自由工会联合会
原标题:IF condition in SQL query

我在此问:

Declare @Type varchar

select  
  if
    (tn.notification_type=1)
  begin
    set @Type=  WORK FLOW 
    print   status   + CAST(@type AS nvarchar(58))  
  end
from tbl_Notification tn

在此,我遇到了一个问题,即列入一个基于表列价值的条件。

E.g. 我有价值1和2及3

当我执行时,我就犯了错误。

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword if .
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword from .

最佳回答

To elaborate on Namphibian s answer, which is accurate:

SELECT
      @Type = (CASE tn.Notification_Type 
                   WHEN 1 THEN  WORK FLOW  
                   ELSE  SOMETHING ELSE  
                END)
FROM tbl_Notification tn

你们也取得了这样的成绩,即你不得不在后来或某种情况下这样做。

问题回答

将发言改为案件说明。 你们可以这样说。

Declare @Type varchar 

if((select tn.notification_type from tbl_Notification tn) = 1) 
begin
   set @Type=  WORK FLOW  
   print  status  + CAST(@type AS nvarchar(58))
end 




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签