Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2003
    Posts
    3

    Unanswered: Help Needed in SQL.

    What is the SQL Statement that would insert the following data

    - First Name: John
    - Last Name: Doe
    - Address: 89 Chestnut Street
    - City: Ridgewood
    - State: NJ
    - Zip: 07450

    into the following table

    create tbl_patient (
    patient_id int IDENTITY PRIMARY KEY CLUSTERED ,
    fname varchar(255) NOT NULL ,
    lname varchar(255) NOT NULL ,
    address varchar(255) NOT NULL ,
    city varchar(255) NOT NULL ,
    state varchar(255) NOT NULL ,
    zip varchar(255) NOT NULL )

  2. #2
    Join Date
    Apr 2003
    Location
    NY
    Posts
    208

    Re: Help Needed in SQL.

    Insert Into TBL_PATIENT(fname,lname,address,city,state,zip)
    Values('John','Doe','89 Chestnut Street','NJ','07450');

  3. #3
    Join Date
    Aug 2003
    Posts
    3
    so there is no need to specify anything for first column which is PK Clustered. Is that right?? I guess it will be generated by system. Let me know about this.

    thanks

  4. #4
    Join Date
    Apr 2003
    Location
    NY
    Posts
    208
    Sorry about that. I missed the patient_id.

    you can either provide the value if you know it

    SQL-> Insert Into TBL_PATIENT(patient_id,fname,lname,address,city,st ate,zip)
    Values(103,'John','Doe','89 Chestnut Street','Ridgewood','NJ','07450');

    or if you have a sequence for the id field you can use the .NextVal

    SQL->Create Sequence p_seq_id increment by 1 start with 1000;

    SQL->Insert Into TBL_PATIENT(patient_id, fname,lname,address,city,state,zip)
    Values(p_seq_id.NextVal,'John','Doe','89 Chestnut Street',
    'Ridgewood','NJ','07450');

    We usually handle the PK values in a trigger that fires Before insert that populates the Id field for us.

    Create Trigger Before_Patient_Insert_Trg
    Before Insert on Tbl_Patient
    For Each Row
    Begin
    :new.patient_id := p_seq_id.NextVal;
    End;

    Then you can use a statement like I first sent you

    SQL->Insert Into TBL_PATIENT(fname,lname,address,city,state,zip)
    Values('John','Doe','89 Chestnut Street','Ridgewood','NJ','07450');


    Hope that helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •