2008-11-15 15 views
5

का उपयोग केवल MySQL का उपयोग करने के लिए, मैं देख रहा हूं कि तालिका संभव है तो केवल एक सम्मिलित कथन चलाएं। तालिका में मौजूद है या नहीं, यह देखने के लिए मैंने सफलतापूर्वक उपयोगकर्ता चर बनाया है। समस्या यह है कि आप एक सम्मिलित कथन के साथ "WHERE" का उपयोग नहीं कर सकते हैं। इस काम को कैसे प्राप्त करें इस पर कोई विचार?एसक्यूएल - एक सशर्त INSERT

// See if the "country" table exists -- saving the result to a variable 
SELECT 
    @table_exists := COUNT(*) 
FROM 
    information_schema.TABLES 
WHERE 
    TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'country'; 

// Create the table if it doesn't exist 
CREATE TABLE IF NOT EXISTS country (
    id INT unsigned auto_increment primary key, 
    name VARCHAR(64) 
); 

// Insert data into the table if @table_exists > 0 
INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands') WHERE 0 < @table_exists; 

उत्तर

6
IF @TableExists > 0 THEN 
    BEGIN 
     INSERT INTO country (name) VALUES ('Afghanistan'),('Aland Islands'); 
    END 
संबंधित मुद्दे