Multiple argument IF statement – T-SQL

You are doing it right. The empty code block is what is causing your issue. It’s not the condition structure 🙂 DECLARE @StartDate AS DATETIME DECLARE @EndDate AS DATETIME SET @StartDate = NULL SET @EndDate = NULL IF (@StartDate IS NOT NULL AND @EndDate IS NOT NULL) BEGIN print ‘yoyoyo’ END IF (@StartDate IS NULL … Read more

Set column as primary key if the table doesn’t have a primary key

This checks if primary key exists, if not it is created IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = ‘PRIMARY KEY’ AND TABLE_NAME = ‘Persons’ AND TABLE_SCHEMA =’dbo’) BEGIN ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id) END ELSE BEGIN — Key exists END fiddle: http://sqlfiddle.com/#!6/e165d/2

How to improve a case statement that uses two columns

You could do it this way: — Notice how STATE got moved inside the condition: CASE WHEN STATE = 2 AND RetailerProcessType IN (1, 2) THEN ‘”AUTHORISED”‘ WHEN STATE = 1 AND RetailerProcessType = 2 THEN ‘”PENDING”‘ ELSE ‘”DECLINED”‘ END The reason you can do an AND here is that you are not checking the … Read more

Declare a variable in DB2 SQL

I assume this forum posting, which I quote fully below, should answer the question. Inside a procedure, function, or trigger definition, or in a dynamic SQL statement (embedded in a host program): BEGIN ATOMIC DECLARE example VARCHAR(15) ; SET example=”welcome” ; SELECT * FROM tablename WHERE column1 = example ; END or (in any environment): … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)