0

I have an existing table called temp_09.jwn. I would like to add a new column called cobrand_bank_id. Is there a way where I can skip the ALTER TABLE step below and just directly write the insert into statement?

ALTER TABLE temp_09.jwn
ADD cobrand_bank_id int;

insert into temp_09.JWN(qqyy, cobrand_bank_id, sum)
ZJAY
  • 2,517
  • 9
  • 32
  • 51
  • 6
    Without adding a column how would you expect to store data in it – Pரதீப் Jan 08 '17 at 02:05
  • 1
    @ZJay can you explain why you want to skip the `alter table` step? Are these statements part of a repeating code so you end up running the `alter table` statement twice. If so, you might 1. move `alter table` outside the repeating code, or 2. check if the column exists and run `alter table` only if the column does not exist. See [how to check if column exists](http://stackoverflow.com/questions/133031/how-to-check-if-a-column-exists-in-sql-server-table) – HappyTown Jan 08 '17 at 02:44

2 Answers2

0

No, you have to add the column first. Schema-less databases (NoSQL) can support this, but an RDBMS needs to have its scheme altered.

It's kind of like saying, "I bought these new shoes and I need a bin to store them, if I just toss them in the corner will a bin appear?" No, you have to get the bin first.

Josh Miller
  • 440
  • 3
  • 11
0

There are several options to achieve schema flexibility in an (SQL-) RDBMS: 1. use a Entity–attribute–value model 2. store a JSON document Depending on your use case, data volume etc. a nosql db could be the better choice. But sometimes you need only one or a few tables to be schema-flexible and your other data is relational.

Some SQL RDBMS support schema flexible tables, E.g. SAP HANA ("CREATE TABLE ... WITH SCHEMA FLEXIBILITY...").

Christoph G
  • 555
  • 2
  • 8