Chapter 10 Inserting, Updating, and Deleting Data Flashcards
What are the 4 ways to insert data into tables?
- INSERT INTO Schema.Table VALUES(N’FT’, N’Feet’, 2540), (N’KM’, N’Kilo Meters’, 12)
- INSERT INTO Schema.Table (Col1, Col2) SELECT Col1, Col2 FROM Schema.Table
- INSERT INTO Schema.Table EXEC (@SQL) OR INSERT INTO Schema.Table EXEC SP_EXECUTE_SQL @SQL
- SELECT * INTO Schema.Table FROM ( select… from ) AS T1
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.
INSERT INTO Sales.MyOrders(custid, empid, orderdate) VALUES(2, 19, ‘20120620’);
OR
INSERT INTO Sales.MyOrders(custid, empid, orderdate)
VALUES(2, 19, ‘20120620’), (1, 21, ‘20150516’); –> Note the comma seperation.
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 and 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 table’s column definition order.
If the underlying table definition changes, and the INSERT statement is not modified, the result is either an error or values written to the wrong column or both.
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.
SET IDENTITY_INSERT ON;
INSERT INTO Schema.Table VALUES(1, 2000)
SET IDENTITY_INSERT OFF;
Note that in order to use this option, 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.
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;
Does INSERT EXEC work when more than one query is returned?
Yes.
INSERT EXEC works when the source dynamic batch or stored procedure has more than one query.
But only 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.
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 when 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.
This can blocking situations due to conflicts related to both data and metadata access.