2013-08-30 48 views
25

我創建了一個存儲過程如下:如果別人在存儲過程中的SQL Server

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, Null, 'T', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL) 
      SET @ParLngId = @@IDENTITY 
     End 
    Return @ParLngId 
End 

所以我設置一個變量@ParLngId,我檢查是否有一個表中的這些數據,如果是,我回來值,如果不是我插一句,返回包含插入行的ID變量...但現在它讓我看到一個SQLException:

子查詢返回多個值。這是不允許的,當子查詢遵循=,! =,<,< =,>,> =或用作表達式時。

有人有解決方案嗎?

+0

「的錯誤說,不同的價值觀已返回」 '='標誌 - 你能在錯誤消息對我們來說,確切的文本編輯嗎? – AakashM

+0

你能複製/粘貼確切的錯誤信息嗎? – UnhandledExcepSean

+0

我剛剛編輯過這個職位 –

回答

35

謝謝大家的答案,但我想出如何做到這一點,最後的程序看起來像這樣:

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', 1, 1, Null, Null, 'non', 'ExtranetClient', 'ExtranetClient', 25032, Null, '[email protected]', 'Extranet-Client', Null, 27, Null, Null, Null, Null, Null, Null, Null, Null, 1, Null, Null, 0) 
      SET @ParLngId = @@IDENTITY 
    End 
Else 
    Begin 
      SET @ParLngId = (Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client') 
      Return @ParLngId 
    End 
End 

所以,我發現的東西出來,這使得它的工作原理是:

如果不存在

它允許我們使用一個布爾值,而不是或或數量造成的計數()

10

如果沒有匹配的行/秒,然後@ParLngIdNULL不爲零,所以你需要IF @ParLngId IS NULL。您也應該使用SCOPE_IDENTITY()而不是@@IDENTITY

+0

@@ Identity可以工作,它在我不需要使用否則如果,我不認爲這個問題來自它 –

0

嘗試這一個 -

CREATE PROCEDURE sp_ADD_USER_EXTRANET_CLIENT_INDEX_PHY 
AS 
BEGIN 

    DECLARE @ParLngId INT 
    SELECT TOP 1 @ParLngId = ParLngId 
    FROM dbo.T_Param 
    WHERE ParStrNom = 'Extranet Client' 

    IF (@ParLngId = 0) 
    BEGIN 
     INSERT INTO dbo.T_Param 
     VALUES ('PHY', 'Extranet Client', NULL, NULL, 'T', 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL) 

     RETURN SCOPE_IDENTITY() 
    END 
    ELSE BEGIN 

     RETURN @ParLngId 

    END 

END 
+1

它也行不通... –

0

嘗試對連接查詢語句

CREATE PROCEDURE [dbo].[deleteItem] 
@ItemId int = 0 
AS 
Begin 
DECLARE @cnt int; 

SET NOCOUNT ON 
SELECT @cnt =COUNT(ttm.Id) 
    from ItemTransaction itr INNER JOIN ItemUnitMeasurement ium 
     ON itr.Id = ium.ItemTransactionId INNER JOIN ItemMaster im 
     ON itr.ItemId = im.Id INNER JOIN TransactionTypeMaster ttm 
     ON itr.TransactionTypeMasterId = ttm.Id 
     where im.Id = @ItemId 

if(@cnt = 1) 
    Begin 
    DECLARE @transactionType varchar(255); 
    DECLARE @mesurementAmount float; 
    DECLARE @itemTransactionId int; 
    DECLARE @itemUnitMeasurementId int; 

     SELECT @transactionType = ttm.TransactionType, @mesurementAmount = ium.Amount, @itemTransactionId = itr.Id, @itemUnitMeasurementId = ium.Id 
     from ItemTransaction itr INNER JOIN ItemUnitMeasurement ium 
      ON itr.Id = ium.ItemTransactionId INNER JOIN TransactionTypeMaster ttm 
      ON itr.TransactionTypeMasterId = ttm.Id 
      where itr.ItemId = @ItemId 
     if(@transactionType = 'Close' and @mesurementAmount = 0) 
      Begin 
       delete from ItemUnitMeasurement where Id = @itemUnitMeasurementId; 

      End 
     else 
      Begin 
       delete from ItemTransaction where Id = @itemTransactionId; 
      End 
    End 
else 
Begin 
    delete from ItemMaster where Id = @ItemId; 
End 
END 
1
if not exists (select dist_id from tbl_stock where dist_id= @cust_id and [email protected]_id) 
    insert into tbl_stock(dist_id,item_id,qty)values(@cust_id, @item_id, @qty); 
else 
    update tbl_stock set qty=(qty + @qty) where dist_id= @cust_id and item_id= @item_id; 
-1

而不是寫作:

Select top 1 ParLngId from T_Param where ParStrNom = 'Extranet Client' 

寫:

Select top 1 ParLngId from T_Param where ParStrNom IN 'Extranet Client' 

即替代由'IN'