Chapter 10: DML Flashcards
Why is it recommended to specify the target column names in INSERT statements?
Because then you don’t care about the order in which the columns are defined in the table. Also, you won’t be affected if the column order is rearranged due
to future definition changes, in addition to when columns that get their values automatically are added.
What is the difference between SELECT INTO and INSERT SELECT?
SELECT INTO creates the target table and inserts into it the result of the query. INSERT SELECT inserts the result of the query into an already existing table.
It’s possible insert data with a stored procedure using INSERT statement?
Yes, it is.
What does do the OUTPUT clause in a INSERT INTO statement?
All the data which it’s going to be inserted in the target table, it’s also inserted in another table for auditing purposes. For use the OUTPUT clause, the syntax is: OUTPUT Inserted.col1, Inserted.col2, …, Inserted.coln INTO @AuditTable
How I can add a row ignoring the identity property column?
Using the SET IDENTITY_INSERT table ON. After the insert statement it’s issued, it needs to be OFF.
What’s the standard syntax for an UPDATE statement?
UPDATE SET = = ... = [WHERE ]
Could an UPDATE statement use a FROM clause and joins?
Yes, it could. The syntax would look like this:
UPDATE
SET
FROM
WHERE
How I can store the values changed during an UPDATE statement?
You could use a variable and store the value updated. It would be like this:
DECLARE @variable AS ;
UPDATE
SET @variable = =
Once the prior statement is executed, @variable will containt the value updated.
In an UPDATE statement, does all SET references executes at the same time?
Yes, it does. Let’s suppose we have the following UPDATE statement:
UPDATE table SET col1 = 'a', col2 = col1 WHERE keycol = 1
You’ll think that col2 will have the value which contains the col1 (which is ‘a’ once is updated), but actually it’ll containt the prior value of col1 before is updated. This is called all-at-once.
What’s the syntax for achieve a DELETE statement?
DELETE [FROM]
[WHERE ]
If there’s not a WHERE clause specified, then it’ll delete all rows from the table.
What does do the MERGE statement?
As his clause’s name indicates: it merges (join together) a source table into a target table, and it allows the three DML statements together (INSERT, DELETE, UPDATE) according to matching predicates and simple predicates.
What’s the syntax of the MERGE statement?
MERGE INTO
USING
ON
WHEN MATCHED [AND ] THEN {UPDATE | DELETE}
WHEN NOT MATCHED [BY TARGET] [AND ]
THEN {INSERT}
WHEN NOT MATCHED BY SOURCE [AND ]
THEN {UPDATE | DELETE}
Is the USING clause from the MERGE statement like FROM clause in the SELECT statement?
Yes, it is.
Is the USING clause from the UPDATE/DELETE statement like FROM clause in the SELECT statement?
Yes, it is.
What’s used for the OUTPUT clause?
It shows/store the data according to an action, such as inserted, updated or deleted.