" ( " not valid in this position, expecting an identifier in MySQL - mysql

Today is, 1/30/2022, I have been following along with an #AlexTheAnalyst video. I am on a Mac and using MySQL version 8.0.27. (The video is using windows based SQL Server Workbench) I am stuck! I am trying to creating a temporary table function. MySQL is not liking the # in the table name #PercentPopVaccinated as used in the video. When I remove it and run the function/query without the # I get 0 rows returned. I have researched on stackoverflow etc. and I am not coming up with a solution that I understand. (Newbie here)
I have dropped the table that was created and I am starting over.
I am getting an error when creating the temp table that states MySQL is expecting an identifier after the first " ( ". Anyone else have a similar issue?
Create Table #PercentPopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
RollingVacCount numeric
)
Insert into #PercentPopulationVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(cast(vac.new_vaccinations as UNSIGNED)) OVER (Partition by dea.location Order by dea.location, dea.date)
as RollingVacCount
-- (RollingVacCount/population)*100
From project_portfolio.covid_deaths dea
Join project_portfolio.covid_vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
-- order by 2,3
Select *, (RollingVacCount/Population)*100
From #PercentPopulationVaccinated;

So I'd say the underlying problem is that you are watching a video tutorial that is using SQL Server, but you are using MySQL. There are many similarities, but it is not going to be an exact match. For instance, the # sign creates a temporary table in Sql Server, but the # is not valid in MySQL. If you want to use a different database service than the tutorial you are watching is for, you are going to have to translate some concepts for yourself.
Another commenter already posted this link, which indicates the syntax for creating temp tables in MySQL.
https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-temporary-tables

Related

VS Crashes when using T-SQL OPENJSON and SELECT *

