0

I am trying to insert a new column in my database if it isn't inserted earlier. My column looks like this

ALTER TABLE dbo.tSafeUnit ADD HasAccess24_7 tinyint not null default 0

I want the column to be inserted only if it is not in the table.

My line of code only inserts it but doesn't check if it exists.

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
Endrit Sheholli
  • 101
  • 1
  • 3
  • 19
  • 3
    Which [DBMS](https://en.wikipedia.org/wiki/DBMS) product are you using? "SQL" is just a query language, not the name of a specific database product. –  Dec 05 '17 at 15:52
  • Inserting rows is done via `insert` **not** via `ALTER TABLE` –  Dec 05 '17 at 15:52
  • my mistake, i am trying to insert a new column – Endrit Sheholli Dec 05 '17 at 15:54
  • If a column with the same name already exists you'll get an error. – jarlh Dec 05 '17 at 15:54
  • 1
    You want to _add_ a new column to the table. – jarlh Dec 05 '17 at 15:55
  • [Edit] your post to tag the DBMS being used, then we'll definitely have a duplicate to mark. – underscore_d Dec 05 '17 at 15:56
  • In Postgres you can do `alter table ... add column if not exists ...` - which DBMS **are** you using? –  Dec 05 '17 at 15:56
  • SQL Server: IF EXISTS ( SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[tSafeUnit]') AND name = 'HasAccess24_7' ) – user8834780 Dec 05 '17 at 15:56
  • Possible duplicate of [How to check if a column exists in SQL Server table](https://stackoverflow.com/questions/133031/how-to-check-if-a-column-exists-in-sql-server-table) – Magnus Dec 05 '17 at 15:57
  • Possible duplicate of [Add a column to a table, if it does not already exist](https://stackoverflow.com/questions/8870802/add-a-column-to-a-table-if-it-does-not-already-exist) – underscore_d Dec 05 '17 at 15:57

4 Answers4

2

Try this:

IF (SELECT COUNT(*) FROM syscolumns WHERE name = 'HasAccess24_7'
AND OBJECT_NAME(object_id) = 'tSafeUnit') = 0
BEGIN
    ALTER TABLE dbo.tSafeUnit ADD HasAccess24_7 tinyint not null default 0
END
Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • 2
    Please note, syscolumns has been deprecated for quite some time now in favor of sys.columns. https://learn.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-syscolumns-transact-sql – Sean Lange Dec 05 '17 at 16:03
  • And using `IF EXISTS (...)` instead of `SELECT COUNT(*)` would also be advisable – marc_s Dec 05 '17 at 16:47
1

If you are using MS SQL server, then you can do it like below :

IF COL_LENGTH('tSafeUnit', 'HasAccess24_7') IS NULL
BEGIN
    ALTER TABLE dbo.tSafeUnit 
    ADD HasAccess24_7 tinyint not null default 0
END
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
1

You can try this.

IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = 'HasAccess24_7' AND TABLE_NAME = 'tSafeUnit')
   AND EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'tSafeUnit')
BEGIN
    ALTER TABLE dbo.tSafeUnit ADD HasAccess24_7 TINYINT NOT NULL DEFAULT 0
END
Mova
  • 928
  • 1
  • 6
  • 23
  • 1
    not accurate at all.... what happen if HasAccess24_7 exists in table tSafeUnit_2 ? – MLeblanc Aug 12 '20 at 12:51
  • So what will happen? – Mova Aug 25 '20 at 00:33
  • if HasAccess24_7 exists in another table, then it will not add the new column, you need to make sure HasAccess24_7 doesn't exists in that particular table, and not exists in every table in the database – MLeblanc Aug 25 '20 at 02:42
  • hahaha... Just look at the condition "COLUMN_NAME = 'HasAccess24_7' AND TABLE_NAME = 'tSafeUnit'". Coming to "and not exists in every table in the database", why does it matter whether the same column is available in any other table or not. Is it even asked by Endrit Sheholli? – Mova Aug 27 '20 at 15:03
-1

test is the column exists for that table in that schema

if not exists ( select 1 
                from information_schema.columns 
                where table_schema = 'dbo' 
                and table_name = 'tSafeUnit' 
                and column_name = 'HasAccess24_7')
begin
    ALTER TABLE dbo.tSafeUnit ADD HasAccess24_7 TINYINT NOT NULL DEFAULT 0;
end
MLeblanc
  • 1,816
  • 12
  • 21