My Blog List

LOVE IS LIFE

#htmlcaption1 #htmlcaption2 Stay Connected

Pages

Wednesday, June 18, 2014

String matching with data in SQL server using COLLATE

String matching with data in SQL server using COLLATE


update CLEAN.Product_Ref set laboratories_Clean = UPPER(laboratories_Clean)
where LOCAL_PACK_ID in 
(select LOCAL_PACK_ID from CLEAN.Product_Ref
where laboratories_Clean != UPPER(laboratories_Clean) collate Latin1_General_CS_AI)                                                                                                                                              

HOW TO CHECK IF STRING CONTAINS LOWER CASE LETTER OR UPPER CASE LETTER USING T-SQL

HOW TO CHECK IF STRING CONTAINS LOWER CASE LETTER OR UPPER CASE LETTER USING T-SQL


Use the below SQL query:-

select distinct laboratories_Clean from CLEAN.Product_Ref
where laboratories_Clean != UPPER(laboratories_Clean) collate Latin1_General_CS_AI

Tuesday, June 17, 2014

Populating Slowly Changing Dimensions with SQL Server 2008 MERGE statement

Populating Slowly Changing Dimensions with SQL Server 2008 MERGE statement

MERGE statement

MERGE is a new feature in SQL Server 2008 that provides an efficient way to perform multiple DML operations. Using MERGE statement INSERT, UPDATE or DELETE can be performed on a target table based on the result of a join with a source table.
The MERGE statement has three WHEN clause, each performs a specific DML operation on a given row in the result set
WHEN MATCHED allows you to UPDATE or DELETE the given row in the target table for every row that exists in both source and target table
WHEN NOT MATCHED [BY TARGET] allows you to INSERT a row into the target table for every row that exists in the source table but not in the target
WHEN NOT MATCHED BY SOURCE allows you to UPDATE or DELETE the given row in the target table for every row that exists in target table but not in source table
You can also specify a search condition with each of the WHEN clause to choose the DML operation to be performed on the selected row
OUTPUT clause of the MERGE statement includes a virtual column $action which is used to identify the DML action performed in each row of the result set

Slowly Changing Dimensions

The data in the Slowly Changing Dimensions (SCD) changes periodically. The SCD may be a customer dimension, whereas any new customers can be added or the contact information of the existing customers may be changed.
Earlier this type of dimensions can be handled using multiple DML statements. With MERGE statement in SQL Server 2008 populating SCD can be achieved using a single statement.
Let us take DimCustomer as a dimension table which tracks the customer contact information (phone) for each customer of a particular retail store. Every day thecustomers table in the transactional system is updated with changes to the existing customer or inserting a new customer record. At the end of each month the changes in the customer table are to be applied in the DimCustomer table. A row has to be inserted for each new customer. Existing customers whose contact information changed need to be updated with IsCurrent as 0 and a new record for the existing customer has to be inserted with current contact information.
CustomerKey
CustomerName
Phone
IsCurrent
5011John Smith5650 578 632
1
5012Patrick Brown7451 241 584
1
Table 1: DimCustomer

CustomerKey
CustomerName
Phone
5011John Smith8845 001 863
5013Jimmy7554 846 563
Table 2: Customer
 The following Transact-SQL statement updates the Phone for the customer John Smith and adds a new record for Jimmy in the DimCustomer table.
 INSERT INTO DimCustomer(CustomerKey, CustomerName, Phone, IsCurrent)
  SELECT CustomerKey, CustomerName, Phone, 1
  FROM
    (
        MERGE DimCustomer as trgCustomerDim
        USING Customer AS CustSrc
        ON (trgCustomerDim. CustomerKey = CustSrc. CustomerKey and trgCustomerDim.IsCurrent = 1)
        WHEN MATCHED THEN
            UPDATE SET trgCustomerDim.IsCurrent = 0
        WHEN NOT MATCHED THEN
            INSERT VALUES (CustSrc. CustomerKey, CustSrc. CustomerName, CustSrc. Phone, 1)
        OUTPUT $action, CustSrc. CustomerKey, CustSrc. CustomerName, CustSrc. Phone
    ) AS Changes(action, CustomerKey, CustomerName, Phone)
  WHERE action = ‘UPDATE’;

The output of MERGE statement is
$action
CustomerKey
CustomerName
Phone
UPDATE5011John Smith8845 001 863
INSERT5013Jimmy7554 846 563
The OUTPUT of the MERGE statement is filtered with the action ‘Update’ to insert the new record for the changes in existing customer details. This will make the current information as available and also maintains the old information of the customer.
 The DimCustomer table after processing the MERGE statement would be like this
