a***@gmail.com
2007-03-22 13:52:49 UTC
If a column does not already exist on a table I want to add it. The
column already exists in the database I am testing this in.
Here is my problem:
IF NOT EXISTS(SELECT 1
FROM sysobjects,
syscolumns
WHERE sysobjects.id = syscolumns.id
AND sysobjects.name = 'Person'
AND syscolumns.name = 'Status')
BEGIN
ALTER TABLE Person
ADD Status char(1)
DEFAULT 'E' NOT NULL
END
results in:
Error: com.sybase.jdbc3.jdbc.SybSQLException: Column names in each
table must be unique. Column name 'Status' in table 'Person' is
specified more than once.
, SQL State: ZZZZZ, Error Code: 2705
However
IF NOT EXISTS(SELECT 1
FROM sysobjects,
syscolumns
WHERE sysobjects.id = syscolumns.id
AND sysobjects.name = 'Person'
AND syscolumns.name = 'Status')
BEGIN
ALTER TABLE Person
ADD Status char(1) NULL
END
results in:
Query 1 of 1 elapsed time (seconds) - Total: 0, SQL query: 0, Building
output: 0 (success)
column already exists in the database I am testing this in.
Here is my problem:
IF NOT EXISTS(SELECT 1
FROM sysobjects,
syscolumns
WHERE sysobjects.id = syscolumns.id
AND sysobjects.name = 'Person'
AND syscolumns.name = 'Status')
BEGIN
ALTER TABLE Person
ADD Status char(1)
DEFAULT 'E' NOT NULL
END
results in:
Error: com.sybase.jdbc3.jdbc.SybSQLException: Column names in each
table must be unique. Column name 'Status' in table 'Person' is
specified more than once.
, SQL State: ZZZZZ, Error Code: 2705
However
IF NOT EXISTS(SELECT 1
FROM sysobjects,
syscolumns
WHERE sysobjects.id = syscolumns.id
AND sysobjects.name = 'Person'
AND syscolumns.name = 'Status')
BEGIN
ALTER TABLE Person
ADD Status char(1) NULL
END
results in:
Query 1 of 1 elapsed time (seconds) - Total: 0, SQL query: 0, Building
output: 0 (success)