When using the below code the VS 2019 crashed without fail. Though it was originally working, possibly prior to a recent update (or upgrade to VS 2019 from VS 2017)
The error message implied a too long identifier and related to a specific file. I temp resolved this by accessing my project via Team Explorer as opposed to startup window. It resurfaced as soon as I edited the file. So I went line-by-line to find the culprit, the code below is the culprit. I cannot understand why it causes the crash.
DECLARE #Characteristics nvarchar(4000) = (SELECT * FROM OPENJSON(#Details)
WITH ([Firstname] nvarchar(256) N'$.firstname',[Lastname] nvarchar(256) N'$.lastname') FOR JSON PATH, WITHOUT_ARRAY_WRAPPER);
It's a legitimate command and SQL accepts including deployments. For my purposes and because I control the WITH clause, "SELECT *", is an acceptable deviation from my practice of stating the columns in the SELECT clause.
VS no longer crashes now that I declare each expected column in the SELECT clause. If somebody has an alternative resolution to solving this problem I would appreciate it.
You do not show enough - at least for me - to understand your issue completely...
What I get:
You want to use SELECT *
You are controlling the WITH clause (I assume: You build it dynamically in your application)
You did not show the actual error message (please do this the next time), but I get, that the error depends on the difference of
SELECT * FROM ...
SELECT Firstname, Lastname FROM ...
I cannot reproduce your issue, but I guess, that there are more sources involved in your actual statement and that * might include more than you tell us.
Did you try to use a table alias after the WITH?
DECLARE #Details NVARCHAR(MAX)=N'[{"a":"a1", "b":"b1"},{"a":"a2","b":"b2"}]';
SELECT
(
SELECT tbl.* --<-- Using "tbl.*" instead of "*"
FROM OPENJSON(#Details)
WITH(a NVARCHAR(100)
,b NVARCHAR(100)) tbl --<-- table alias "tbl"
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);

Prompt user to input a variable in MySQL

At school, I believe I work with Oracle SQL Developer when writing SQL. And in this I can type:
SELECT Book_Title, Auth_ID
FROM book
WHERE Auth_ID = '&Enter ID';
This will then display a little message box where the user can enter an ID number to see all the books written by an author with that ID number.
I want to know if there is a way to do this in MySQL. I have looked and the nearest thing I can find is setting a variable before hand, which is not quite what I'm looking for:
SET #EnterID := 2;
select Book_Title, Auth_ID
from book
where Auth_ID = #EnterID;
The above statement in MySQL will return all the books with author ID of 2, but only because I set it to that previously. I want the user to be able to enter the variable.
Thanks.
Oracle has the concept of interactive queries, those that as you said you can run by adding the '&' before your variables names, that is a variable substitution, this concept doesn't exist in MySql, MySql is not interactive and requires the user to enter the values in the variables by using the keyword 'SET' and # (instead of & like in Oracle).
So, no, you cannot do what you are looking for since this is not a client-side implementation either.
BTW, I just noticed this was asked so many years ago, amazing that this is still not added as a feature in mysql.
For a prompt, you must put the char ':' followed by the name of the variable
Example :
select *
from YOUR_TABLE
where YOUR_COLUMN = :your_var
mysql is to run SQL queries .SQL is a query language, it is not for user interaction
See : How to ask MySQL to prompt for values in a query?

How to find out the changes happened for all objects

How I can find out the changes happened in database like modifying functions, table indexes, procedures and adding or removing columns.
Here in this query
select * from sys.objects
where type IS NOT NULL
and modify_date between '2013-07-21' and '2013-07-29'
but here I am getting created objects list and modifying list, but if I deleted any object it is not showing anything.
How can I get the all the changes happened in database between specific dates?
Try a source control solution for SQL. I've used RedGate's SQL Source Control before, and it records a history of changes like this, including who made the change, and what was changed.
http://www.red-gate.com/products/sql-development/sql-source-control/
It's a bit expensive, but it's good. I don't know if there's a way to do it (especially deletions) just with SQL itself.
Many of these incidents are recorded in the default trace.
DECLARE #path NVARCHAR(260);
SELECT
#path = REVERSE(SUBSTRING(REVERSE([path]),
CHARINDEX(CHAR(92), REVERSE([path])), 260)) + N'log.trc'
FROM sys.traces
WHERE is_default = 1;
SELECT
LoginName,
HostName,
StartTime,
ObjectName,
TextData -- may or may not be populated
FROM sys.fn_trace_gettable(#path, DEFAULT)
WHERE EventClass IN
(
164, -- object:altered
46, -- object:created
47 -- object:deleted
)
AND StartTime >= '20130721' AND StartTime < '20130730';
Why you should never use BETWEEN for date range queries.

Select query returns false result

eg:
Table : user
column : user_id (type is int)
SELECT * FROM user WHERE user_id = '10xyz'
is giving same result of
SELECT * FROM user WHERE user_id = '10'
The input value is not integer but not giving an error in this case.
The reason why you are getting the same result is because MySQL automatically removes the trailing characters from the string and implicitly converts it to integer.
SQLFiddle Demo
SQLFiddle Demo (updated)
If you don't want to change all your code, but you have your database queries all going through one or a few subs, you can change those to check for warnings after using a statement handle (e.g. if ( $sth->{mysql_warning_count} ) ...).
Or you can create a DBI subclass that does that automatically for you, promoting warnings to errors. If you do, many others have use for such a thing. There are configuration settings to give an error instead of a warning when updating or inserting something like '10xyz' into an integer field, but not anything broader than that, and dear Oracle considers it Not a Bug. Maybe MariaDB does (or could do) better?
datatype of user_id is in database is INT
that why it giving same output and not error

MySQL Not Operator

I'm in the process of migrating a locally hosted MySQL database over to a cloud based MySQL database using Xeround. I'm running a test script that uses a left join to form a table and then runs two select statements
--one where the VAL and KVAL fields are equal and one that returns the complement of this set (where the VAL and KVAL sets are not equal).
I'm having no problems getting the records where VAL and KVAL match using (VAL=KVAL) as a where statement. I'm able to get the records where VAL=KVAL in both setups. I'm able to get the complement in my local setup using the where statement: VAL!=KVAL OR (KVAL IS NULL).
However, when I run this same Select/Where statement in my Xeround setup it returns a NULL set. The Xeround database uses PHP MyAdmin if that is helpful. I've also played around with <>, placing an exclamation mark or not statement outside of the original where statement. This should be fairly straight forward. Can you help me out?
The complement condition of
WHERE ( val = kval )
is:
WHERE ( val <> kval OR val IS NULL OR kval IS NULL )