my sql use a variable in the ALTER TABLE ADD COLUMN statement
Asked Answered
A

3

8

Hi I am looking to create a table with date as column name.

I am using this code to Add column to the table:

DROP PROCEDURE IF EXISTS filldates;
DELIMITER |
CREATE PROCEDURE filldates(dateStart DATE, dateEnd DATE)
BEGIN
  WHILE dateStart <= dateEnd DO
    ALTER TABLE dates
    ADD dateStart VARCHAR(30);

    SET dateStart = date_add(dateStart, INTERVAL 1 DAY);
  END WHILE;
END;
|
DELIMITER ;
CALL filldates('2017-01-01','2017-12-31');

But it is showing the error dateStart as duplicate content, because it creates a column 'dateStart' instead of date. How can I use dateStart as a varible.

When I use "INSERT INTO tablename (_date) VALUES (dateStart);" instead of ALTER TABLE statement, it doesn't show any error and it is inserting the dates to database, but as rows in '_date' column. I want the dates to be added as column name.

How can use dateStart as a variable

Accalia answered 8/5, 2017 at 13:33 Comment(1)
This is going to be a bad design of your table.Dative
S
14

The short answer is that you cannot use variables as database, table, or column names in MySQL.

The only thing that you can do is to concatenate the sql statement as string and execute it as a prepared statement.

SET @s=CONCAT('ALTER TABLE dates ADD COLUMN `',dateStart,'` VARCHAR(30)');
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

However, adding field names dynamically in large quantities to a table usually indicates a bad design (unless your are preparing materialised pivot tables for reporting purposes out of data that cannot be changed).

Soothsay answered 8/5, 2017 at 13:53 Comment(1)
just had to change @s1 in line 2 of your code to @sAccalia
D
2

This going to be Add Column of your Table but this is not a good idea.

set @datestart = "2017-01-03";
set @sql = concat('Alter table tbl_1 Add Column `' ,@datestart, '`Varchar(30)');

PREPARE stmt from @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

If you wanted a loop then use Cursor

Dative answered 9/5, 2017 at 1:54 Comment(9)
1) no explanation as to why to use the above code 2) the code is the same as I proposed, so adds no additional value 3) cursors are used for looping through the results of a select. There is no need to use one here.Soothsay
Before i do this i use to test your code because i am wondering you are not using @ sign.. And the result of my test is return me an error.. that's why i generate another one. Do you want me to remove this post?Dative
I did not use a @ because datestart is a parameter to a stored proc in the question. No need to use a @ in that case.Soothsay
also your variable @s become @s1 in prepare statement.Dative
Never realized that is for stored proc.. It's my mistake. That's is why i didn't do any comment at your post. i'll just test it. Then create another one.Dative
I think this is just another option for the future user of this page. By using variable.Dative
The parameter is a variable.Soothsay
I guess your right but parameter maybe compose of variable and a variable couldn't be called as a parameter anymore.Dative
But in Declaration and using both is different. Am i right? I know your better than me. You can correct me if im wrong.Dative
A
-1

I have the same issue, and this worked for me.

declare @colname varchar (128)
set @colname=convert(varchar,DATEPART(DAY,GetDate()))+'\'+convert(varchar,DATEPART(MONTH,GetDate()))+convert(varchar,DATEPART(YYYY,GetDate()))
exec ('ALTER TABLE temp ADD ['+@colname +'] int NULL')
Adventurous answered 16/2, 2021 at 15:28 Comment(1)
Did you test this in MySQL? That's the database tagged in the question. I think your syntax would work in Microsoft SQL Server, but MySQL has different syntax for DECLARE, no EXEC function, and does not use square-brackets as identifier delimiters.Enzymolysis

© 2022 - 2024 — McMap. All rights reserved.