Errata

Transact-SQL Cookbook

Errata for Transact-SQL Cookbook

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
Other Digital Version ?
?

http://oreilly.com/catalog/transqlcook/chapter/ch08.html

The SQL to calculate a 90% exponential moving average is not giving the correct result. The use of x.Sales is wrong, and the weights are used in the wrong order.

Change this:

SUM(CASE WHEN y.Id=x.Id THEN 0.9*y.Sales ELSE 0.1*x.Sales END) exponential_average

To this:

SUM(CASE WHEN y.Id=x.Id THEN 0.1*y.Sales ELSE 0.9*y.Sales END) exponential_average

Anonymous  Apr 09, 2009 
Printed Page 2
1st paragraph 2nd script

the original script:

INSERT Pivot
SELECT f1.i+f2.i+f3.i
FROM Foo f1, Foo F2, Foo f3

no longer works in SQL 2008 where pivot appears to be a reserved word. replacing the script with something like:

INSERT Pivot_tbl
SELECT f1.i+f2.i+f3.i
FROM Foo f1, Foo F2, Foo f3

works nicely

Anonymous  Oct 10, 2011 
Printed Page 2.2
ONLINE VERSION 3rd paragraph

****
(About the page no. I'm reading this online.http://safari.informit.com)
****
2.2 The Students Example
After Create Table states:
"If you execute the ""ch01.ImplementingSetDifference.objects.sql"" script, all the
tables needed for the recipes in this chapter will be created and populated with
data."
The downloaded file is "ch02".ImplementingSetDifference.objects.sql

Anonymous   
Printed Page 12
Figure 2.7

The figure attempts to describe the set difference. However the result set is
missing one element. As set difference is defined as : S1 S2 = { x : x in S1, x
not in S2 } the result of {a, b, c, d} {a, d, e, f } = {b, c}, as opposed to the
set {c} as shown in the diagram.

Anonymous   
12
Figure 2-7

If the difference result set should include elements in Set1 that are not in Set2, it seems to me that both C and B are part of the difference between set 1 and 2.

Additionally if the difference result set should include elements not in both Set1 and Set2, then the difference set should be B,C,E & F because only A & D are in both sets.

Could someone email me with if both previous statements are incorrect and the difference between set1 and set2 is just C as illustrated in 2-7?

Thanks in advance for any help you will give me.

Conrad

Anonymous  Oct 26, 2010 
Printed Page 22
Solution

The query reads "GROUP BY CourseId, StudentName", but the result is ordered as if the
clause were "GROUP BY StudentName, CourseId".

Anonymous   
Printed Page 157
top of page, inside "CREATE TRIGGER UpdateStock"

Your audit logging method will not record an update to the ProductID. On line 15 of
the UpdateStock trigger your WHERE clause is "d.ProductId=i.ProductId" (p 157). If a
user changes the ProductID the cursor will never fetch the row pair from the deleted
and inserted tables and therefore the 'E'/'N' pair will not appear in the log table.
I coded this, changed "Bananas" to "Yes we have no bananas" and verified the absence
from the StockLog table. The rest of the audit log functions (e.g., snapshots) will
not work reliably when rows are missing from StockLog.

We all know that updating what is basically a rowid is trouble-in-the-making, but you
should explicitly disallow this with the following snippet at the beginning of the
UpdateStock trigger:

IF UPDATE(ProductID)
BEGIN
RAISERROR('You cannot update ProductID because the action will not be logged.', 16,
1)
ROLLBACK TRANSACTION
RETURN
END

Please let me know what you think.

Otherwise, I like this method.

Anonymous   
Printed Page 169
Bottom of page, and continuing to the next page

This is a trigger to record Updates to a table that I modified from O'Reilly's
"Transact-SQL Cookbook". The goal is to save space in the audit table by only storing
values that have actually changed. When I tried the original version, I discovered
that the trigger would not record the value of a column that was updated from a Null
value to an actual value. I then added some additional logic which records the
changed value regardless of whether the original value was a Null value or an actual
value.

CREATE TRIGGER AuditUpdate
ON [Table]
FOR UPDATE
AS
BEGIN

INSERT INTO AuditTrail(TableName, Action, NetUserID, Node, ActionDate, EventID,
[Column_Name])
SELECT 'TableName', 'O', system_user, host_name(), current_timestamp, newid(),
(CASE WHEN d.[ColumnName]!=i.[ColumnName] THEN d.[ColumnName] ELSE NULL END)
FROM deleted d, inserted i
WHERE d.[ColumnName]=i.[ColumnName]
/* 'O' indicates this row is storing Old values.*/

INSERT INTO AuditTrail(TableName, Action, NetUserID, Node, ActionDate, EventID,
[Column_Name])
SELECT 'TableName', 'N', system_user, host_name(), current_timestamp, newid(),
/* Here is the logic which overcame the problem of not recording a change from a Null
value to an actual value */
(CASE WHEN d.[ColumnName] IS NULL THEN i.[ColumnName] WHEN
d.[ColumnName]!=i.[ColumnName] THEN i.[ColumnName] ELSE NULL END)
FROM deleted d, inserted i
WHERE d.[ColumnName]=i.[ColumnName]
/* 'N' indicates this row is storing New values.*/

END

/* The original logic was as follows: */
/*
(CASE WHEN d.[ColumnName]!=i.[ColumnName] THEN i.[ColumnName] ELSE NULL END)
*/

Anonymous   
Printed Page 242

You claim a 95% confidence value for the t-distribution table, whereas you are actually using 97.5% confidence value.

Anonymous   
Printed Page 258
8.5, calculating the financial median

Adding the case statement to the SELECT will not properly return the median when there is an even number (n) of observations and the two middle observations (n/2 & (n/2+1)) are equal.

Example:
series = {1,2,3,3,4,5}
median = 3
financial median = 3
Transact-SQL Cookbook financial median = 3.5

Anonymous