我已经创建了一个存储过程如下:
- 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()导致的数字,