I have a stored procedure and a part of it is:
SET #TableAlias = " ls_flock AS _flk ";
SET #Select = CONCAT(
" _flk.Id as Id, _flk.Code as Code, ",
CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
" as FlockManager"
);
SET #Join = " JOIN employee as _em ON _flk.EmployeeId = _em.Id ";
I am unable to CONCAT two columns as a field with a CONCAT function.
And if I remove following line, code works fine:
CONCAT(" _em.FirstName", " ", "_em.LastName "), -- here I want to CONCAT 2 columns as one field.
" as FlockManager"
I'm getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.LastName as FlockManager FROM ls_flock AS _flk JOIN employee as _em ON _flk.' at line 1
I suspect that you want to generate a query string that uses CONCAT() - because I see that the table alias _em is declared further in the #Join variable. For this, you would need to move the function inside the query string.
SET #Select = CONCAT(
" _flk.Id as Id, ",
"_flk.Code as Code, ",
"CONCAT(_em.FirstName, "" "", _em.LastName) s FlockManager"
;
And then, you just don't need the outer CONCAT():
SET #Select =
" _flk.Id as Id, _flk.Code as Code, CONCAT(_em.FirstName, "" "", _em.LastName) as FlockManager";
Related
I am trying to multiply a row by a variable (calculated amount):
double servingsMultiplier = 1;
double servingSizeMultiplier = 1;
Calculate the values for "servingsMultiplier" and "servingSizeMultiplier".
String selectQry5 = ("SELECT ci_id, cr_id, ci_ingedient, (ci_amount*servingsMultiplier) AS ci_amount, " +
" (ci_unit*servingSizeMultiplier) AS ci_unit " +
" FROM at_cat_ingredient " +
" WHERE cr_id = ? " +
" ORDER BY ci_ingedient;");
The above works when I use a constant (e.g., 2); however, not when I use a variable. I get the error message:
"SQLException in recipePDF:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 'servingsMultiplier' in 'field list'.
An identifier like servingsMultiplier inside the sql statement is not recognized as the value of the variable but as a column name, which of course does not exist.
Use ? placeholders for servingsMultiplier and servingSizeMultiplier in the statement and pass their values just like you pass the parameter in the WHERE clause:
String selectQry5 =
"SELECT ci_id, cr_id, ci_ingedient, " +
"(ci_amount * ?) AS ci_amount, " +
"(ci_unit * ?) AS ci_unit " +
"FROM at_cat_ingredient " +
"WHERE cr_id = ? " +
"ORDER BY ci_ingedient;";
If you want to use mysql variables, then add # before variable name.
SELECT ci_id, cr_id, ci_ingedient, (ci_amount*#servingsMultiplier) AS ci_amount,
(ci_unit*#servingSizeMultiplier) AS ci_unit
FROM at_cat_ingredient
WHERE cr_id = #id
ORDER BY ci_ingedient;
I have an sql query that is Constructed as follows:
tmpQUERY = "INSERT INTO vertix_users (user_name, user_email, user_pass) " +
"VALUES (" + mysql.escape(userName) +
", " + mysql.escape(userEmail) +
", " + mysql.escape(userPass) + ")";
tmpQUERY = "MERGE vertix_users WITH (HOLDLOCK) AS oldData " +
"USING (VALUES (#user_name, #user_email)) AS newData (" + mysql.escape(userName) + ", " + mysql.escape(userEmail) + ") " +
"ON newData.user_name = oldData.user_name AND newData.user_email = oldData.user_email " +
"WHEN NOT MATCHED BY TARGET THEN " + tmpQUERY;
When I execute the code, I get the following log:
MERGE vertix_users WITH (HOLDLOCK) AS oldData USING (VALUES (#user_name, #user_email)) AS newData ('asfasfsaff vvv', 'asfasfasfasf') ON newData.user_name = oldData.user_name AND newData.user_email = oldData.user_email WHEN NOT MATCHED BY TARGET THEN INSERT INTO vertix_users (user_name, user_email, user_pass) VALUES ('asfasfsaff vvv', 'asfasfasfasf', 'saqqq')
And a sql syntax error that reads:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MERGE vertix_users WITH (HOLDLOCK) AS oldData USING (VALUES (#user_name, #user_e' at line 1]
A simple insert statement without the merge works fine, but as soon as I add the second portion of that code, it creates that error.
MERGE is not supported by MySQL, The equivalent for that is
INSERT ... ON DUPLICATE KEY UPDATE
I have a problem in this query:
string sqlString = "DELETE FROM [upload_news] WHERE (SELECT TOP " + no_of_recordss + " * FROM [upload_news] WHERE [country]='" + countryy.Text + "')";
Error Message :
Error: {"An expression of non-boolean type specified in a context
where a condition is expected, near ')'."}
How can i fix this ?
In the where clause you need a boolean expression.
Moreover, mysql doesn't support select top, you have to use limit instead and you can use it directly on delete
So your query should be:
delete from upload_news
where country=<SOME_COUNTRY> limit <NO_OF_RECORDS>
You have to replace values within "<>" with your desired values.
Or in your "strange" syntax:
string sqlString = "DELETE FROM [upload_news] WHERE [country]='" + countryy.Text + "' limit "+no_of_recordss;
I need help.... I am not good at SQL I get this error when I try to apply a JOIN:
[ Token line number = 1,Token line offset = 66,Token in error = JOIN ]
This is My SQL:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
"FROM Team" +
"LEFT JOIN Fixtures" +
"ON Team.TeamId=Fixtures.HomeTeam" +
"ORDER BY Team.TeamName";
Team Table Has PK: TeamId
Fixture Table Has FK: HomeTeam
I am using WebMatrix 2. Razor WebPages
No spaces between line concatenations. Change every line to include space at the end.
var query = "SELECT Team.TeamName, Fixtures.HomeTeam " +
"FROM Team " +
"LEFT JOIN Fixtures " +
"ON Team.TeamId=Fixtures.HomeTeam " +
"ORDER BY Team.TeamName";
As pointed by Charles Brentana, you have missed the spaces in your SQL command.
Maybe a better solution is to you use a verbatim string literal, i.e. a string created with an # character before the double-quote character, that can span multiple lines:
var query = #"SELECT Team.TeamName, Fixtures.HomeTeam
FROM Team
LEFT JOIN Fixtures
ON Team.TeamId=Fixtures.HomeTeam
ORDER BY Team.TeamName";
You need spaces between your strings.
I avoid this by putting the space as the first character, so it's really obvious when you forget to code it:
var query = "SELECT Team.TeamName, Fixtures.HomeTeam" +
" FROM Team" +
" LEFT JOIN Fixtures" +
" ON Team.TeamId=Fixtures.HomeTeam" +
" ORDER BY Team.TeamName";
If you consistently code this way you'll be able to spot any missing spaces instantly.
I am trying to add the group_concat function to hsqldb so that I can properly test a query as a unit/integration test. The query works fine in mysql, so I need it to work in hsqldb (hopefully).
// GROUP_CONCAT
jdbcTemplate.update("DROP FUNCTION GROUP_CONCAT IF EXISTS;");
jdbcTemplate.update(
"create aggregate function group_concat(in val varchar(100), in flag boolean, inout buffer varchar(1000), inout counter int) " +
" returns varchar(1000) " +
" contains sql " +
"begin atomic " +
" if flag then" +
" return buffer;" +
" else" +
" if val is null then return null; end if;" +
" if buffer is null then set buffer = ''; end if;" +
" if counter is null then set counter = 0; end if;" +
" if counter > 0 then set buffer = buffer || ','; end if;" +
" set buffer = buffer + val;" +
" set counter = counter + 1;" +
" return null;" +
" end if;" +
"end;"
);
Adding this aggregation function solves most of the problem. It will correctly behave like mysql's group_concat. However, what it won't do is let me use the distinct keyword like this:
group_concat(distinct column)
Is there any way to factor in the distinct keyword? Or do I rewrite the query to avoid the distinct keyword altogether?
HSQLDB has built-in GROUP_CONCAT and accepts DISTINCT.
http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_aggregate_funcs
At the moment you cannot add DISTINCT to a user-defined aggregate function, but this looks like an interesting feature to allow in the future.