INSERT and SELECT INTO Flashcards
Which SQL statements can you use to insert data?
- INSERT VALUES
- INSERT SELECT
- INSERT EXEC
- SELECT INTO
SQL Server 70-461 10-01
Which of the four INSERT statements require that you specify the target column names?
None, but it is considered best practice to do so otherwise the EXEC, VALUES or SELECT portion of an INSERT statement has to specify the columns in column definition order and if the underlying table definition changes, errors could be encountered or values could be put in the wrong columns if the column order in the EXEC, VALUES or SELECT portion of the INSERT statement isn’t changed to match.
SQL Server 70-461 10-01
Do you need to specify a value for a column with an IDENTITY property in an INSERT statement?
No, the value will generate automatically.
SQL Server 70-461 10-01
How can you set the value for a column that has an IDENTITY property using an INSERT statement?
Turn on a session option called IDENTITY_INSERT with the following syntax:
SET IDENTITY_INSERT ON;
Remember to turn this option off when you’re done. Also, note that you have to have VERY strong permissions to turn this option off and on. You need to own the table or have ALTER
permissions on the table.
SQL Server 70-461 10-01
What is another way, besides the IDENTITY property, that data can be inserted into a column automatically using an INSERT statement?
A value can be assigned automatically due to the column having a DEFAULT constraint.
SQL Server 70-461 10-01
What is the syntax in an INSERT VALUES statement for using the DEFAULT constraint value?
Option 1
- You can omit the column in the INSERT VALUES statement entirely and the DEFAULT value will automatically be assigned.
- Example: INSERT INTO schema.TableName (column 2, column 3) VALUES (value 2, value 3);
Option 2
- You can explicitly add the column name in the columns list and the word DEFAULT in the values list and the DEFAULT value will automatically be assigned.
- Example: INSERT INTO schema.TableName (column 1, column 2, column 3) VALUES (DEFAULT, value 2, value 3); “
SQL Server 70-461 10-01
What is the syntax in an INSERT SELECT statement for using the DEFAULT constraint value?
Just omit the column name in the target column list and of course it would not be included in the SELECT portion of the statement.
SQL Server 70-461 10-01
What happens if you don’t include a column in the target column list of an INSERT statement?
- First, SQL Server checks to see if the column gets its value automatically from an IDENTITY property or DEFAULT constraint.
- Second, if there is no IDENTITY property or DEFAULT constraint SQL Server will check to see if the column allows NULLS. If it does, SQL Server assumes a NULL.
- Third, if there is no IDENTITY property, DEFAULT constraint and NULL’s aren’t allowed, SQL Server will generate an error.”
SQL Server 70-461 10-01
What is the proper syntax for an INSERT VALUES statement?
INSERT INTO schema.TableName (column 1, column 2, column 3) VALUES (value 1, value 2, value 3);
SQL Server 70-461 10-01
How many rows can be inserted with the INSERT VALUES statement?
1 or more
SQL Server 70-461 10-01
What is the syntax for inserting multiple rows using an INSERT VALUES statement?
Separate each row with a comma.
Ex.
INSERT INTO schema.TableName (Column 1, Column 2, Column 3) VALUES
(8, ‘USA’, ‘Minnesota’),
(2, ‘USA’, ‘California’),
(3, ‘USA’, ‘Colorado’);
SQL Server 70-461 10-01
What happens if one row fails to insert in an INSERT VALUES statement with multiple rows?
If even one row fails to insert to the target table, then the entire statement will fail and no rows will be entered into the target table since it is considered one transaction .
SQL Server 70-461 10-01
How is the INSERT VALUES statement different from the INSERT SELECT statement?
With the INSERT VALUES statement you explicitly spell out the values. With the INSERT SELECT statement the values are obtained via a SELECT statement.
SQL Server 70-461 10-01
What is the proper syntax for an INSERT SELECT statement?
INSERT INTO schema.TableName (column 1, column 2, column 3)
SELECT column a, column b, column c FROM schema.TableName;
SQL Server 70-461 10-01
What does the INSERT EXEC statement do?
It inserts the result set (or sets) returned by a dynamicbatch or a stored procedure into the specified target table.
SQL Server 70-461 10-01