|
|
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 | |
| |
| |
| |
| |
|
|
There is no answer at this time. |
|
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. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |