我正在编写一个存储过程,在此过程中,我正在使用isnull。如果值为null,我想使用Select语句作为替换值,这甚至可以吗?

IF ISNULL(@v_FilePrefix, (SELECT @v_FilePrefix = TransactionTypePrefix 
                            FROM [ConfigTransactionType] 
                           WHERE TransactionTypeID = @TransactionTypeID));
有帮助吗?

解决方案

您可以使用以下方式:

IF @v_FilePrefix IS NULL
BEGIN

    SELECT @v_FilePrefix = TransactionTypePrefix
    FROM [ConfigTransactionType]
    WHERE TransactionTypeID = @TransactionTypeID

END

我认为这是您所追求的?

其他提示

假设@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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top