db2 stored procedure if else variable comparison - sql

I am trying to compare a variable in db 2 sp using like, however it always goes to the else part of the statement, can someone correct the syntax here..here is the part of the code
do
IF(#variable like '%abc') THEN
set #anotherVariable='abc';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSEIF (#variable like '%def') THEN
set #anotherVariable='def';
ELSE
set #anotherVariable='xyz';
END IF;
END FOR;
This code is part of a cursor, query always returns 1 value, however my comparison is not working(incorrect syntax?), it always goes to the last else as if it never was able to match. I know that value is there but its not comparing in this manner...Thanks

Are you writing SQL-PL code? Because variables are referenced directly by its name, not preceded by the '#' sign.
Another thing is that you are using the like operator outside a query. 'Like' is not a function, it is a predicate: http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000751.html
Instead, you can use a 'case' in the select and then return the corresponding value. In that way you do not need to do an 'if-else': http://pic.dhe.ibm.com/infocenter/db2luw/v10r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0023458.html

Related

How to select characters between wildcards in Azure Data Factory expression for Switch Block

I have 2 pipelines that are currently selected based on an IF condition, which works well. I now need to add a third pipeline , so using a Switch block instead. And instead of tablename as the deciding factor, ideally I would use logic something like this:
does the sourcetablename value contain {FromDate} then goto pipeline A
does the sourcetablename value contain {passinid} then goto pipeline B
else goto pipeline C
The sourcetablename value comes with the initial gettablestoload lookup.
An example sourcetablename value is used for the API call in this instance, and would look like this for example: blahblah/content/{passinid}/user/blahblah
I am struggling with the expression for the switchblock. Previously I have matched on the last 10 characters of the tablename, this just seems a little bit tricky.
Here is an example of an expression to remove the last 46 characters, just to give you an idea where I am struggling up to:
#substring(activity('GetTablesToLoad').SourceTableName,0,sub(length(activity('GetTablesToLoad').SourceTableName),46))
Would anyone have an idea please?
If this was SQL it would be something like this:
DECLARE #text VARCHAR (500) = 'content/{passinid}/user'
SELECT SUBSTRING(#Text, CHARINDEX('{', #Text)
, CHARINDEX('}',#text) - CHARINDEX('{', #Text) + Len('}'))
thankyou
You can use if function to check for required value directly. I have taken a parameter called sourceTableName for demonstration instead of lookup value.
I have used the following dynamic content as expression value for switch activity. You can replace the pipeline().parameters.sourceTableName with lookup activity SourceTableName value. The name of switch case cannot start with { so, I have directly taken the name.
#if(contains(pipeline().parameters.sourceTableName,'{passinid}'),'passinid',if(contains(pipeline().parameters.sourceTableName,'{FromDate}'),'FromDate','execute default'))
When the parameter value is blahblah/content/{passinid}/user/blahblah, the corresponding set variable3 is executed.
When the parameter value is blahblah/content/{FromDate}/user/blahblah, the corresponding set variable4 is executed.
If these values are not present (blahblah/conten/user/blahblah), then the default case would be executed (set variable2 activity).

Can I use different selection comparisons based on a passed value in SQL?

I wasn't really sure of the best wording for the question but here is my dilemma:
I am passing a value to a sql query as #district. This value may be the exact district but it also has the possibility of being a value that should create a set of multiple districts. So if I pass 002 I want the WHERE clause to say I.Offense_Tract = #district. If I pass Other I want the WHERE clause to say I.Offense_Tract in (). What I am trying to do is something like:
AND
CASE
WHEN #district = "Other" THEN I.Offense_Tract in ('BAR','COL','GER','MEM','MIL','JAIL','JAILEAST','SCCC','1USD','2USD')
ELSE I.Offense_Tract = #district
END
But this doesn't work. The problem, restated, is if the value passed is anything other than Other, I just want it to be =. If Other is passed, I want it to be IN.
You don't need the CASE expression.
You can apply this logic with operators AND and OR:
AND (
(#district = 'Other' AND I.Offense_Tract IN ('BAR','COL','GER','MEM','MIL','JAIL','JAILEAST','SCCC','1USD','2USD'))
OR
(#district <> 'Other' AND I.Offense_Tract = #district)
)
Note that, in databases like MySql, Postgresql and SQLite, your code would work as it is.

Defining the (alias) name of a field

I've been looking over some code in an old Classic ASP system of ours that builds its own SQL within the stored procedure and then executes it {shudders}.
Several of the SELECTion lines contain an assignment, similar to:
SELECT
my_field = CASE WHEN value = whatever THEN 1 ELSE 0 END
...
Is there any difference (or anything I need to be aware of) between this and using a standard AS alias?...
SELECT
CASE WHEN value = whatever THEN 1 ELSE 0 END AS my_field
...
No, the following code is all synonymous:
SELECT one = 1;
SELECT 1 one;
SELECT 1 AS one;
SELECT 'one' = 1; --this is deprecated, don't use it.
Which you use (apart from the last), is normally down the preference. Personally, I use AS. One reason is I can then easily tell queries that return datasets, and those that assign values to variables a part.
The 2 examples that you have given are identical. However, when you go through the old code you might also find a variant with an # sign before my_field, like this:
SELECT
#my_field = CASE WHEN value = whatever THEN 1 ELSE 0 END
In this case a varable called #my_field is assigned a value, but nothing is SELECTed. This you can not rewrite to the other syntax using AS #myfield.

How can I add a where statement on list conditionally in report query?

How can I add a where statement in iReport query with a condition that a list is not empty:
WHERE
CASE WHEN length($P{list}) > 0 THEN table_x.id IN ($P!{list}) END
I tried to display the report passing an empty list, but it does not work.
Jasper report has it's own query system and I presume your $P{list} is a class that extends java.util.Collection for example a java.util.List, since you state "passing an empty list"
To create a prepared statement IN query on Collection in jasper report you use:
WHERE $X{IN, table_x.id, list}
Do note that if list is null or it is an empty list this query will return all records, see using-parameters-queries
JasperReports handles special characters in each value. If the parameter is null or contains an empty list, meaning no value has been set for the parameter, the entire $X{} clause is evaluated as the always true statement “0 = 0”.
To not show any records, you need to add at least one null value to the List in java before you pass parameter to jasper-report.
if (list.isEmpty()){
list.add(null);
}
Please also note that you should avoid using $P!{param} since this creates
an sql through string concatenation an open your application to SQL injection attacks, always try to use prepared statement instead.
Your ELSE clause is NULL, and that is false. I would recommend avoiding the CASE:
WHERE length($P{list}) = 0 OR table_x.id IN ($P!{list})
If i understood correctly, try using this:
WHERE (CASE WHEN length($P{list}) > 0 THEN table_x.id IN ($P!{list}) END) <> ''

Set blank result equal to specific string in SQL Server

I have a query that returns a result set which contains a certain column that needs some tweaking. Basically, in the result set, there are certain rows that contain a blank value for the applicable column. What I need to do is set all instances of that blank value to a specific string. I have tried declaring a variable and setting the variable equal to the column name (using a SELECT statement) and then using an IF statement to set the value to a specific string if it is blank (' '). My code thus far is as follows:
declare #sourceNode varchar(30)
set #sourceNode = (select sn_name from pt_cust)
if #sourceNode = '' begin
set #sourceNode = 'None'
end
This code returns an error stating that the sub-query returns more than 1 value. This seems like an easy task but I am stuck at the moment. How can this be accomplished?
This is a case (heh) for CASE:
SELECT CASE WHEN sn_name = '' THEN 'None' ELSE sn_name END
FROM pt_cust
You are getting the error message you mentioned because your SELECT statement can return more than one row, in which case it can not be assigned to a variable.