Generating an INSERT Statement
One of the ways to insert data into a table is creating an INSERT statement and running it against the database. This tutorial step shows you how to generate the INSERT statement automatically.
Assuming that the zoo in our tutorial has just hired two new zookeepers, you will create a script that adds these two new employees to the table tblZookeepers.
To generate the INSERT script:
1.Open the "ZooDB" project created previously and double-click the ZooDBConnect data source to connect to it.
2.Click the SQL Editor toolbar button or select the menu option File | New | SQL Editor (Ctrl+N). A new SQL Editor window opens.
3.In the Online Browser, find the tblZookepers table and, holding the Ctrl key pressed, select all its columns except the ZookID column.
4.Using the right mouse button, drag and drop the selected columns from the Online Browser into the SQL Editor window. A context-sensitive menu appears.
5.Select Insert from the context menu. The following statement appears in the SQL window:
INSERT INTO [ZooDB].[dbo].[tblZookeepers] ([FirstName], [LastName], [Address], [City], [State], [Telephone], [DOB]) |
6.Edit the statement as follows:
INSERT INTO [ZooDB].[dbo].[tblZookeepers] ([FirstName], [LastName], [Address], [City], [State], [Telephone], [DOB]) |
7.Create a second INSERT statement and edit it as follows:
INSERT INTO [ZooDB].[dbo].[tblZookeepers] ([FirstName], [LastName], [Address], [City], [State], [Telephone], [DOB]) VALUES ('Bruno', 'Katz', '87 Panther Place', 'Trenton', 'NJ', '(515)555-7667', '1969-09-13'); |
8.Click the Execute button or press F5.
To check the results, open a new SQL Editor window and enter:
SELECT * FROM tblZookeepers |
Click Execute to run the statement and notice that two new rows have been inserted into the table.