How to create the SQL INSERT statement

SQL INSERT is a fundamental query for adding one or multiple rows of data into a specific table within a database. To run the statement, you need to specify a table name and, optionally, column names with their corresponding values that you want to insert into a table. Usually, the syntax of SQL INSERT does not cause any significant difficulties, but there can be some challenges.

dbForge Query Builder for SQL Server helps not only avoid complications while building SQL queries but also designs them without manual coding. The tool offers a graphical interface that allows users to visually design SQL statements by dragging and dropping tables, selecting columns, defining conditions, and specifying joins. This visual approach simplifies query creation, especially for those less familiar with SQL syntax. Also, to start using dbForge Query Builder for SQL Server, you don't need in-depth knowledge of SQL syntax, as the instrument delivers a simple and intuitive interface.

In this tutorial, we'll show you how to convert the SELECT query into INSERT VALUES and INSERT RESULTS with the help of the Query Builder tool. The INSERT VALUES statement allows adding user-defined values to columns. However, it cannot be used when you need to add multiple values to a table. Instead, you can use INSERT RESULTS to insert values from several tables into a single target table.

Add user-defined values to the target table

For demo purposes, we are going to use an empty table Sales.Staff from the BicycleStoreDev database and then populate it with test data. First, open the table on the Query Builder diagram.

1. In the Database Explorer, right-click the table and select Send to > Query Builder.

Alternatively, from the Database Explorer, drag the table to the diagram.

2. In the upper-left corner of the diagram, right-click SELECT and select Change Type > Insert Values to add the values to the table. The statement has been converted to the INSERT INTO statement.

3. On the diagram, select the columns for the query. The selected columns will be automatically displayed on the Insert tab of the Tabbed Editor.

Note
If the Tabbed Editor is not displayed, you can open it by clicking Counting all products Show Tabbed Editor on the Query toolbar or main menu.

To exclude the columns, clear the corresponding checkboxes on the diagram.

4. Insert the values into the columns. To do this, on the Insert tab of the Tabbed Editor, enter the values in the Columns fields.

Insert values

5. Switch to the Text view to review the designed query text.

Review the designed query

6. Execute the statement by pressing F5 or clicking Execute to verify that the values have been inserted in the table successfully.

Add values from multiple tables to the target table

1. From the Database Explorer, select several tables and drag them to the Query Builder diagram.

2. Switch to the Where tab to add a filtering condition. To do this, click Counting all products Add and specify the condition. For example, the result should be filtered based on the criteria that SalesPersonID equals 280.

  • Select the SalesPersonID column as an operand.
  • Set equals as a criteria operator.
  • Enter 280 as a value.
Filter conditions

3. On the diagram, select the columns you want to add to the target table.

4. Change the query type from Select to Insert Results. To do this, right-click the diagram and select Change Type > Insert Results.

Change result

5. In the Choose Target Table to Insert Data to dialog that opens, select the database, schema, and table you want to insert data into, and click OK.

Note
The table, query fields, and column data types must match, while column names can differ. If they don't match like in our example, then click the field and correct the order.

Order not matching

As you can see, the query type has been changed.

Changes result

6. Switch to the Text view to preview the SQL query script and execute the query to add the designed data to the table.

Preview the query

Conclusion

We've demonstrated you only a small part of the capabilities of dbForge Query Builder for SQL Server. The tool is renowned for its user-friendly interface, efficient query-building features, intuitive design, and its ability to streamline database development tasks. Download dbForge Query Builder for SQL Server and elevate your database management experience!

Additionally, feel free to watch the video to see how to create the SQL INSERT statement using the Query Builder tool.