Skip to main content
Removed historical information (that is what the revision history is for)—the answer should be as if it was written right now.
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Tweak the below to suit your specific requirements:

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int 

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int 

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

if not exists (select column_name from INFORMATION_SCHEMA.columns where table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int 

That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Rollback to Revision 4
Source Link
Luke Bennett
  • 32.9k
  • 3
  • 33
  • 57

Tweak the below to suit your specific requirements:

IFif NOTnot EXISTSexists (SELECTselect  column_name FROMfrom  INFORMATION_SCHEMA.columns WHEREwhere  table_name = 'MyTable'   ANDand column_name = 'MyColumn')   ALTER TABLE MyTable  alter table MyTable ADDadd MyColumn INT int 

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

IF NOT EXISTS (SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE table_name = 'MyTable'   AND column_name = 'MyColumn')   ALTER TABLE MyTable  ADD MyColumn INT  

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

if not exists (select  column_name from  INFORMATION_SCHEMA.columns where  table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int 

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

deleted 118 characters in body
Source Link
Pரதீப்
  • 94.1k
  • 21
  • 144
  • 179

Tweak the below to suit your specific requirements:

ifIF notNOT existsEXISTS (select SELECT column_name from   FROM INFORMATION_SCHEMA.columns where   WHERE table_name = 'MyTable' and AND column_name = 'MyColumn')   ALTER TABLE MyTable  alter table MyTable addADD MyColumn intINT  

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

if not exists (select  column_name from   INFORMATION_SCHEMA.columns where   table_name = 'MyTable' and column_name = 'MyColumn') alter table MyTable add MyColumn int 

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Tweak the below to suit your specific requirements:

IF NOT EXISTS (SELECT column_name FROM INFORMATION_SCHEMA.columns WHERE table_name = 'MyTable'  AND column_name = 'MyColumn')   ALTER TABLE MyTable  ADD MyColumn INT  

Edit to deal with edit to question: That should work - take a careful look over your code for stupid mistakes; are you querying INFORMATION_SCHEMA on the same database as your insert is being applied to for example? Do you have a typo in your table/column name in either statement?

Appreciate the intent behind the previous edit, but as StackOverflow says to "always respect the original author" and SQL formatting is subjective, I'm reverting to my preferred style :)
Source Link
Luke Bennett
  • 32.9k
  • 3
  • 33
  • 57
Loading
format
Source Link
SteveC
  • 17k
  • 25
  • 116
  • 180
Loading
Deal with edit to question
Source Link
Luke Bennett
  • 32.9k
  • 3
  • 33
  • 57
Loading
Source Link
Luke Bennett
  • 32.9k
  • 3
  • 33
  • 57
Loading