Insert Into SQL Database

This command allows you to insert a record into a database.

To use this command you will need to know the table structure of the database.

Parameters

Simul8 SQL Database: The name of the database where the record is to be inserted.
SQL Insert Command: SQL insert command syntax. This follows the format:

INSERT into XXX ('aaa','bbb') VALUES('yyy','zzzz')

where:

  • XXX is the name of the table in your database you wish to insert into the record
  • aaa and bbb are the name of the fields you wish to insert values into
  • yyy and zzz are the values you wish to insert into the fields. Note value yyy would be inserted into field aaa.

Example

Suppose you have a db called myDB that contains a table called “Persons” that in turn has the simple structure below:

LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger

If you want to insert this record into the table

LastName: Hetland
FirstName: Camilla
Address: Hagabakka 24
City: Sandnes

Then you achieve this as follows:

Insert into SQL Database myDB, “INSERT INTO Persons VALUES('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes') ”

This will result in:

LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes


Now suppose that you want to insert a partial record into the db

LastName: Rasmussen
Address: Storgt 67

Then you do this as follows \
Insert into SQL Database myDB, “INSERT INTO Persons (LastName, Address) VALUES ('Rasmussen', 'Storgt 67')”

This will now give you: LastName FirstName Address City
Pettersen Kari Storgt 20 Stavanger
Hetland Camilla Hagabakka 24 Sandnes
Rasmussen Storgt 67

As you can see from the second example the SQL query is very similar to the first. Since it was a partial record you wanted to add, you had to supply additional information to specify what fields in the record you wanted to fill.

See Also