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

前端之家收集整理的这篇文章主要介绍了sql-server – 如果在存储过程中存在sql server前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一个存储过程如下:
  1. Create Procedure sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY
  2. (
  3. @ParLngId int output
  4. )
  5. as
  6. Begin
  7. SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client')
  8. if(@ParLngId = 0)
  9. begin
  10. Insert Into T_Param values ('PHY','Extranet Client',Null,'T',NULL,1,NULL)
  11. SET @ParLngId = @@IDENTITY
  12. End
  13. Return @ParLngId
  14. End

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

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

有人有解决方案吗?

解决方法

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

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

if not exists

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

猜你在找的MsSQL相关文章