I have 3 variables which will be conditional when #path is not NULL i.e
#serverName,#analysisDB,#cubeName. Hence I used a CASE statement in the above SELECT statement. Similarly, for #path is not NULL the #cubename also needs to be conditional for LVL5.L5_CubeName (see REPLACE statement) but its inside a REPLACE statement and the CASE block becomes too complicated for the same. How do I write it if I want to do something like
CASE WHEN #path!='' THEN #cubeName ELSE LVL5.L5_CubeName in the REPLACE statement
SELECT LVL5.L5_Id,
CASE WHEN #path!='' THEN #serverName ELSE LVL5.L5_ServerName END [AnalysisServer],
CASE WHEN #path!='' THEN #analysisDB ELSE LVL5.L5_AnalysisDatabase END [AnalysisDatabase],
REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'##Level1',ISNULL(#lvl1DataVal,'')),
'##Level2',ISNULL(#lvl2DataVal,'')),'##CubeName',ISNULL(#cubeName,'')) [MDXQuery],
LVL5.L5_ReplaceMDX [ReplaceMDX],
LVL5.L5_RefreshDate [RefreshDate],
LVL5.L5_ReportAttribute [ReportAttribute],
LVL5.L5_ReportTitle [ReportTitle]
FROM Report_SR_Level5 [LVL5]
WHERE L4_ID = #L4_ID ORDER BY LVL5.L5_DisplayOrder
Something like this:
CASE WHEN #path is not null or #path != ''
THEN REPLACE(REPLACE(REPLACE(LVL5.L5_MDXQuery,'##Level1',
ISNULL(#lvl1DataVal,'')),'##Level2',ISNULL(#lvl2DataVal,'')),'##CubeName',
ISNULL(#cubeName,'')) [MDXQuery]
ELSE Somethingelse END cubedata
Related
I am having three parameters $page, $date, $search. i need a single select sql query where i have to check all the three variables one by one whether any of these variables is having some value or not or if all of the three variables is having some value. According to which it will match from the database and give the result. Please help me out.
You can make where clause as follows in your Select query.(Below is MS SQL format, you can modify CASE statement equivalent with MY SQL)
SELECT * FROM TABLE (if more than one table, you can use join)
WHERE TABLE.search_COLUMN = case when #search <> '' then #search else TABLE.search_COLUMN end
AND
TABLE.page_value_COLUMN = case when #page_value <> '' then #page_value else TABLE.page_value_COLUMN end
AND
TABLE.date_value_COLUMN = case when #date_value <> '' then #date_value else TABLE.date_value_COLUMN end
I want to write a query that will verify 8 parameters of a row are the same as the parameters passed to the procedure. If any one of them is different then modify a status, otherwise do nothing.
Is there a way I can check all those parameters in a single IF case? For example:
IF (v_duty <> duty) OR (v_current <> current) OR (v_frequency <> frequency) THEN
* UPDATE ......;
END IF
Or do I have to use an ELSE IF for each comparison I want to make?
The above doesn't work, with or without brackets between each test.
You could probably manage this with a simple UPDATE and WHERE condition:
UPDATE table_name
SET status_column = 'new_status'
WHERE identifying_column = :identifier
AND (
v_duty != :v_duty
OR v_current != :current
OR v_frequency != :frequency
)
Who can please explain the difference between CASE-statement, IF-statement and IF-function?
What is the difference in terms of usage and "how it works"?
From the manual, it looks like the if function is just a less flexible form of the case expression. For example, you could write:
select if(username = 'darxysaq', 'high', 'low') as awesomeness
And the equivalent with case:
select case when username = 'darxysaq' then 'high' else 'low' end as awesomeness
But case is more flexible. It allows more than one branch, like:
select case
when username = 'darxysaq' then 'high'
when username = 'john skeet' then 'medium'
else 'low'
end as awesomeness
And it can act like a switch:
select case username
when 'darxysaq' then 'high'
when 'john skeet' then 'medium'
else 'low'
end as awesomeness
Now the if statement is an entirely different beast. It is a control statement in MySQL procedures. The statement form looks like:
CREATE FUNCTION GetAwesomeness (username varchar(50))
RETURNS varchar(20)
BEGIN
IF username = 'darxysaq' THEN
return 'high';
ELSEIF username = 'john skeet' THEN
return 'medium';
ELSE
return 'low';
END IF;
END; //
Here's a SQL Fiddle with the statement version. It looks like Mr Bean isn't all that he's made up to be!
A final note: the case expression is standard SQL and works in most databases. The if function is not standard SQL and will not work in other databases, like SQL Server or PostgreSQL.
I'm using a case statement with MySQL.
When f.winner field is NULL, it returns "Loss" instead of f.method. So it seems as though the MySQL case statement can't handle blanks and nulls like this.
(
CASE f.winner
WHEN :fighter_id THEN "Win"
WHEN NULL OR "0" THEN f.method
ELSE "Loss"
END
) AS result,
Sometimes both f.winner and f.method are blank, and I just want the result to return as blank in those situations.
You could use IFNULL function and remove the NULL from your WHEN clause:
(
CASE IFNULL(f.winner, "0")
WHEN :fighter_id THEN "Win"
WHEN "0" THEN f.method
ELSE "Loss"
END
) AS result,
What I have is a bit column NonMileage and based on that bit column i want to make a variable that I can use inside of a where clause.
this is a two part question:
How do you case a variable? The code below does not case the
#NoMileageListing
And then I have it setting #MileListingClause as a string, can I
just use #MileListingClause like where #MileListingClause?
.
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT
#NoMileageListing CASE #NoMileageListing when 1 then
SET #MileListingClause = 'tblcargarage.miles >= 0' else
SET #MileListingClause = 'tblcargarage.miles != 0' end case;
here's the answer
SET #NoMileageListing = (SELECT NonMileage FROM tbldealerships);
SELECT CASE #NoMileageListing
WHEN 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
found here:
Mysql Storing a variable with the result of an SELECT CASE
I think the problem is located in the when part.
It should be:
SELECT CASE #NoMileageListing
WHEN #NoMileageListing = 1 THEN 'tblcargarage.miles >= 0'
ELSE 'tblcargarage.miles != 0'
END
INTO #NoMileWhereClause;
select #NoMileWhereClause;
to create the dynamic query with the #noMileWhereClause refer to the answer here:
How To have Dynamic SQL in MySQL Stored Procedure