Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

IF Conditions in SQL

$
0
0

Hello, I am progressing in my knowledge of SQL. I have a query that I built that will INSERT and UPDATE a table depending on a parameter that gets passed in from a web page. It works fine. But, I need to make an upgrade and I am not sure where to begin. I have an idea, but wanted to get some input. My stored proc takes the following params and I have conditions set on how the updates/Inserts will be performed:

DECLARE @sSql NVARCHAR(4000),@FromFlag VARCHAR(2), @ToFlag VARCHAR(2), @CountCheckedBox INT,
		  @OrderNumber VARCHAR(50), @RowCount INT, @Msg VARCHAR(100),@UserId VARCHAR(50),@Key VARCHAR(30),@OrderID INT
  SET @CountCheckedBox = 0
  SET @UserID = '__AuthUser__' 
  SET @Key = '__KEY__'	


  IF '__FROM9TO0__' = 'Y'  SELECT @FromFlag = '9', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1
  IF '__FROM5TO0__' = 'Y' SELECT @FromFlag = '5', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1
  IF '__FROM5TO9__' = 'Y' SELECT @FromFlag = '5', @ToFlag = '9', @CountCheckedBox = @CountCheckedBox + 1
  IF '__FROM-1TO0__' = 'Y' SELECT @FromFlag = '-1', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1
  IF '__FROM-1TO9__' = 'Y' SELECT @FromFlag = '-1', @ToFlag = '9', @CountCheckedBox = @CountCheckedBox + 1 

My new requirement is:

I want to do a complete UPDATE of my table now, based on the last IF statement

IF '__FROM-1TO9__' = 'Y' SELECT @FromFlag = '-1', @ToFlag = '9', @CountCheckedBox = @CountCheckedBox + 1 

I do not need to pass in the  @OrderNumber VARCHAR(50) now. Because I want to UPDATE a flag on all records if this condition is true. I believe that I need to do this:

IF '__FROM9TO0__' = 'Y' SELECT @FromFlag = '9', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1 IF '__FROM5TO0__' = 'Y' SELECT @FromFlag = '5', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1 IF '__FROM5TO9__' = 'Y' SELECT @FromFlag = '5', @ToFlag = '9', @CountCheckedBox = @CountCheckedBox + 1 IF '__FROM-1TO0__' = 'Y' SELECT @FromFlag = '-1', @ToFlag = '0', @CountCheckedBox = @CountCheckedBox + 1 IF '__FROM-1TO9__' = 'Y' SELECT @FromFlag = '-1', @ToFlag = '9', @CountCheckedBox = @CountCheckedBox + 1 BEGIN DECLARE @sSql2 NVARCHAR(4000),@FromFlag2 VARCHAR(2), @ToFlag2 VARCHAR(2), @CountCheckedBox2 INT SET @FromFlag2 = @FromFlag SET @ToFlag2 = @ToFlag SET @CountCheckedBox2 = @CountCheckedBox --UPDATE FLAG NOW SET @RowCount = 0 EXEC('UPDATE __MYDB__..myTbl SET Status = ? WHERE Status = ? AND Key = ? ',@ToFlag,@FromFlag,@Key) AT __MySERVER__ SET @RowCount = @@ROWCOUNT END

--my other logic goes here for the other IF statements


Will this be sufficient??


mark a fisher


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>