Greetings, I am writing a stored procedure where I would like to be able to process 1, 2, or 3 passed parameters.I thought that I could just create 3 seperate IF blocks but it is not working the way I intended.Expected execution: Each of the three passed variables are sequentially checked for a value other than NULL. If any of the three values contain a valid value then the appropriate UPDATE query is ran. This allows for a single call from the .NET code instead of using three seperate SP calls. Actual execution: When passing any one of the three parameters it works as intended.Like this:EXEC [dbo].[usp_EditSubmittedCharge] @PatientURN = N'9876543210', @CurrentDateOfCharge = '2011-06-06', @CurrentTimeOfCharge = '06:06:00', @NewDateOfCharge = NULL, @NewTimeOfCharge = '06:10:00', @NewCPTcode = NULLGO
When more than one parameter is passed, then only the first IF statement that is TRUE will be processed.I assumed that every IF statement would be evaluated sequentially, but it seems as though as soon as any of the IF conditions is met, the procedure terminates.In this example, only @NewTimeOfCharge will updateEXEC [dbo].[usp_EditSubmittedCharge] @PatientURN = N'9876543210', @CurrentDateOfCharge = '2011-06-06', @CurrentTimeOfCharge = '06:06:00', @NewDateOfCharge = NULL, @NewTimeOfCharge = '06:10:00', @NewCPTcode = '12345'GO
Explanation of Passed Parameters : @PatientURN varchar(10) -- Use to identify record to edit @CurrentDateOfCharge as date -- Use to identify record to edit @CurrentTimeOfCharge as time(0) -- Use to identify record to edit @NewDateOfCharge as date -- New value or NULL @NewTimeOfCharge as time(0) -- New value or NULL @NewCPTcode as varchar(10) -- New value or NULLCREATE PROCEDURE [dbo].[usp_EditSubmittedCharge] @PatientURN varchar(10) ,@CurrentDateOfCharge as date ,@CurrentTimeOfCharge as time(0) ,@NewDateOfCharge as date ,@NewTimeOfCharge as time(0) ,@NewCPTcode as varchar(10)AS-- If a new charge date was passedIF @NewDateOfCharge IS NOT NULL BEGIN UPDATE SubmittedCharges SET DateOfCharge=@NewDateOfCharge WHERE SubmittedCharges.URN = @PatientURN AND SubmittedCharges.DateOfCharge = @CurrentDateOfCharge AND SubmittedCharges.TimeOfCharge = @CurrentTimeOfChargeEND-- If a new charge time was passedIF @NewTimeOfCharge IS NOT NULL BEGIN UPDATE SubmittedCharges SET TimeOfCharge=@NewTimeOfCharge WHERE SubmittedCharges.URN = @PatientURN AND SubmittedCharges.DateOfCharge = @CurrentDateOfCharge AND SubmittedCharges.TimeOfCharge = @CurrentTimeOfChargeEND-- If a new CPT code was passedIF @NewCPTcode IS NOT NULL BEGIN UPDATE SubmittedCharges SET ChargeCode=@NewCPTcode WHERE SubmittedCharges.URN = @PatientURN AND SubmittedCharges.DateOfCharge = @CurrentDateOfCharge AND SubmittedCharges.TimeOfCharge = @CurrentTimeOfChargeEND
This is the script to create the test table that I am working with.USE [MobileChargeCapture]GO/****** Object: Table [dbo].[SubmittedCharges] Script Date: 11/08/2011 08:58:45 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[SubmittedCharges]( [URN] [varchar](15) NOT NULL, [DateOfCharge] [date] NOT NULL, [TimeOfCharge] [time](0) NOT NULL, [ChargeCode] [varchar](20) NOT NULL, [SubmittedByUser] [varchar](6) NOT NULL, [ChargeComplete] [varchar](1) NOT NULL, [DateTimeStamp] [datetime2](0) NOT NULL, CONSTRAINT [PK_Charges] PRIMARY KEY CLUSTERED ( [URN] ASC, [DateOfCharge] ASC, [TimeOfCharge] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOINSERT [dbo].[SubmittedCharges] ([URN], [DateOfCharge], [TimeOfCharge], [ChargeCode], [SubmittedByUser], [ChargeComplete], [DateTimeStamp]) VALUES (N'9876543210', CAST(0x55340B00 AS Date), CAST(0x00B8560000000000 AS Time), N'66666', N'ISMEB', N'N', CAST(0x00156400E9340B0000 AS DateTime2))INSERT [dbo].[SubmittedCharges] ([URN], [DateOfCharge], [TimeOfCharge], [ChargeCode], [SubmittedByUser], [ChargeComplete], [DateTimeStamp]) VALUES (N'9876543210', CAST(0xE9340B00 AS Date), CAST(0x00782D0000000000 AS Time), N'36620', N'ISMEB', N'N', CAST(0x00616500E9340B0000 AS DateTime2))INSERT [dbo].[SubmittedCharges] ([URN], [DateOfCharge], [TimeOfCharge], [ChargeCode], [SubmittedByUser], [ChargeComplete], [DateTimeStamp]) VALUES (N'9876543210', CAST(0xE9340B00 AS Date), CAST(0x00D0B60000000000 AS Time), N'31500', N'ISMEB', N'N', CAST(0x0088B700E9340B0000 AS DateTime2))INSERT [dbo].[SubmittedCharges] ([URN], [DateOfCharge], [TimeOfCharge], [ChargeCode], [SubmittedByUser], [ChargeComplete], [DateTimeStamp]) VALUES (N'9876543210', CAST(0xE9340B00 AS Date), CAST(0x00A0B90000000000 AS Time), N'31500', N'ISMEB', N'N', CAST(0x002CB800E9340B0000 AS DateTime2))
Am I missing something? Can I have multiple IF's without nesting, or utilizing ELSE IF or ELSEThank you,Mike