concept amount in category sql

This is an excerpt from Manning's book SQL Server DMVs in Action: Better Queries with Dynamic Management Views.
Table 1.1. The major DMV groups
Any system sessions that have a session_id of 50 or less are filtered out. The amount of space used by a session is calculated by summing user_objects_alloc_page_count and internal_objects_alloc_page_count. Similarly, the amount of space not reclaimed is calculated as the difference between user_objects_alloc_page_count and user_objects_dealloc_page_count added to the difference between internal_objects_alloc_page_count and internal_objects_dealloc_page_count. The results are sorted by the amount of space each session still owns. An example of the type of output for this query is shown in figure 9.5.
The output shows, for completed batches, the amount of space in tempdb that each session still has associated with it, along with details of the session and connection that allow you to report on where the query is running from (host_name), who is running the SQL query (login_name), and the text of the last-completed SQL query. If space usage is a problem (as reported in the SQL Server error log), you can use the output to determine which session/user/SQL query is using the most space, with a view to taking corrective action. Such action includes rescheduling the queries to run at a time when more resources are available or rewriting the relevant query to use less tempdb space, for example, replacing a hash join with a nested loop join.
Here we’ll examine and discuss the importance of tempdb, see how to determine the amount of total and free space it has, and drill down into the amount of space used by the different types of objects it contains.

This is an excerpt from Manning's book SQL Server MVP Deep Dives.
Listing 2. Trigger to ensure line item total > 0
CREATE TRIGGER dbo.invoiceLineItem$UPDATETrigger ON dbo.invoiceLineItem AFTER UPDATE AS BEGIN DECLARE @rowsAffected int, --stores the number of rows affected @msg varchar(2000) --used to hold the error message SET @rowsAffected = @@rowcount --no need to continue on if no rows affected IF @rowsAffected = 0 return SET NOCOUNT ON --to avoid the rowcount messages SET ROWCOUNT 0 --in case the client has modified the rowcount BEGIN TRY --[validation section] IF EXISTS( SELECT * FROM ( SELECT sum(amount) as total FROM invoiceLineItem WHERE invoiceId in (SELECT invoiceId FROM inserted UNION ALL SELECT invoiceId FROM deleted) GROUP BY invoiceId) as totals WHERE totals.total < 0) RAISERROR ('The sum of amounts for the item must be > 0',16,1) --[modification section] INSERT INTO invoiceLog(invoiceNumber, action, changedByLoginId) SELECT invoice.invoiceNumber, 'Added LineItems',original_login_id FROM inserted JOIN invoice ON inserted.invoiceId = invoice.invoiceId END TRY BEGIN CATCH IF @@trancount > 0 ROLLBACK TRANSACTION --[log errors if desired] DECLARE @ERROR_MESSAGE nvarchar(4000) SET @ERROR_MESSAGE = ERROR_MESSAGE() RAISERROR (@ERROR_MESSAGE,16,1) END CATCH END
It won’t come as a surprise that you’ll also have to test for dependencies on three or more attributes. But these are increasingly rare as the number of attributes increases, so you should make a trade-off between the amount of work involved in testing all possible combinations on one hand, and the risk of missing a dependency on the other. The amount of work involved is often fairly limited, because in the previous steps you’ll often already have changed the model from a single many-attribute relation to a collection of relations with only a limited number of attributes each, and hence with a limited number of possible three-or-more-attribute combinations.