MYSQL: Check if table exists does not work - mysql

IF OBJECT_ID(N`db_291702_2`.`aaCoRrankingDateManage`, N'U') IS NOT NULL
BEGIN
PRINT 'Table Exists'
END
What is wrong with this? Why do I get errors?
Neither of the suggested ways in how-to-check-if-a-table-exists-in-sql-server/ works for me.
PS. "does not work" means errors like
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 ''db_291702_2'.'aaCoRrankingDateManage' LIMIT 0, 30' at line 1
Additional info:
I am using phpMyAdmin, my two databases are called db_291702_1 and db_291702_2, the latter has two tables, one of them is called aaCoRrankingDateManage

If you want to escape table or column names then use backticks
select `group` from table1
A static string must be included in quotes
select * from users where name = 'john'
And the syntax of every DB engine is a little bit different. The above works for MySQL, but SQL-Server has a different syntax. There you use brackets [] to escape names.
But you only need to escape names if you use reserved words. You don't have to escape everything.

The given source code is no MySQL Code.

Related

mysql: Not able to create a table

create table 5390e910_abb3_40e2_bdfa_bd9d369e6dc6 like sample_table
is failing.
ERROR: #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 '5390e910_abb3_40e2_bdfa_bd9d369e6dc6 like
notification_sample_history' at line 1
Now I don't see any flaw with the above name as the mysql documentation says:
64 characters max
can start with any alphabet/digit
an contain underscores/alphabets/digits
Though I am able to run something like this:
create table 1f8b784f_f580_4a82_9e93_167a2d9c79f5 like sample_table
I believe it's the first "e". When I swap that character for any other letter, the query works just fine.
I'm not positive on this, but my suspicion is that MySQL is reading your string and considering it to mean the number "5,390 times 10 to the power of 910abb3[etc]". And while all-numeric table names are valid they must be quoted.
Try enclosing your table name in back-ticks. This works for me:
create table `5390e910_abb3_40e2_bdfa_bd9d369e6dc6` like abbreviations;

Find and replace with MySQL version 5.5.3

I am trying to find a weird character thats in front of my £ sign and replace it with nothing.
Coding I tried was
update [orders_total]
set [text]=replace([text],'[Â]','[]');
but mysql returns this
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 '[orders_total] set [text]=replace([text],'[Â]','[]')' at line 1
I know nothing of Mysql but its going to take me ages to manually remove these chars so any help would be much appreciated.
Thanks in advance
Thats not mysql syntax and in mysql it should be as
update orders_total
set text=replace(text,'Â','');
Abhik is absolutely right in his answer. An alternate form of writing MySQL queries is:
update `orders_total` set `text` = replace(`text`, '..', '...');
Backticks are not required. They may be valuable when a table's column is named order, for example. Order is a reserved keyword used in order by ... clause. In order to use a reserved keyword like order, use backticks.
Example:
select `order`, `id`, ... from `tablename` where .... order by `order`;

Syntacts error in mysql query

Someone gave me this query to delete community from a database on my server using phpMyAdmin, it worked when he use it so I asked him to send it to me, but I get a error
MySQL said:
Documentation
#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 '"SELECT * FROM `connections` WHERE 1" :
delete from connections where communit' at line 1
I did a search on the error but could not figure it out.
"SELECT * FROM `connections` WHERE 1" :
delete from connections where community="XYZ"
You should separate your queries with ; and not :
The error you are receiving as you've shown it is because this line is incorrect:
SELECT * FROM `connections` WHERE 1"
First because it's ending in an unnecessary double quotation mark. MySQL does not use double quotes but single quotes, and even still the other single quote is not there to match it.
The single quotes are also a problem for community="XYZ", this should read: community = 'XYZ'
Secondly, you don't have a condition for your where statement, you must be missing something like:
WHERE columnName = 1;
If you were trying to select everything from connections, you can just remove that where clause all together.
EDIT
In addition, MySQL queries are separated by a semi-colon, not a colon, so MySQL will not realize you are trying two different queries.

Correct format to use regex in MySQL SELECT statement (not within WHERE section)

I need to use REGEX (or similar functiuonality) to update an existing table in order to identify if a string field contains a correctly formatted code:
UPDATE tblRequests SET flagIsRefCodeOK=(RefCode REGEX '^[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}$') WHERE DataSetID=11
But it doesn't seem to like the statement:
#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 'REGEX '^[A-Z0-9]
Note, this code will be added to a BEFORE INSERT trigger, which is responsible for updating a number of flags. I'm not fussed (much) with whether the REGEX is correct, especially within MySQL, I just want it to try to work. Then I'll figure out the exact REGEX if this doesn't work.
Thnx
The operator name is REGEXP not REGEX, so try:
UPDATE tblRequests
SET flagIsRefCodeOK= (RefCode REGEXP '^[A-Z0-9]{8}-(?:[A-Z0-9]{4}-){3}[A-Z0-9]{12}$')
WHERE DataSetID=11;

EDMX With lambda expression with MySQL

I am using EDMX with MySql 5.1. It is working fine except When I try to execute the lambda expression, it shows me the following 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 '[XYZ].[UserID] AS [UserID], [XYZ].[FirstName] A' at line 17
where [XYZ] is the table name and [UserID], [FirstName] are the columns of that table. Following is the statement, that I want to execute -
_context.XYZSet.Where(org => org.ACDID == sbuID || !(org.ACDID.HasValue)).ToList();
Please help..
I do not know anything about EDMX, but from that error it looks like it's using MS SQL Server syntax to escape table and column names, which is not supported by MySQL. MySQL uses backticks for that, not square brackets.
If you could get EDMX to stop escaping the table and columns names then you might be okay, assuming none of the table/column names are reserved words.