Search code examples
sqldb2default

Insert Blank value in DB2 for different data types


I have a table in DB2 with more than 150 columns of different data types including CHARACTER, DECIMAL, DATE, TIMESTAMP and VARCHAR.

I am creating an insert script for only one record where I need the first column to be populated by a default value and all remaining columns should be Blank irrespective of the data type.

For e.g.

INSERT INTO TABLE1 (COL1, COL2, COL3, COL4 ... COL150)
VALUES ('hello','','','','' ... '')

COL1 is CHARACTER, COL2 can be TIMESTAMP, COL3 can be VARCHAR, COL4 can be VARCHAR. Can I use '' for all data types?


Solution

  • Not sure if I understood your question well, but let me give it a better explanation than the one that I did in my comment.

    Every time I have to insert something in my database I use, well my boss force me lol, to create functions. In my tables most of the col allows NULL values. So, this is an example (not perfect by the way):

    function insertMajor($application_id, $major,$type, $conn){
    //insert
    $query = "INSERT INTO [dbo].[Applicant_Majors] "
            . "([application_id],"
            . "[program],"
            . "[type])"
            . "VALUES('$application_id','$major','$type')";
    
    //connection
    $stmt = sqlsrv_query($conn, $query);
    
    if ($stmt === false) {//start if
        die(print_r(sqlsrv_errors(), true));
     }//end if
    
    }
    

    My Applicant_Majors table has more columns such as name, date_of_declaration etc, but does columns can be null or the user can leave that question in blank.

    Not sure if this is what you are trying to do.

    PS: I use SQLserver 2008 my code can be different from yours