concept application in category sql

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.
The clustered index of an ever-increasing value that never changes will never get fragmented by INSERTs or UPDATEs (except UPDATEs that increase row size), thus reducing the need for index maintenance. A clustered index requires an exclusive, table-level lock in order to be rebuilt. Consequently, during a clustered index rebuild, table data is unavailable to the application except for transactions that are using the read-uncommitted isolation level. Therefore, using IDENTITY keys as clustered primary keys can reduce potential downtime.
Listing 5. Creating support objects in the database on the DataReceiver instance
USE DBSender GO -- enable service broker for our database ALTER DATABASE DBSender SET ENABLE_BROKER -- get the Service broker ID to use later SELECT service_broker_guid FROM sys.databases WHERE name = 'DBSender' -- returns 030F00E8-386F-46AF-8622-655E320D2A1C on my machine GO -- create the schema that holds service broker support objects CREATE SCHEMA DataTransfer -- create table to hold errors when sending messages CREATE TABLE DataTransfer.SendErrors ( ID BIGINT IDENTITY(1, 1) PRIMARY KEY, ErrorProcedure NVARCHAR(128) NOT NULL, ErrorLine INT NOT NULL, ErrorNumber INT NOT NULL, ErrorMessage NVARCHAR(MAX) NOT NULL, ErrorSeverity INT NOT NULL, ErrorState INT NOT NULL, TransferData XML NOT NULL, ErrorUTCDate DATETIME NOT NULL DEFAULT GETUTCDATE() ) GO -- stored procedure that sends data in the form of XML payload CREATE PROCEDURE DataTransfer.spSendTransferData @xmlData XML, @dlgId UNIQUEIDENTIFIER OUTPUT AS BEGIN BEGIN TRY -- begin the dialog. -- if @dlgId is NULL a new conversation is started -- if it's not null the conversation is reused BEGIN DIALOG CONVERSATION @dlgId FROM SERVICE [//DataTransfer/DataSender] TO SERVICE '//DataTransfer/DataReceiver', -- tells us to which SB instance we're sending to -- change to DataSenderDB service broker guid '962FF4B4-428B-482D-A0A7-748F62D3C41C' ON CONTRACT [//DataTransfer/Contract] WITH ENCRYPTION = OFF; -- Send our data to be DataTransfered ;SEND ON CONVERSATION @dlgId MESSAGE TYPE [//DataTransfer/Message] (@xmlData) END TRY BEGIN CATCH INSERT INTO DataTransfer.SendErrors ( ErrorProcedure, ErrorLine, ErrorNumber, ErrorMessage, ErrorSeverity, ErrorState, TransferData) SELECT ERROR_PROCEDURE(), ERROR_LINE(), ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_SEVERITY(), ERROR_STATE(), @xmlData -- here you can add extra logging to the error log -- or email notifications, etc... END CATCH END GO -- sproc that does some work and sends data. -- it's an entry point and we call this sproc -- from an application or a job CREATE PROCEDURE DataTransfer.spDoWork AS /* In the project I worked on we looped over the tables, got the needed data and sent it. So this is just META CODE! */ WHILE Looping over the tables BEGIN -- create the xml message we'll send SELECT @msg = CONVERT(XML, '<M TableSchema="' + @TableSchema + '" TableName="' + @TableName + '"><rows>Rows in XML go here</rows></M>') -- if our message is not null let's send it using our stored -- procedure for it. IF (@msg IS NOT NULL) EXEC DataTransfer.spSendTransferData @msg, @dlgId OUT END -- after we've transferred all the data -- send the conversation is over message so the DataReceiver instance -- can end it ;SEND ON CONVERSATION @dlgId MESSAGE TYPE [//DataTransfer/EndOfTransfer]The stored procedure spDoWork is the entry point to the Service Broker infrastructure. Everything else is transparent to the application. On a project I was working on, we used it to 1) check for changed rows in each table that we had to transfer over to the DataReceiver instance, 2) get those rows, 3) send per table changes in one message, and 4) send the [//DataTransfer/EndOfTransfer] message when we processed all the tables.
Having to do this in the application would require huge numbers of changes. This way we could abstract it all inside the database. That’s one of the benefits of having an asynchronous messaging system inside the database. The code in the next listing creates the Service Broker objects on the DataSender instance.

This is an excerpt from Manning's book SQL Server MVP Deep Dives.
The best place for business logic is in the data. Every variable in the business formula should be stored in the database rather than coded in any programming language. The behavior of the application is then determined not by .NET code, or T-SQL case expressions, but by the data. Joining from the current data set to the business-rules data sets can dynamically plug the correct values into the business formula for the appropriate row. An admin application can modify the business-rule data at any time without code changes. This is the freedom and elegance that a data-driven database adds to the application.
Another interesting property of the SqlException class is the Errors property. This is a collection of SqlError objects. The SqlError class includes only the SQL Server–specific properties from the SqlException object that are listed in table 1. Because a batch of SQL can generate multiple SQL Server errors, an application needs to check whether multiple errors have occurred. The first error in the Errors property will always match the error in the SqlExcpetion’s properties. Listing 11 is an example.
Listing 4. XML result
<RingBufferTarget eventsPerSec="0" processingTime="0" totalEventsProcessed="1" eventCount="1" droppedCount="0" memoryUsed="424"> <event name="deprecation_final_support" package="sqlserver" id="207" version="1" timestamp="2008-10-30T08:54:31.689Z"> <data name="feature"> <type name="unicode_string" package="package0" /> <value>DATABASEPROPERTY</value> <text /> </data> <data name="message"> <type name="unicode_string" package="package0" /> <value>DATABASEPROPERTY will be removed in the next version of SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use it.</value> <text /> </data> </event> </RingBufferTarget>