我正在编写存储过程,在此过程中我使用的是isNULL。如果值为null,我想使用select语句作为替换值,这是否可能?
IF ISNULL(@v_FilePrefix, (SELECT @v_FilePrefix = TransactionTypePrefix
FROM [ConfigTransactionType]
WHERE TransactionTypeID = @TransactionTypeID));
答案 0 :(得分:1)
您可以使用:
IF @v_FilePrefix IS NULL
BEGIN
SELECT @v_FilePrefix = TransactionTypePrefix
FROM [ConfigTransactionType]
WHERE TransactionTypeID = @TransactionTypeID
END
我认为这就是你所追求的目标?
答案 1 :(得分:1)
假设@TransactionTypeID将始终返回值:
SELECT @v_FilePrefix = COALESCE(@v_FilePrefix, TransactionTypePrefix)
FROM [ConfigTransactionType]
WHERE TransactionTypeID = @TransactionTypeID
COALESCE将返回第一个非空值。如果@v_FilePrefix不为null,则只将值设置为自身。
但最好使用:
IF @v_FilePrefix IS NULL
BEGIN
SELECT @v_FilePrefix = TransactionTypePrefix
FROM [ConfigTransactionType]
WHERE TransactionTypeID = @TransactionTypeID
END