CustomerKey
CustomerName
Phone
IsCurrent
5011John Smith5650 578 632
0
5012Patrick Brown7451 241 584
1
5013Jimmy7554 846 563
1
5011John Smith8845 001 863
1

Thursday, June 5, 2014

SQL Server 2008 INDEX

SQL Server Indexing features - 

SQL server gave us the feature of indexing for speeding up the performance in how we retrieve data through queries. Imagine we have a book with many chapters, if we are required to turn pages and find a particular chapter of our interest it would take more time.

Indexing in a book gives a flexibility to look-up the page number of the book and directly go to that page number to find our chapter of interest and hence in a way more quick.

Similarly we have indexing in SQL server to retrieve data based a key value.


Types of indexes in SQL server
  • Clustered
  • Non-clustered
  • Unique
  • Filtered
  • XML
  • Full text
  • Spatial
  • Column store
  • Index with included columns
  • Index on computed columns
Today we will be discussing about Clustered, Non-Clustered and Unique indexes.

Clustered Index - 

It determines the way in which the data is physically ordered in a table. A table can have only one clustered index.

To understand this better let us create a table as shown below :-

Create table tblEmployee
(
[EmpID] int primary key,
[EmpName] varchar(20),
[Gender] char(1),
[EmpSalary] int
)

After creation of the table open "Object Explorer", expand tables in the database where the table tblEmployee was created expand "Key" and "Indexes", you can see the creation of primary key and an Index.

So we can now conclude for the above example that creation of primary key creates clustered index by default. Please see below screenshot for reference.









So whats actually the use of this clustered index creation!!!

As we stated above that indexing determines the way data is physically stored in a database, in the below example we will see how it stores the data.


In the creation of the table tblEmployee we have used EmpID as the primary key and the clustered index is created on this column. Lets try to insert values in the table tblEmployee.

Insert into tblEmployee values(3,'Indranil','M',25000)
Insert into tblEmployee values(1,'Vipul','M',35000)
Insert into tblEmployee values(5,'Deepika','F',18000)
Insert into tblEmployee values(4,'Mukul','M',19000)
Insert into tblEmployee values(2,'Shikha','F',21000)


We can see that while insertion of values in the table, the order of the EmpID is not inserted orderly, but as we have the clustered index on the EmpID listed, when we view the data through select statement, the data are automatically ordered in ascending order of EmpID. So the clustered index on EmpID helped the data get stored in accordance to ascending order of EmpID.

select * from tblEmployee







Thursday, January 9, 2014

SQL Server 2008 Auditing - For SQL Jobs (Delete and Create)

SQL Server 2008 Auditing


I recently got presented with a challenge: How do you monitor people creating or deleting a job.  just be able to monitor and find out who created or deleted the jobs and what time?


What actually is SQL Server Auditing??

It is a secure means to track who created a schema, table, SQL server agent jobs etc..
If someone makes changes to a table schema, or has created a new one it is easy to track those and make a record without notifying the user making the changes.


How to Audit SQL Server Agent Jobs??

Step 1:-  Open your database with log in name as "sa" as you will require administrator rights to create the audit feature

Step 2:-  Expand "Security", Right click on "Audit" and select "New Audit".

Step 3:- Enter the Audit name. Here in example the Audit name is taken as "SqlAgentObjectAccess_Audit"

Step 4:- Let the queue delay be 1000

Step 5:- Enter Audit destination as "Application Log" and Click "OK"

See the screenshot for reference - 








You can Also use the below T-SQl Command to create the audit feature



use master
GO  
-- Create a Server Audit to log all audit events to Windows Application Log  
CREATE SERVER AUDIT [SqlAgentObjectAccess_Audit]  
TO APPLICATION_LOG 
 WITH 
 (    QUEUE_DELAY = 1000 
       ,ON_FAILURE = CONTINUE 
         ,AUDIT_GUID = 'e1f7d882-b26e-4b70-bc03-87af197eb7de' 
  ) 
 ALTER SERVER AUDIT [SqlAgentObjectAccess_Audit] WITH (STATE = ON) 
 GO


Note that by default SQL Server Audits are disabled when they are created, which means nothing will be written to your log until until it is enabled. This can be done just by expanding the "Audit" and then right clicking on the new audit created and choosing “Enable”.


NEXT STEP:-

The next bit is to set up the Database Audit on the SQL Agent Jobs table

