Google Answers Logo
View Question
 
Q: IF NOT EXISTS by column name in sql ( No Answer,   5 Comments )
Question  
Subject: IF NOT EXISTS by column name in sql
Category: Computers > Programming
Asked by: isaac123456789-ga
List Price: $30.00
Posted: 30 Aug 2005 18:27 PDT
Expires: 29 Sep 2005 18:27 PDT
Question ID: 562422
How do i add a column in a sql table only if it not exists
something like "ALTER TABLE `Cart` CHANGE IF EXISTS `EmailCust`
`EmailCust` VARCHAR(254)" (sql 4.0) that i shouldn get a error

Request for Question Clarification by palitoy-ga on 31 Aug 2005 01:24 PDT
I am a little unsure as to what you require, can you clarify your
question a little?

Are you seeking to only add data to a column if that data does not
already exist?  For instance only add an email address if it does not
already exist.  If this is your problem, your solution should lie in
the UNIQUE command.

I would be happy to provide you with an answer discussing how to use
this command, just let me know by responding to this clarification.

palitoy-ga

Clarification of Question by isaac123456789-ga on 31 Aug 2005 13:18 PDT
hi palitoy-ga
Thanks for your response.

my question is to add a new column if the "column" is not already
exist (not on the data inside the existing column)
that i shouldn get a sql error

Thanks

Request for Question Clarification by hammer-ga on 31 Aug 2005 14:07 PDT
Isaac123456789-ga,

You would likely do this outside of the ALTER TABLE statement. For
example, if you are working in PHP, you might attempt a SELECT on the
column and check for an error before performing the ALTER TABLE.

If you are interested in this type of solution, please let us know
what language you are using.

Note to researchers: If Customer responds, feel free to provide an
answer. I may not be able to get back to this question.

- Hammer

Clarification of Question by isaac123456789-ga on 31 Aug 2005 16:27 PDT
in a perl script i know how to do it but i need in sql

Clarification of Question by isaac123456789-ga on 14 Sep 2005 15:19 PDT
I need a solution for mysql 4.0
Answer  
There is no answer at this time.

Comments  
Subject: Re: IF NOT EXISTS by column name in sql
From: jaseaux-ga on 30 Aug 2005 21:56 PDT
 
You have a table, and you only want to update it with data if there is
already data present.  If not, you don't want to add new data?
Subject: Re: IF NOT EXISTS by column name in sql
From: hammer-ga on 06 Sep 2005 05:46 PDT
 
isaac123456789-ga,

Palitoy-ga's solution is specific to SQL Server, which is a particular
Microsoft database package. The technique uses syntax which is not
part of the SQL standard itself, but is specific to Microsoft's
implementation, called T-SQL. MySQL does not use T-SQL and does not
have object_id. I don't believe that SQL alone offers a solution to
your problem. You can, in your code, attempt the operation and test
for the returned mySQL error so you can respond appropriately.

- Hammer
Subject: Re: IF NOT EXISTS by column name in sql
From: francescosydney-ga on 19 Sep 2005 23:06 PDT
 
Here's the answer:

1. Check a property on the column. If it returns null, then the column
doens't exist:

IF columnproperty (object_id('MyTableName'), 'MyColumnName', 'AllowsNull') IS NULL
BEGIN
ALTER TABLE dbo.MyTableName ADD
	MyColumnName varchar(100) NULL

END
Subject: Re: IF NOT EXISTS by column name in sql
From: isaac123456789-ga on 20 Sep 2005 09:41 PDT
 
hi francescosydney-ga 
this answer is gone work onlt on sql server and i need for mysql
thanks anyway
Subject: Re: IF NOT EXISTS by column name in sql
From: hayarci-ga on 24 Sep 2005 00:21 PDT
 
Hi,

Maybe this is now a real answer 
but I think you are searching in the difficult way.

These are simple ways;
the two scenario in my head about your question.

First; You could upgrade your db to mysql 5.0; so you could easily use
these new features; "If Then" clause, "Stored Procedures" and also use
"Information_Schema Tables".

After upgrade, the job is writing a script that, takes the column
names from information schema table per table, and by If Clauses try
to Alter them in a sp.

My second suggestion; exract datas from "show tables" to a txt file.
Prepare it as "show columns table_example_name" per line. 
Try to execute sql sentences line by line and import the result a
table, with the table's name.
Then prepare an other batch file for your "Alter clause" with using
the column name data in the table. Prepare your clause what you wish.
Execute your last batch file.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy