IF语句t-sql

时间:2013-08-23 10:39:37

标签: sql sql-server

包含列表:number1,number2,number3

在程序中进行查询:SELECT number1,number2,number3 FROM myTbl

,现在要检查

if number2 < number1 and number1 < number3 print 'OK'` 

怎么做?我不能写选择它

6 个答案:

答案 0 :(得分:3)

如果您需要选择:

SELECT number1, number2, number3
FROM YourTable
WHERE
  number2 < number1 
  AND number1 < number3

SELECT 
  number1, number2, number3,
  CASE WHEN number2 < number1 AND number1 < number3 THEN 'OK' ELSE '' END AS Res
FROM YourTable    

答案 1 :(得分:3)

问题有点不清楚,但如果你想要条件打印,试试这个

if exists (SELECT * FROM myTbl where number2 < number1 and number1 < number3)
    print 'Ok'

答案 2 :(得分:1)

这样的东西?

declare @test int;
select @test = case when number2<number1 and number1<number3 then 1 else 0 end
       from test1 where id=3;
if @test = 1 then   print 'OK'

SQL Fiddle

答案 3 :(得分:0)

我想你想要这个

if @number2 < @number1 and @number1 < @number3
Begin
    Print 'OK'
End

或者这个

Select CASE WHEN ( @number2 < @number1 and @number1 < @number3THEN expression1) 
Then 'OK' Else '' End

答案 4 :(得分:0)

我想:

DECLARE @number1 int
DECLARE @number2 int
DECLARE @number3 int

SELECT 
    @number1 = number1,
    @number2 = number2,
    @number3 = number3
FROM myTbl
WHERE (some condition to get only row)

IF (@number2 < @number1 and @number1 < @number3)
BEGIN
    print 'OK'
END

答案 5 :(得分:0)

declare @ID int = '' --im assuming you have some sort of ID and multiple rows in the table
declare @num1 int = (select number1 from mytable where [ID] = @ID)
declare @num2 int = (select number2 from mytable where [ID] = @ID)
declare @num3 int = (select number2 from mytable where [ID] = @ID)

IF @num2 < @num1 AND @num1 < @num3
begin
SELECT 'OK'
PRINT 'OK'
end

ELSE
begin
SELECT'OK'
PRINT 'OK'
END