Execute the below T-SQl command to set the Database Audit on the SQL Agent Jobs table


-- Create Database Audit specification to audit all execute calls initiated by dbo 

USE [msdb]
GO

CREATE DATABASE AUDIT SPECIFICATION [SqlAgentObjectAccess_Audit_MSDB]
FOR SERVER AUDIT [SqlAgentObjectAccess_Audit]
ADD (EXECUTE ON OBJECT::[dbo].[sp_add_job] BY [dbo]),
ADD (EXECUTE ON OBJECT::[dbo].[sp_add_job] BY [SQLAgentUserRole]),
ADD (EXECUTE ON OBJECT::[dbo].[sp_delete_job] BY [dbo]),
ADD (EXECUTE ON OBJECT::[dbo].[sp_delete_job] BY [SQLAgentUserRole])
WITH (STATE = ON)
GO

Testing desired changes for Audit feature

Create a job and then delete the job. Go to control panel -> System and Security -> Open Windows Event log viewer -> Open Application Log.

You will see one log entry for sp_add_job call and another log entry for sp_delete_job call


Note: The SQL Server Audit feature does not work with SQL Server standard edition









  


Tuesday, October 1, 2013

SQL 2008 trim leading space, white space, tab

CREATE FUNCTION dbo.LTrimX(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @trimchars VARCHAR(10)
SET @trimchars = CHAR(9)+CHAR(10)+CHAR(13)+CHAR(32)
IF @str LIKE '[' + @trimchars + ']%' SET @str = SUBSTRING(@str, PATINDEX('%[^' + @trimchars + ']%', @str), 8000)
RETURN @str
END
GO
CREATE FUNCTION dbo.RTrimX(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @trimchars VARCHAR(10)
SET @trimchars = CHAR(9)+CHAR(10)+CHAR(13)+CHAR(32)
IF @str LIKE '%[' + @trimchars + ']'
SET @str = REVERSE(dbo.LTrimX(REVERSE(@str)))
RETURN @str
END
GO
CREATE FUNCTION dbo.TrimX(@str VARCHAR(MAX)) RETURNS VARCHAR(MAX)
AS
BEGIN
RETURN dbo.LTrimX(dbo.RTrimX(@str))
END
GO
/* Run the created function */
SELECT dbo.TRIMX('         word leading trailing spaces           ') AS 'TrimmedWord'
GO

http://youtu.be/1iVZFeL7IOE

Thursday, September 5, 2013

A beautiful morning in Bangalore

A Beautiful Morning in Bangalore

It was a beautiful morning in Bangalore. My name is Neha and I was lying on the bed and looking outside of the window. I saw a beautiful tree with purple flowers and sun shining brightly. I closed my eyes and I heard somebody shouting. I looked through the window and tried to identify what the commotion was all about. I saw my brother four years younger to me, amidst all of these confusion and chaos down below. As my parents were out of country it was me to take care of him. As I reached outside my house I came to know last night my brother had an accident while driving car with an auto driver and the auto driver trailed him since then and reached my house and demanded money. I yelled to my brother "Yeh sab log kuch bhi kar sakte hai paise ke liye, Paise de doh aur ghar ke andar aa jao"(These drivers can do anything for money, give them the money and come inside). He did as I said.

While serving him breakfast I asked him "what was the matter with the auto driver?” He replied that it was not his fault while narrating the entire story. My brother had boozed the previous night with his friends and had a small accident. I loved my brother a lot, just told him not to worry, he smiled back and we returned to our normal daily chores. I could see he was happy as I didn’t scold him for this entire matter and I love to see those smiles on his face.

It was December 14th, tomorrow is my best friend Tyagi’s birthday. We all friends planned to have the birthday celebration at my friend Sneha’s farmhouse. Lots of things to do and time was less. But finally we made all our arrangements. We all friends gathered together and right at midnight Tyagi cut the cake and party began. It was 2:00am all had lots of fun and were prepared to leave. I called up my brother to pick me up. On the way back to our home I was explaining him what all we did in the party, how much fun we had but noticed that hardly he was listening. Asked him “Are you alright?” to which he replied murmuring “haan thik hun” (I am alright). I turned on the music and continued with the party mood. Suddenly there was a flash of light, all things went blank, no sound, and no music.

A morning in Bangalore, I am lying on bed, looking outside the window. I see a beautiful tree with purple flowers and sun shining brightly. No one is shouting, there is silence all around I closed my eyes, tears falling down my cheeks.