none
tsql execution: how to disable column check? RRS feed

  • Frage

  • hi,

    is it possible to enable the following:

    i first check if a table exists. if exists, then check each column of a table. if table or column(s) not exist then create it. then i try to insert data. see code below:

    -------------------------------------

    IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'XYZ')
    
    BEGIN
    
    	CREATE TABLE XYZ(
    
    		Id INT IDENTITY (1,1) NOT NULL,
    
    		Test VARCHAR(50) NULL
    
    	)
    
    END
    
    ELSE
    
    BEGIN
    
    	IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'XYZ' AND COLUMN_NAME = 'Test')
    
    	BEGIN
    
    		ALTER TABLE XYZ ADD Test VARCHAR(50) NULL
    
    	END
    
    END
    
    
    
    INSERT INTO XYZ(Test) VALUES ('Test')

    --------------------------------------------------------

    if the table already exists (without column "Test") there is a Error while Execution: "Column Test does not exist"

    is it possible to run the script? i don't want to execute the insert with EXEC('INSERT INTO...') and without any GO Between the lines.

    thank you very much

    hidry

    Donnerstag, 23. September 2010 14:13

Antworten