2009-11-06 24 views
13

Qualcuno può, per favore, indicare che cosa sta facendo male con questa stored procedure, per favore. Non riesco a farlo compilare e il mio software non fornisce alcun indizio utile su cosa ci sia che non va.Procedura memorizzata IF/ELSE

CREATE PROCEDURE web.createSubscriptions 
    (
    @Member_Id BIGINT, 
    @Trans_type VARCHAR(100), 
    @Payment_Status VARCHAR(100), 
    @Payment_Date DATETIME, 
    @Trans_Id VARCHAR(100) 
    ) 

AS 
DECLARE @tmpType VARCHAR(15) 
BEGIN 

INSERT INTO TBL_SUBSCRIPTIONS (subs_MemberID, subs_Type, subs_Status, subs_DateGenerated, subs_PaypalTransaction) VALUES(@Member_Id, @Trans_Type, @Payment_Status, @Payment_Date, @Trans_Id) 

IF(@Trans_type = 'subscr_signup') 
    BEGIN 
    @tmpType = 'premium' 
    END 
ELSE(@Trans_type = 'subscr_cancel') 
    BEGIN 
    @tmpType = 'basic' 
    END 

UPDATE TBL_MEMBERS 
SET members_Type = @tmpType 
WHERE members_Id = @Member_Id 

END 

risposta

15

Nick ha ragione. L'errore successivo è il else dovrebbe essere else if (al momento hai un'espressione booleana nel tuo altro che non ha senso). Ecco quello che dovrebbe essere

ELSE IF(@Trans_type = 'subscr_cancel') 
    BEGIN 
    SET @tmpType = 'basic' 
    END 

Al momento è la seguente (che è sbagliato):

ELSE(@Trans_type = 'subscr_cancel') 
    BEGIN 
    SET @tmpType = 'basic' 
    END 

Ecco un suggerimento per il futuro- doppio click sull'errore e SQL Server Management Studio andrà alla riga in cui risiede l'errore. Se pensi che SQL Server dia errori criptici (cosa che non penso che faccia), allora non hai lavorato con Oracle!

+0

Eccellente grazie. Sciocco errore da fare. ;-( – Munklefish

+0

Sono contento che sia ordinato ora. – RichardOD

+0

@Munklefish hey ma sono contento che tu l'abbia fatto (non che io sia cattivo ma ..) perché questo mi ha aiutato con una domanda di base che ho pensato a SQL: P –

8

Non sta dando errori? Prova
SET @tmpType = 'premium '
e
SET @tmpType = 'basic'

+0

ho appena provato e che restituisce il seguente errore inutile la sintassi non corretta in prossimità '@Trans_type'. – Munklefish

+0

Questo è sicuramente parte del problema – RichardOD

+0

La mia risposta ha la prossima soluzione. :-) – RichardOD

1

provare

set @tmptype 
+0

ho appena provato e che restituisce il seguente errore inutile "la sintassi non corretta in prossimità '@Trans_type'" – Munklefish

3

Vi manca la '' dichiarazione SET quando si assegnano alle variabili nel IF .. blocco ELSE?

+0

ho appena provato e che restituisce il seguente errore inutile "la sintassi non corretta in prossimità '@Trans_type'" – Munklefish

1

sì Nick ha ragione.

è necessario utilizzare SET o SELECT per assegnare a @tmpType

+0

Ho appena provato e restituisce il seguente errore inutile "Sintassi non corretta vicino a '@Trans_type'" – Munklefish

+0

Vedere la mia risposta per la prossima correzione ... – RichardOD

-1

provare

IF(@Trans_type = 'subscr_signup')  
BEGIN 
set @tmpType = 'premium' 
END 
ELSE iF(@Trans_type = 'subscr_cancel') 
    begin 
    set @tmpType = 'basic' 
    END 
+0

Che ha vinto funziona. Altrimenti deve essere altrimenti se – RichardOD

+0

bit di un commento strano che arriva tanto tempo dopo che la vera risposta è stata rivelata. ;-) – Munklefish

0

Solo un consiglio per questo, non è necessario l'inizio e di fine se contiene solo una singola istruzione.

cioè:

IF(@Trans_type = 'subscr_signup')  
set @tmpType = 'premium' 
ELSE iF(@Trans_type = 'subscr_cancel') 
    set @tmpType = 'basic' 
0

Prova questa con SQL join dichiarazioni

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 
Problemi correlati