1

I know that this might seem like a strange question, but let me try and explain it. I have a database table called 'plan' and in it the first column is called 'username' and the columns after it are called 'question1', 'question2' and so on. I now need to add a hundred or so more columns named like this, but it would be nice to have a sql statement that would automatically do that for me.

I know this wasn't set up in the best way, but if you have a solution, please let me know :)

EbilGenius
  • 69
  • 1
  • 9

2 Answers2

0

There isn't any SQL command or feature that would do this automatically; sure you can generate the alter table statements and add the columns programmatically; however, your design would be terribly flawed.

Instead of adding columns, you should create a table containing the question, the user_id (or username, whatever is the PK) to hold the records. If you need to identify a question by number (or ID), simply add another column called question_id.

Icarus
  • 63,293
  • 14
  • 100
  • 115
-1

Write the query in sql to excel. Seperate the incrementing number. Drag down until excel row 100. Hard to explain but i guess you ll figure it out. You'll have 100 incrementing add column sql statements. copy paste run it on a query tool.

erichste
  • 759
  • 4
  • 16
  • -1 I am sorry but this is a terrible advice. One does not create 100 columns in a table just because one needs to store 100 questions. – Icarus Jan 06 '13 at 03:29
  • He already admits that he isnt doing something normal. I m just focusing on solving his question. Should I suggest a book on RDBMS theory??? – erichste Jan 06 '13 at 03:34
  • Frankly, that'd be a better answer. The idea here is not to answer blindly because the user says he wants to do something -specially if it's clearly flawed,- but rather point him in the right direction or even suggesting him to read an article or a book on good practices. – Icarus Jan 06 '13 at 03:39