加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 如果在存储过程中存在sql server

发布时间:2020-12-12 06:57:04 所属栏目:MsSql教程 来源:网络整理
导读:我已经创建了一个存储过程如下: Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY(@ParLngId int output)asBegin SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client') if(@ParLngId = 0) begin Insert Into
我已经创建了一个存储过程如下:
Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
(
@ParLngId int output
)
as
Begin
    SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
    if(@ParLngId = 0)
        begin
            Insert Into T_Param values ('PHY','Extranet Client',Null,'T',NULL,1,NULL)
            SET @ParLngId = @@IDENTITY
        End
    Return @ParLngId
End

所以我设置一个变量@ParLngId,我检查一个表中是否有这样的数据,如果是,我返回值,如果不是我插入一个,并返回包含插入行的Id的变量…但现在它显示了一个SqlException:

Subquery returned more values. This is not permitted when the subquery follows =,! =,<,<=,>,> = Or when used as an expression.

有人有解决方案吗?

解决方法

感谢大家的答案,但我知道如何做到这一点,最后的程序如下所示:
Create Procedure sp_ADD_RESPONSABLE_EXTRANET_CLIENT
(
@ParLngId int output
)
as
Begin
if not exists (Select ParLngId from T_Param where ParStrIndex = 'RES' and ParStrP2 = 'Web')
    Begin
            INSERT INTO T_Param values('RES','¤ExtranetClient','ECli','Web','non','ExtranetClient',25032,'informatique.interne@company.fr','Extranet-Client',27,0 )
            SET @ParLngId = @@IDENTITY
    End
Else
    Begin
            SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
            Return @ParLngId
    End   
End

所以我发现的东西,使它的工作原理是:

if not exists

它允许我们使用一个布尔值而不是Null或0或一个由count()导致的数字,

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读