Extremely simple question. I need to alter a bunch of tables in a DB to add a new column. This is my code:
# the maximum character length we allow for chat is 14
SET #maxlen = 14;
alter table tbl1 add chat varchar(#maxlen);
alter table tbl2 add chat varchar(#maxlen);
alter table tbl3 add chat varchar(#maxlen);
alter table tbl4 add chat varchar(#maxlen);
alter table tbl5 add chat varchar(#maxlen);
I get the 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 'maxlen)' at line 1
It says line 1 in the error code, but the error is happening in line 5, which is the first alter statement. How can I do what I'm looking to do here?
What you ask for requires dynamic SQL.
Consider:
set #maxlen = 14;
set #sql = concat('alter table tbl1 add chat varchar(', #maxlen, ')');
prepare stmt from #sql;
execute stmt;
deallocate prepare stmt;
Related
I am trying to add new event to rename a table in my DB use by phpmyadmine, and it will give me the errors :
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near SET #tablename = 'Today Raw Data';SET #newname = ' raw Data'; SELECT #query' at line3
my code is :
select #yesterday := date_sub(curdate(),interval 1 day);
SET #tablename = 'Today Raw Data';
SET #newname = ' raw Data';
SELECT #query := CONCAT('RENAME TABLE `', #tablename, '` TO `',#yesterday,#newname, '`');
PREPARE STMT FROM #query;
EXECUTE STMT;
CREATE TABLE alter (first_day DATE, last_day DATE);
How to fix it: Just because the word alter is reserved does not mean it cannot be used, it just has special requirements to use it as the MySQL engine is trying to call the functionality for the alter command. To fix the issue, you will want to surround the word with backticks ``, this is usually the button just to the left of the "1" button on the keyboard. The code block below shows how the code will need to look in order to run properly.
CREATE TABLE `alter` (first_day DATE, last_day DATE);
Missing Data
Sometimes data in the database is missing. This can cause issues when this data is required for a query. For example, if a database is built requiring an ID number for every student, it is reasonable to assume a query will be built to pull a student record by that ID number. Such a query would look like:
SELECT * from students WHERE studentID = $id
If the $id is never properly filled in the code, the query would look like this to the server:
SELECT * from students WHERE studentID =
www.google.com.tr/
First I create my table with one text column in 5.6.34 - MySQL Community Server (GPL) like so :
CREATE TABLE `$_query_test`.`t1` ( c1 TEXT NOT NULL ) ENGINE = InnoDB;
Then I execute:
SET #var = 321; INSERT INTO `t1` (c1) VALUES (#var);
I get one entry with a value of 321 as expected, but when i execute:
SET #var = '_string_';
ALTER TABLE `t1` ADD #var TEXT;
I get an 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 '#var TEXT' at line 1
What is the proper syntax to do something like this? I have searched far and wide and so far got nothing on this.
The answer is that you cannot substitute an sql variable in place of an identifier (e.g. field name, table name, etc). Period.
What you can do is to create the full sql statement as a string either on application level or in mysql with the identifier already substituted and you execute the full statement.
In mysql you can do this via prepared statements:
PREPARE stmt1 FROM CONCAT('ALTER TABLE `t1` ADD ', #var, ' TEXT');
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
You can use variables for values but not field names. You will need to do that with whatever server language you are using before composing the alter table.
I want to set the auto_increment value for each column by first finding the max id value.
I am referencing the code from this SO question.
The mysql docs for prepared statements show a similar format, so I am confused.
When I try running the prepare statement I get a failure. Why?
Below is the output when I try to prepare a regular statement and then when I try to prepare the auto_increment statement with a '?' for binding later.
mysql> PREPARE stmt1 FROM 'ALTER TABLE user AUTO_INCREMENT=2';
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> PREPARE stmt1 FROM 'ALTER TABLE user AUTO_INCREMENT=?';
ERROR 1064 (42000): 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 '?' at line 1
An alternative would be
set #alter_statement = concat('alter table user auto_increment = ', #value);
prepare stmt1 from #alter_statement;
execute stmt1;
deallocate prepare stmt1;
For some reasons it seems many people are experiencing a syntax error when using prepare with ?.
My guess is that it fail because the given value will be replaced with the value, between two single quotes (this is just a guess though).
long time user, first time poster.
I have 2 tables;
a1_watchlists {id(PK),name,date}
a1_watchlist {id(PK),watchlists_id(FK(a1_watchlists.id)),company_name,asx_code,date}
I also have 2000 other tables that have been created with the name 'asx_'+[asx_code] (where asx_code is pulled from another table)
this table looks like;
asx_[asx_code] {date(PK),open,high,low,close,volume}
I want to select all from a1_watchlists and a1_watchlist and then select the latest date from the asx_[asx_code] table using the value from a1_watchlist.asx_code to generate the [asx_code] part of the table name.
The problem I have is that I want to use the value from a1_watchlist.asx_code as the table name prepending the string 'asx_' to this first.
Closest I have been able to get is;
DECLARE #TableName VARCHAR(100)
SELECT *
FROM a1_watchlist AS wl
JOIN a1_watchlists AS wls
ON wls.id = wl.watchlists_id
SET #TableName = 'asx_' + wl.asx_code
INNER JOIN (SELECT MAX(date),open,high,low,close,volume,amount_change,percent_change FROM #TableName)
This currently give the 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 'DECLARE #TableName VARCHAR(100)
SELECT *
FROM a1_watchlist AS wl
' at line 1
The expected colums I need in the final result would be:
wl.id,wl.watchlists_id,wl.company_name,wl.asx_code,asx_[asx_code].date,asx_[asx_code].open,asx_[asx_code].high,asx_[asx_code].low,asx_[asx_code].close,asx_[asx_code].volume
Let me know if you require more information.
I'm not going to speak to what to do in the case where you have 2000+ tables that start with asx+ some code... (i live in a town with multiple bridges) or even whether what you're doing is the best way to get where you want to go. BUT, it does look like you're attempting to concatenate things together and create a dynamic statement. If that sounds right, then I'd recommend you look into prepared statements. Like the following. Hope this helps.
DELIMITER $$
DROP PROCEDURE IF EXISTS prRetrieveAllFromTable$$
CREATE PROCEDURE prRetrieveAllFromTable(tableName VARCHAR(64))
BEGIN
SET #s = CONCAT('SELECT * FROM ',tableName );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
CALL prRetrieveAllFromTable('calendar');
http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-prepared-statements.html
How To have Dynamic SQL in MySQL Stored Procedure
I'm trying to rename a list of tables using a constant prefix defined as a #var:
SET #p='newprefix_';
RENAME TABLE `oldprefix_tablename1` TO CONCAT(#p, 'tablename1');
RENAME TABLE `oldprefix_tablename2` TO CONCAT(#p, 'tablename2');
This syntax is wrong, but I see that:
SELECT CONCAT(#p, 'tablename'); //outputs newprefix_tablename
What's the correct way/syntax to use here?
You can't do it directly the way you are trying. As the manual says (http://dev.mysql.com/doc/refman/5.1/en/user-variables.html)
User variables are intended to provide data values. They cannot be
used directly in an SQL statement as an identifier or as part of an
identifier, such as in contexts where a table or database name is
expected, or as a reserved word such as SELECT.
You have to use prepared statements:
SET #p = 'newprefix_';
SET #s = CONCAT('RENAME TABLE `oldprefix_tablename1` to ', #p, 'tablename1');
PREPARE stmt FROM #s;
EXECUTE stmt;