Multiple condition in if....else inside stored procedure - mysql

IF region='Mumbai' OR region='Chennai' OR region='Bangalore' OR region='Pune' region='Coimbatore' OR region='Ahmedabad' THEN
SET region='South-west';
ELSE
SET region='North+East';
END IF;
I am trying to create a sp i have error in the above the Condition. rest of the Stored procedure is working fine when i remove the above if condition.
i am getting the error for multiple or condition in if statement.
please help.

You are missing an OR. Try:
IF region='Mumbai' OR region='Chennai' OR region='Bangalore' OR region='Pune' OR region='Coimbatore' OR region='Ahmedabad' THEN
Also, I think an IN would work here as well (I'm not 100% sure since I don't use MySQL regularly):
IF region IN ('Mumbai','Chennai','Bangalore','Pune','Coimbatore','Ahmedabad') THEN
That would be much more readable, if MySQL is happy with it.

Related

Symfony 2 - Doctrine ORM Update query not working

I am trying to update a mysql table with following query using Doctrine. But the table is not get updated. Also below code didnt throw any error. I am totally confused. If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes. it is working after placed proper qoutes for values in the query. Need help to solve this puzzle.
Since i am new to doctrine, i will use the examples give in querybuilder class file.
$support = $this->createQueryBuilder('p')
->update('gcns', 'g')
->set("g.isActive", "0")
->andWhere("g.issn='".$issn."'");
Do you ever execute the query or are you just building it? You should have something along these lines to execute it:
$support->getQuery()->getSingleScalarResult();
If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes.
getDQL() returns DQL not SQL, so it will have improper quotesif you try to run it directly inside MySQL, but that's expected.
You shouldn't concatenate $issn into your query. You should use parameters instead:
$qb = $this->createQueryBuilder()
$support = $qb->update('gcns', 'g')
->set('g.isActive', '0')
->andWhere( $qb->expr()->eq('g.issn', ':issn') )
->setParameter( 'issn', $issn )
->getQuery()->getSingleScalarResult()
;

Condition check with IN clause mysql

I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.

SQL Update returning zero rows

UPDATE starfsfolk
SET starfsfolk.stada=2
WHERE starfsfolk.deild LIKE '%Hugbúnaðardeild%';
UPDATE starfsfolk
SET starfsfolk.stada=3
WHERE starfsfolk.deild LIKE '%Markaðsdeild%'
is prescisely the code i'm using.
i've tried various different versions of it(like = "Markaðsdeild" or LIKE "Markaðsdeild")
most of which work if i'm using select, but i needed to use update and its not working for some reason.
This WHERE command works on select but
it returns zero rows if i'm using the update command. What am i doing wrong?
Edit:
Just to clarify, stada is set to 1 in all cases before and after the update command. it hasn't changed from 1 to 2 and 3 like i wanted it to.
Edit2: heres a screenshot of the database, i could also give you the create commands.
Edit3: Stada is bit, not int, i found out, not sure what that changes tho.
Solution: Solved myself, since bit is just 1 and 0, the error was in the creation of the table so i remade it with stada as int and now the code is working.
If the value of stada is not changed by the query then zero rows will be returned because nothing was updated.
The character sets of the server and of the client are different.
http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html

How to set mysql user variable when report loading Birt

Im using birt reporting tool since from year. its cool product very good.
Today i got a problem. that i want to set mysql user varible when report loading as a example SET #user="USR009";
I tried several ways to do this i couldnot get the answer could some one help me on this issue very thank full
im stuck on this problem
regards
roshan
You can set the value in the query using a report parameter, and initialize it in the script.
Add a report parameter (PARAMETER_1)
Add "SET #user = QUERY_PARAMETER;" before your select statement in your data set.
While the data set is selected, select the script tab and find the "beforeOpen" drop-down. In here, add a line: this.queryText = this.queryText.replace("QUERY_PARAMETER", params["PARAMETER_1"].value);
The script will replace all instances of QUERY_PARAMETER with the text. You might still need quotes around it in the set statement (i.e SET #user = "QUERY_PARAMETER";).
Problem Was resolved create a SP
delimiter //
Create PROCEDURE abcd(param INT)
BEGIN
SET #USER=param;
SELECT #USER;
END//
And in your query call the SP with varible;
works like charm

Create stored procedure with CONTAINS in SQL Server 2008

I want to create a stored procedure to do some combined keyword search using CONTAINS,something like below:
SELECT theContent
FROM FtsTest
WHERE CONTAINS
(theContent,
' FORMSOF (INFLECTIONAL, keyword1) AND FORMSOF (INFLECTIONAL, keyword2)');
and he number of keywords may vary, so I tried to pass the whole 'FORMSOF... AND FORMSOF.....'clause as a parameter,declaring the parameter as nvarchar(max),but it won't let me do it,saying The argument type "nvarchar(max)" is invalid for argument 2 of "CONTAINS".
So, is there any way to make it work with the sp?
Thanks!
Just declare argument 2 of contains as nvarchar(4000) instead of nvarchar(max) and it will work.
See the difference here: https://msdn.microsoft.com/en-us/library/ms186939.aspx
2 GB is a bit too much for the search expression.
this seems stupid,but using nvarchar(500) instead of nvarchar(max), Sql Server cheerfully accepts it and works just fine.
Still trying to gain some insight on sp_executesql,thanks.
You could build it dynamically and pass in the keywords as parameters. Executing with sp_executesql allows you to take advantage of the query plan cache, as described in the answers to this question:
Alternative to executing dynamic sql
You may need to watch out for this issue, though, that relates to parameter sniffing and full text queries:
http://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=510118