Chapter 10 Inserting, Updating, and Deleting Data Flashcards
What are the 4 ways to insert data into your tables?
(1) INSERT VALUES, (2) INSERT SELECT, (3) INSERT EXEC, (4) SELECT INTO
What is the INSERT VALUES statement?
With the INSERT VALUES statement, you can insert one or more rows into a target table based on value expressions, e.g.
INSERT INTO Sales.MyOrders(custid, empid, orderdate)
VALUES(2, 19, ‘20120620’);
With INSERT VALUES, why should you specify the target column names after the table name?
Specifying the target column names after the table name is optional but considered a best practice because it allows you to control the source value to target column association. They can be ordered irrespective of the order in which the columns are defined in the table. Without the target column list, you must specify the values in column definition order. If the underlying table definition changes, but the INSERT statements aren’t modified, this can result in errors or values written to the wrong column.
How does INSERT VALUES handle values for columns with the INDENTITY property?
A value for INDENTITY columns is usually not specified in the INSERT VALUES statement because the property generates the value for the column automatically. If you want to provide your own value instead of letting the IDENTITY property do it for you, you need to first turn on a session option called IDENTITY_INSERT, e.g.
SET IDENTITY_INSERT ON;
…
SET IDENTITY_INSERT OFF;
Note that in order to use this option, you need quite strong permissions. You need to be the owner of the table or have ALTER permissions on the table.
How does INSERT VALUES handle values for columns with default constraints?
The INSERT VALUES statement can specify a value for this column explicitly and override the default; otherwise, SQL Server will use the default expression to generate that value.
Another way to achieve the same behavior is to specify the column name in the names list and the keyword DEFAULT in the respective element in the VALUES list.
If you don’t specify a value for a column within an INSERT VALUES statement, what does SQL Server do?
SQL Server will first check whether the column get it’s value automatically - for example from an IDENTITY or a default constraint. If that’s not the case, SQL Server will check whether the column allows NULLs, in which case it will assume a NULL. If that’s not the case, SQL Server will generate an error.
Can you insert multiple rows with INSERT VALUES?
Yes. Simply separate the rows with commas:
INSERT INTO Sales.MyOrders(custid, empid, orderdate)
VALUES (2, 11, ‘20120620’),
(5,13, ‘20120621’),
(6,12, ‘20120622’);
Note that when using multiple rows, the entire statement is considered one transaction. If any row fails to enter the target table, the entire statement will fail and no row is inserted.
What is the INSERT SELECT statement?
The INSERT SELECT statement inserts the result set returned by a query into the specified target table. INSERT SELECT also supports optionally specifying the target column names. Also, you can omit the columns that get their values automatically from an IDENTITY property, default constraint, or when allowing NULLs.
e.g.
SET IDENTITY_INSERT Sales.MyOrders ON;
INSERT INTO Sales.MyOrders (orderid, custid, empid, orderdate)
SELECT orderid, custid, empid, orderdate
FROM Sales.Orders
WHERE shipcountry=’Norway’
SET IDENTITY_INSERT Sales.MyOrders OFF
Does INSERT SELECT result in less logging than other insert operations?
Yes. In certain conditions, the INSERT SELECT statement can benefit from minimal logging which could result in improved performance when compared to a fully logged operation.
What is the INSERT EXEC statement?
With the INSERT EXEC statement, you can insert the result set (or sets) returned by a dynamic batch or a stored procedure into the specified target table. INSERT EXEC supports specifying an optional target column list and allows omitting columns that accept their values automatically.
e.g.
SET IDENTITY_INSERT Sales.MyOrders ON;
INSERT INTO Sales.MyOrders(orderid, custid, empid, orderdate)
EXEC Sales.OrdersForCountry
@country = ‘Portugal’
SET IDENTITY_INSERT Sales.MyOrders OFF;
SET IDENTITY_INSERT Sales.MyOrders OFF;
Does INSERT EXEC work when more than one query is returned?
Yes. INSERT EXEC works even when the source dynamic batch or stored procedure has more than one query. But that’s as long as all queries return result sets that are compatible with the target table’s definition.
What is the SELECT INTO statement?
The SELECT INTO statement involves a query (SELECT) and a target table (INTO). It creates the target table based on the definition of the source query and inserts the result rows from the query into that table.
The statement copies from the source some aspects of the data definition such as column names, types, nullability, and IDENTITY property. Other aspects like indexes, constraints, triggers, permissions are not copied. If you want to include the latter aspects, you need to script them from the source and apply them to the target manually.
e.g.
SELECT orderid, custid, orderdate
INTO Sales.MyOrders
FROM Sales.Orders
WHERE shipcountry=’norway’
When using SELECT INTO, do you have direct control over the definition of the target?
No. You don’t have direct control over the definition of the target. If you want target columns to be defined differently than the source, you need to apply some manipulation.
For example, if the source orderid column is IDENTITY and you don’t want the target column to have this property, you can apply a manipulation like orderid + 0 AS orderid. This will remove the IDENTITY property, but will cause the target column to allow NULLs. If you want the target column to be defined as not allowing NULLs, you need to use the ISNULL function to return a non-NULL value in case the source is NULL, e.g. ISNULL(orderid + 0, -1) AS orderid.
If you want the target column’s type to be different than the source, you can use the CAST/CONVERT functions, but remember that this will cause the target column to allow NULLs.
What are the drawbacks of using the SELECT INTO statement?
You have limited control over the definition of the target table. Some things you can control indirectly such as column data types and nullability, but some things you simply can’t control such as the file group of the target table.
What happens while a SELECT INTO statement executes?
SELECT INTO involves both creating a table and populating it with data. This means that the target table’s metadata and the actual data are exclusively locked until the SELECT INTO transaction finishes. As a result, you can run into blocking situations due to conflicts related to both data and metadata access.