If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Check column existance

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-20-08, 06:21
thepercival thepercival is offline
Registered User
 
Join Date: Jan 2003
Location: The Netherlands
Posts: 145
Check column existance

Hello,

I want to add a column only if it not exists. Is this possible?

-----------------------------
DROP PROCEDURE IF EXISTS mycheckColumn;

DELIMITER $$

CREATE PROCEDURE mycheckColumn( sTableName VARCHAR(255), sColumnName VARCHAR(255) )
BEGIN
DECLARE sDummy VARCHAR(100);
SET @sSQL = CONCAT( 'SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_name = \'', sTableName, '\' LIMIT 0, 1 ' );
PREPARE stmt FROM @sSQL;
EXECUTE stmt;
DROP PREPARE stmt;
END$$

DELIMITER ;
---------------------------------------------
So when I do : CALL mycheckColumn('voe2dsel','Id'); It will display table-info.
I want to put the result in an output parameter. How do I do this?
Is this possible?

thanx
Reply With Quote
  #2 (permalink)  
Old 08-20-08, 06:31
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
why would you want to?
what circumstances do you think that you will need to add columns to a table whilst the application is live?
what circumstances do you think will exist that may make a column name 'go away' or be deleted?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 08-20-08, 06:45
thepercival thepercival is offline
Registered User
 
Join Date: Jan 2003
Location: The Netherlands
Posts: 145
reply

Does it really matter why I want this? But for you; It is an update script. This updatescript will run everytime there is an update. So when you create a column the first time it is ok, but when you create the same column the next time the script will give an error. That is the reason.
Reply With Quote
  #4 (permalink)  
Old 08-20-08, 07:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by thepercival
Does it really matter why I want this?
yes, knowing the reason why the person asking the question wants to do something will usually allow the person answering the question to understand the problem better

in this case, i'm having a hard time with the concept of wanting to add a column to a table more than once, such that you need to detect this in application logic before executing it

what's wrong with just allowing the database to return an error message?

granted, the error will not say "dude, that table already has that column" but translating the database error message into a user-friendly message is the job of your application, right?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-20-08, 07:15
thepercival thepercival is offline
Registered User
 
Join Date: Jan 2003
Location: The Netherlands
Posts: 145
I didn't say the script run's in an application, but if you run the script from the command line than you still don't want so see errors. Right?
Reply With Quote
  #6 (permalink)  
Old 08-20-08, 07:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
no, i disagree

if the script is trying to do something wrong, i don't mind seeing errors
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On