Tuesday, 19 June 2012

Lesson 16: Inserting Rows in SQL

You can insert new rows into a table by using Scalable SQL.

Insert into tablename ( fieldname, fieldname, fieldname)
values ( value, value, value);

  • When inserting data, use the Start Transaction command so that any errors can be rolled back.
  • You must specify the values to be inserted.
  • When performing an insert, enclose character values in single quotes.
  • Do not enclose numeric values in single quotes.
  • Use the Rollback Work command to undo changes.
  • Use the Commit Work command to finalize changes.
Example:Insert the following vendor into the TrnVendor table.
Vendor Id: TV055
Name: Party Games
Address: PO Box 136
City: Chicago
State: IL
Current Balance: 2498.62
  1. To start the transaction, type:
Start transaction;
  1. To insert the records, type:
Insert into TrnVendor (VendId, Name, Addr1, City, State, 
CurrBal )
values ( 'TV055', 'Party Games', 'PO Box 136', 'Chicago', 'IL', 2498.62);
  1. To view the record, type:
Select VendId, Name, Addr1, City, State, CurrBal [-ZZZ,ZZZ.99]
from TrnVendor
where VendId = 'TV055';
  1. To roll back your work, type:
Rollback work;

No comments:

Post a Comment