如果该值为 NULL,则发送其他内容

If the value is NULL then send something else

列 MaxPlaces 有时有一个值,有时只是 NULL。 我必须将此过程发送到 C#,但是当在代码中处理 NULL 时,代码会中断。

ISNULL 不工作出现以下错误:

Additional information: Column 'MaxPlaces' does not belong to table getLvLValue.

 ALTER PROCEDURE [dbo].[getLvLValue] @LevID NVARCHAR(100)
 AS
 BEGIN
     SELECT a.BCID
         ,a.CharacteristicID
         ,b.CharacteristicName
         ,a.LevID
         ,ISNULL(b.MaxPlaces, '20')
    FROM BCCharacteristics AS a
    INNER JOIN Characteristic AS b ON a.CharaID = b.CharaID
    WHERE LevID = 1
    AND a.BCID = @LevelID
    AND a.CharaID = b.CharaID;
 END

谁能帮我解决这个问题。只需发送一个参数,如果 MaxPlaces 为 NULL,则将值 20 发送到 C#。

提前致谢!

它应该有效,但您没有为结果列指定别名,试试这个:

ALTER PROCEDURE [dbo].[getLvLValue] 
       @LevID  nvarchar(100)
AS
BEGIN

SELECT a.BCID
     , a.CharacteristicID
     , b.CharacteristicName
     , a.LevID, ISNULL(b.MaxPlaces, '20') as MaxPlaces --here was the issue
FROM BCCharacteristics AS a INNER JOIN Characteristic AS b ON a.CharaID = b.CharaID
WHERE LevID =1 AND a.BCID = @LevelID AND a.CharaID = b.CharaID;

END

您应该提供列名称

ALTER PROCEDURE [dbo].[getLvLValue] 
   @LevID  nvarchar(100)

AS
BEGIN
SELECT a.BCID, a.CharacteristicID, b.CharacteristicName, a.LevID, ISNULL(b.MaxPlaces, '20') as MaxPlaces
 FROM BCCharacteristics AS a INNER JOIN Characteristic AS b ON a.CharaID = b.CharaID
WHERE LevID =1 AND a.BCID = @LevelID AND a.CharaID = b.CharaID;

END