Convert SQL Server Query to MySQL
What do I need to do to convert this query to work within in MySQL?
Thank you for help.
[SQL] DECLARE
@CountOfEmp INT = (
SELECT
COUNT (*)
FROM
names
)
;
[Err] 1064 - 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 'DECLARE
@CountOfEmp INT = (
SELECT
' at line 1
[SQL] DECLARE
@CountOfEmp INT = (SELECT COUNT(*) FROM `names`) WITH GroupsOfWeeks AS (
SELECT
*, n = 1 + (DATEPART(WEEK, Dates) - 1) %@CountOfEmp
FROM
tbl_dates
) SELECT
Dates,
DayDates,
`names`
FROM
GroupsOfWeeks
INNER JOIN `names` ON n = id
ORDER BY
Dates;
[Err] 1064 - 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 'DECLARE
@CountOfEmp INT = (SELECT COUNT(*) FROM `names`) WITH GroupsOfWeeks AS' at line 1
BEGIN
DECLARE CountOfEmp CURSOR FOR SELECT COUNT(*) FROM `names`;
OPEN CountOfEmp;
CLOSE CountOfEmp;
END
And error is:
BEGIN
DECLARE CountOfEmp CURSOR FOR SELECT COUNT(*) FROM `names`;
[Err] 1064 - 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 'DECLARE CountOfEmp CURSOR FOR SELECT COUNT(*) FROM `names`' at line 21
The above URL is utilizing DELIMITER, although I have no idea why that may be necessary, let alone when. Sorry, I’m not 100% sure what the issue could be at this point.
mysql> DELIMITER //
DROP PROCEDURE
IF EXISTS prc_test //
CREATE PROCEDURE prc_test ()
BEGIN
DECLARE
CountOfEmp INT;
SET CountOfEmp := (SELECT COUNT(*) FROM `names`);
SELECT
CountOfEmp;
WITH GroupsOfWeeks AS (
SELECT
*, n = 1 + (DATEPART(WEEK, Dates) - 1) %CountOfEmp
FROM
tbl_dates
) SELECT
Dates,
DayDates,
`names`
FROM
GroupsOfWeeks
INNER JOIN `names` ON n = id
ORDER BY
Dates;
END //
DELIMITER;
CALL prc_test ();
Query OK, 0 rows affected
1064 - 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 'GroupsOfWeeks AS (
SELECT
*, n = 1 + (DATEPART(WEEK, Dates) - 1) %CountOfEm' at line 9
My problem is convert this part of code SQL Server query to MySQL:
WITH GroupsOfWeeks AS (
SELECT
*, n = 1 + (DATEPART(WEEK, Dates) - 1) %@CountOfEmp
FROM
tbl_dates
) SELECT
Dates,
DayDates,
`names`
FROM
GroupsOfWeeks
INNER JOIN `names` ON n = id
ORDER BY
Dates;
because the variable is correct value in the sp.
mysql> DELIMITER //
DROP PROCEDURE
IF EXISTS prc_test //
CREATE PROCEDURE prc_test ()
BEGIN
DECLARE
CountOfEmp INT;
SELECT @CountOfEmp := COUNT(*) FROM names;
SELECT @CountOfEmp ;
END //
DELIMITER;
CALL prc_test ();
Query OK, 0 rows affected
Query OK, 0 rows affected
+-------------------------+
| @CountOfEmp := COUNT(*) |
+-------------------------+
| 5 |
+-------------------------+
1 row in set
+-------------+
| @CountOfEmp |
+-------------+
| 5 |
+-------------+
1 row in set
Query OK, 0 rows affected
I might be wrong, but wouldn’t that be the same as
SELECT
Dates,
DayDates,
`names`
FROM
(SELECT
*, n = 1 + (DATEPART(WEEK, Dates) - 1) %@CountOfEmp
FROM
tbl_dates)
INNER JOIN `names` ON n = id
ORDER BY
Dates;
you have certainly right… but the problem is my teacher of relational database management system…
I have to submit this examination for version SQL Server and version for MYSQL
i can’t remember, but in all of these threads on various forums that you have posted, did you ever mention that it was a school assignment when you asked people to solve your problems?
this is the first case and I did not because it is shame for me, I’m sorry
other discussions concerning personal experiments nothing to do with the school assignment.
however, I understand the problem and found the solution, this version is working.
Now the output in SQL server is 251 records, in mysql the output is 251 records.
I change this line:
1 + (DATEPART(week, Dates) - 1)
to:
1 + (EXTRACT(WEEK FROM `dates`))
thank you.
DELIMITER //
DROP PROCEDURE
IF EXISTS prc_test//
CREATE PROCEDURE prc_test ()
BEGIN
DECLARE
CountOfEmp INT ; SELECT
@CountOfEmp := COUNT(*)
FROM
NAMES ; SELECT
@CountOfEmp ; SELECT
`dates`,
`names`
FROM
(
SELECT
*, 1 + (EXTRACT(WEEK FROM `dates`)) %@CountOfEmp
FROM
`dates`
) AS GroupsOfWeeks
INNER JOIN `names` ON 1 + (EXTRACT(WEEK FROM `dates`)) %@CountOfEmp = `names`.id
ORDER BY
`dates` ;
END//
DELIMITER ;
CALL prc_test ();
To be fair though, I do appreciate that cms9651 has consistently shown his attempts at solving said problems instead of just asking for answers (granted this particular thread doesn’t exactly fit that first statement, but he did later get himself to a closer working solution).
@cms9651; , I’m not 100% certain, as I really think there is missing information that I would need to make this assessment, but my guess is because the first of January is on a Tuesday (the first business day being Wednesday), it is excluding the first work week in 2013 and starting with the first full work week.
Since I am uncertain with how your logic is determining what is considered the first work week of a year, let alone, how it is filtering down to only show dates from 2013, I can’t tell where this error is located within your procedure. Can you provide more information or possibly look into what in your project may be excluding the first week of 2013 since it starts on a Wednesday and not on Monday?
Dope!, I missed the fact that you posted that. Cool, glad it was something simple, those are always the hardest to usually track down, but its great knowing you don’t have to rework the entire query.