Friday, June 11, 2010

SS SLUG: June 2010 Meetup



SS SLUG: June 2010 Meetup

It's time again for another edition of the SQL Server Sri Lanka User Group meeting. Please see image below for all the details.



Sunday, May 16, 2010

OUTPUT Clause (Transact-SQL)

This Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. The results can also be inserted into a table or table variable. Additionally, you can capture the results of an OUTPUT clause in a nested INSERT, UPDATE, DELETE, or MERGE statement, and insert those results into a target table or view.

Here I use the AdventureWorks sample database to show the sample T SQL for OUTPUT clause.

USE AdventureWorks;

BEGIN TRANSACTION;

DECLARE @testoutput TABLE
(
SalesOrderID int
, SalesOrderDetailID int
, QtyBefore int
, QtyAfter int
, ModifiedDate datetime2
, UserName varchar(30)
)

SELECT * FROM @testoutput;

UPDATE Sales.SalesOrderDetail
SET OrderQty = 5

OUTPUT inserted.SalesOrderID
, inserted.SalesOrderDetailID
, deleted.OrderQty
, inserted.OrderQty
, GETDATE()
, SUSER_NAME()
INTO @testoutput
WHERE SalesOrderID = 43659 AND SalesOrderDetailID = 1;


SELECT * FROM @testoutput;

ROLLBACK TRANSACTION;

SELECT OrderQty FROM Sales.SalesOrderDetail WHERE SalesOrderID = 43659 AND SalesOrderDetailID = 1;

Friday, May 14, 2010

SSSLUG May 2010 Meetup


We have organized the May 2010 meetup of the SS SLUG for May 19. Please see below details regarding it. If you are in the Colombo area on that day, we would be happy to see you there. For more information checkout the SS SLUG home page.




Sunday, April 25, 2010

Basic transaction concepts

This is mostly referred as "ACID" properties.

So, what are they?

  • Atomicity - When Two or more transactions are taking place, either all of them are committed or none of them are committed.

  • Consistency - That is, after a transaction, valid and new data exists in the database or the data returned to its original state. ROLLBACK function provides the facility of returning data to its original state.
  • Isolation – During a transaction, data must be in an isolated level there by not allowing other transactions to access it.
  • Durability - Data is still available even if the server fails. This is achieved by CHECKPOINTS and DATABASE RECOVERY PROECESS.

Monday, March 29, 2010

Correlated Subqueries

Any queries can be evaluated by executing the subquery once and substituting the resulting value or values into the WHERE clause of the outer query. In queries that include a correlated subquery (also known as a repeating subquery), the subquery depends on the outer query for its values. This means that the subquery is executed repeatedly, once for each row that might be selected by the outer query.

This query retrieves one instance of each employee's first and last name for which the bonus in the SalesPerson table is 5000 and for which the employee identification numbers match in the Employee and SalesPerson tables.


USE AdventureWorks;
GO
SELECT DISTINCT c.LastName, c.FirstName, e.EmployeeID
FROM Person.Contact AS c JOIN HumanResources.Employee AS e
ON e.ContactID = c.ContactID
WHERE 5000.00 IN
(SELECT Bonus
FROM Sales.SalesPerson sp
WHERE e.EmployeeID = sp.SalesPersonID) ;
GO
And the result is,
LastName FirstName EmployeeID
------------------ -- ------------------ -----------------
Ansman-Wolfe Pamela 280
Saraiva José 282

(2 row(s) affected)
Correlated subqueries can also include table-valued functions in the FROM clause by referencing columns from a table in the outer query as an argument of the table-valued function. In this case, for each row of the outer query, the table-valued function is evaluated according to the subquery.