Ich habe eine gespeicherte SQL Server-Prozedur mit drei Bedingungen. Derzeit ist nur der erste IF
Block egal, die Parameter ausführen:SQL Server gespeicherte Prozedur mehrere IF-Anweisungen
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
/*
DESCRIPTION: Assigns/reassigns orders in [GIS].[dbo].[QCOrders]
PARAMETERS:
@FldrInitKey VARCHAR(30) = NULL,
@UserName VARCHAR(20) = NULL,
@QCTrack DECIMAL(6,2),
@WF BIT,
@idQC INT = NULL
RETURNS:
@RC 0 (success), 1 (failure)
EXAMPLE:
EXEC spQCAssignOrders '889G1Pt', 'arajendran', '2016.10', '0', NULL
MODIFICATIONS:
03/10/2016 A.Rajendran Created
*/
ALTER PROCEDURE [dbo].[spQCAssignOrders]
@FldrInitKey VARCHAR(30) = NULL,
@UserName VARCHAR(20) = NULL,
@QCTrack DECIMAL(6,2),
@WF BIT,
@idQC INT = NULL
AS
SET NOCOUNT ON
SET ROWCOUNT 0
SET XACT_ABORT ON
BEGIN TRY
/* Declare and initialize variables */
DECLARE @RC INT, @TranCount INT
SELECT
@RC = 0, @TranCount = @@TRANCOUNT
IF @TranCount = 0
BEGIN TRANSACTION
IF @idQC IS NOT NULL
BEGIN
UPDATE [GIS].[dbo].[QCOrders]
SET UserName = @UserName,
AssignedDte = CASE
WHEN @UserName IS NULL
THEN NULL
ELSE GETDATE()
END,
Unable = NULL,
Error = NULL,
CompletedDate = NULL,
AdminCheck = NULL
WHERE
idQC = @idQC
END
IF @FldrInitKey IS NOT NULL
BEGIN
UPDATE [GIS].[dbo].[QCOrders]
SET UserName = @UserName,
AssignedDte = GETDATE()
WHERE
QCTrack = @QCTrack
AND FldrInitKey = @FldrInitKey
AND WellsFargo = @WF
AND UserName IS NULL
AND Unable <> 1
END
IF @FldrInitKey IS NULL
BEGIN
UPDATE [GIS].[dbo].[QCOrders]
SET UserName = @UserName,
AssignedDte = GETDATE()
WHERE
QCTrack = @QCTrack
AND WellsFargo = @WF
AND UserName IS NULL
AND Unable <> 1
END
IF @TranCount = 0 AND (XACT_STATE()) = 1
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF (XACT_STATE()) = -1
ROLLBACK TRANSACTION
EXEC [TOD].[dbo].[spRethrowError]
SET @RC = 1
END CATCH
SET NOCOUNT OFF
SET ROWCOUNT 0
RETURN @RC
GRANT EXECUTE ON [GIS].[dbo].[spQCAssignOrders] TO db_allowexec
Wenn ich dies ausführen möchten:
EXEC spQCAssignOrders '889G1Pt', 'arajendran', '2016.10', '0', NULL
die zweite IF-Anweisung ausgeführt werden soll, aber es funktioniert nicht. Warum?
Klärung
zweiten IF-Anweisung:
IF @FldrInitKey IS NOT NULL
BEGIN
UPDATE [GIS].[dbo].[QCOrders]
SET UserName = @UserName,
AssignedDte = GETDATE()
WHERE
QCTrack = @QCTrack
AND FldrInitKey = @FldrInitKey
AND WellsFargo = @WF
AND UserName IS NULL
AND Unable <> 1
END
können Sie angeben, welche 'IF' ?. Für mich ist der zweite 'IF'' IF @idQC IS NOT NULL' und '@ idQC' ist' NULL'. – Lamak
Der zweite 'IF' sollte' IF @FldrInitKey IS NOT NULL' sein. – alybaba726
ok, aber ... Nun, dein erstes 'IF' ** ist **' IF @TranCount = 0'. Sie sollten das in Ihrer Frage anstelle eines Kommentars klarstellen – Lamak