Inserting Values into a sql table

  • I am inserting a new record into a table in a stored procedure. Do I always have to insert a value for each column in the table? I would like to use the defaults on some columns.

    Insert into tblMail

    Select Name = @Name,

    Address1 = @Address1,

    Address2 = @Address2,

    City = @City,

    State = @State,

    Country = @Country

    If I have Country set to USA as a default why can't I just have

    Insert into tblMail

    Select Name = @Name,

    Address1 = @Address1,

    Address2 = @Address2,

    City = @City,

    State = @State

    Thank you for any guidance.

  • You need to qualify the columns you are INSERTing INTO.

    INSERT INTO tblMail (Address1, Address2, City, State)

    Select Name = @Name,

    Address1 = @Address1,

    Address2 = @Address2,

    City = @City,

    State = @State

    IF you try and do it without the COLUMN qualifiers the INSERT will fail because you are not passing the # of data elements that SQL knows about.  Also, you need to ensure that the COUNTRY column in NULLABLE or the INSERT will fail anyway.

    Hope this helps,

    AJ



    Good Hunting!

    AJ Ahrens


    webmaster@kritter.net

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply