I seem to be missing something here. This is from a SQL book:
======
In the following snippet, we have two similar stored procedures. The difference is in the use of the BEGIN … END code block. Executing the first one will execute the second PRINT statement, as the IF statement will take only the first line as part of the decision:
Compare it with the following snippet. Here, neither of the PRINT statements will be executed:Code:CREATE PROCEDURE ut_NoBeginEnd AS BEGIN DECLARE @var1 int, @var2 int SET @var1 = 1 SET @var2 = 2 IF @var1 = @var2 PRINT @var1 PRINT @var2 END
======Code:CREATE PROCEDURE ut_BeginEnd AS BEGIN DECLARE @var1 int, @var2 int SET @var1 = 1 SET @var2 = 2 IF @var1 = @var2 BEGIN PRINT @var1 PRINT @var2 END END
Now, in the first example, why does the first PRINT statement not run?
In the second, why don't either of them run?
I need to create an IF statement based on a SELECT Statement like this:
It seems simple enough to me. If there are records found from the SELECT statement then I don't want to do the DELETE statements, but need to let the calling procedure know this was a problem, but can't convince SQL Server of all this.Code:ALTER PROCEDURE [dbo].[frmOpenImportRemoveItem] @nvchrAction nvarchar(50) = '', @ImportIDToDelete int = 0 AS IF @nvchrAction = 'REMOVEIMPORT' BEGIN -- verify no items were posted for this invoice. IF ((SELECT count(bitProcessed) FROM tblInvoiceImportHistory WHERE intCustomerInvoiceImportID = @ImportIDToDelete) = 0) -- If there are no posted items, then remove the import DELETE tblInvoiceImportHistoryAddresses WHERE intCustomerInvoiceImportID = @ImportIDToDelete DELETE tblInvoiceImportHistoryAmounts WHERE intCustomerInvoiceImportID = @ImportIDToDelete DELETE tblInvoiceImportHistory WHERE intCustomerInvoiceImportID = @ImportIDToDelete DELETE tblInvoiceImportParams WHERE intCustomerInvoiceImportParamID = @ImportIDToDelete -- indicate successful RETURN 0 ELSE -- otherwise indicate not successful RETURN 1 END END
I'm considering returning 1 if @ImportIDToDelete int = 0, but one step at a time.
So, what am I missing?
Thanks you!




Reply With Quote