Transact-sql中的条件运算符

时间:2010-01-18 20:30:09

标签: sql sql-server

有没有办法做到这一点,例如在Transact-sql中使用某种条件运算符?

IF @ParentBinaryAssetStructureId = -1
BEGIN
    SET @ParentBinaryAssetStructureId = NULL
END

UPDATE  BinaryAssets.BinaryAssetStructures 
SET     ParentBinaryAssetStructureId = @ParentBinaryAssetStructureId
WHERE   BinaryAssetStructureId = @OriginalBinaryAssetStructureId

3 个答案:

答案 0 :(得分:7)

USE NULLIF()

UPDATE  BinaryAssets.BinaryAssetStructures 
SET     ParentBinaryAssetStructureId = NULLIF(@ParentBinaryAssetStructureId,-1)
WHERE   BinaryAssetStructureId = @OriginalBinaryAssetStructureId

答案 1 :(得分:4)

c语言中的三元(条件)运算符:

x = doSomething ? 5 : 7

将在SQL中以这样的方式编写:

SELECT @x = CASE WHEN @doSomething = 1 THEN 5 ELSE 0 END

可能有多个案例(当条款时):

SELECT @x = CASE WHEN @doSomething = 1 THEN 5 WHEN @somethingElse = 1 THEN 20 ELSE 0 END

答案 2 :(得分:2)

UPDATE  BinaryAssets.BinaryAssetStructures 
SET     ParentBinaryAssetStructureId =
   CASE  ParentBinaryAssetStructureId  
     WHEN -1 THEN NULL
     ELSE ParentBinaryAssetStructureId
   END
WHERE   BinaryAssetStructureId = @OriginalBinaryAssetStructureId

给那个旋转