9

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

1
  • This is going to be a bad design of your table. Commented May 9, 2017 at 1:50

3 Answers 3

16

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).

Sign up to request clarification or add additional context in comments.

1 Comment

just had to change @s1 in line 2 of your code to @s
3

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

9 Comments

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.
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?
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.
also your variable @s become @s1 in prepare statement.
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.
|
-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')

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.