Tuesday, 24 June 2014

Searching and finding a string value in all columns in a SQL Server table

Problem
Sometimes there is a need to find if a string value exists in any column in your table.  Although there are system stored procedures that do a "for each database" or a "for each table", there is not a system stored procedure that does a "for each column".  So trying to find a value in any column in your database requires you to build the query to look through each column you want to search using an OR operator between each column.  Is there any way this can be dynamically generated?
Solution
Once again this is where T-SQL comes in handy along with the use of system tables or system views.  The code below allows you to search for a value in all text data type columns such as (char, nchar, ntext, nvarchar, text and varchar). 
The stored procedure gets created in the master database so you can use it in any of your databases and it takes three parameters:
  • stringToFind - this is the string you are looking for.  This could be a simple value as 'test' or you can also use the % wildcard such as '%test%', '%test' or 'test%'.
  • schema - this is the schema owner of the object
  • table - this is the table name you want to search, the procedure will search all char, nchar, ntext, nvarchar, text and varchar columns in the table
The first thing you need to do is create this stored procedure by copying the below code and executing it in a query window.
USE master
GO

CREATE PROCEDURE sp_FindStringInTable @stringToFind VARCHAR(100), @schema sysname@table sysname
AS

DECLARE 
@sqlCommand VARCHAR(8000DECLARE @where VARCHAR(8000DECLARE @columnName sysname
DECLARE @cursor VARCHAR(8000)
BEGIN TRY
   SET @sqlCommand 'SELECT * FROM [' @schema '].[' @table '] WHERE'
   
SET @where ''

   
SET @cursor 'DECLARE col_cursor CURSOR FOR SELECT COLUMN_NAME
   FROM ' 
DB_NAME() + '.INFORMATION_SCHEMA.COLUMNS
   WHERE TABLE_SCHEMA = ''' 
@schema '''
   AND TABLE_NAME = ''' 
@table '''
   AND DATA_TYPE IN (''char'',''nchar'',''ntext'',''nvarchar'',''text'',''varchar'')'

   
EXEC (@cursor)

   
OPEN col_cursor   
   FETCH NEXT FROM col_cursor INTO @columnName   

   
WHILE @@FETCH_STATUS 0   
   BEGIN   
       IF 
@where <> ''
           
SET @where @where ' OR'

       
SET @where @where ' [' @columnName '] LIKE ''' @stringToFind ''''
       
FETCH NEXT FROM col_cursor INTO @columnName   
   
END   

   CLOSE 
col_cursor   
   DEALLOCATE col_cursor 

   SET @sqlCommand @sqlCommand @where
   
--PRINT @sqlCommand
   
EXEC (@sqlCommand)  END TRY
BEGIN CATCH
   PRINT 'There was an error. Check to make sure object exists.'
   
IF CURSOR_STATUS('variable''col_cursor') <> -3
   BEGIN
       CLOSE 
col_cursor   
       DEALLOCATE col_cursor 
   END
END 
CATCH
Once the stored procedure has been created you can run some tests.
Here are some tests that were done against the AdventureWorks database.
Find the value 'Irv%' in the Person.Address table.
USE AdventureWorks
GO
EXEC sp_FindStringInTable 'Irv%''Person''Address'

Find the value '%land%' in the Person.Address table.
USE AdventureWorks
GO
EXEC sp_FindStringInTable '%land%''Person''Address'

Find the value '%land%' in the Person.Contact table.
USE AdventureWorks
GO
EXEC sp_FindStringInTable '%land%''Person''Contact'

That's all there is to it.  Once this has been created you can use this against any table and any database on your server.

SQL Server Cursor Example

Problem
In my T-SQL code I always use set based operations.  I have been told these types of operations are what SQL Server is designed to process and it should be quicker than serial processing.  I know cursors exist but I am not sure how to use them.  Can you provide some cursor examples?  Can you give any guidance on when to use cursors?  I assume Microsoft created them for a reason so they must have a place where they can be used in an efficient manner.
Solution
In some circles cursors are never used, in others they are a last resort and in other groups they are used regularly.  In each of these camps they have different reasons for their stand on cursor usage.  Regardless of your stand on cursors they probably have a place in particular circumstances and not in others.  So it boils down to your understanding of the coding technique then your understanding of the problem at hand to make a decision on whether or not cursor based processing is appropriate or not.  To get started let's do the following:
  • Look at an example cursor
  • Break down the components of the cursor
  • Provide additional cursor examples
  • Analyze the pros and cons of cursor usage

Example SQL Server Cursor

Here is an example cursor from tip Simple script to backup all SQL Server databases where backups are issued in a serial manner:
DECLARE @name VARCHAR(50-- database name  DECLARE @path VARCHAR(256-- path for backup files  DECLARE @fileName VARCHAR(256-- filename for backup  DECLARE @fileDate VARCHAR(20-- used for file name
SET @path 'C:\Backup\' 
SELECT @fileDate CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR 
SELECT 
name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb'
OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @name  
WHILE @@FETCH_STATUS 0  
BEGIN  
       SET 
@fileName @path @name '_' @fileDate '.BAK' 
       
BACKUP DATABASE @name TO DISK = @fileName 

       
FETCH NEXT FROM db_cursor INTO @name   END  

CLOSE 
db_cursor  
DEALLOCATE db_cursor

SQL Server Cursor Components

Based on the example above, cursors include these components:
  • DECLARE statements - Declare variables used in the code block
  • SET\SELECT statements - Initialize the variables to a specific value
  • DECLARE CURSOR statement - Populate the cursor with values that will be evaluated
    • NOTE - There are an equal number of variables in the DECLARE <cursor_name> CURSOR FOR statement as there are in the SELECT statement.  This could be 1 or many variables and associated columns.
  • OPEN statement - Open the cursor to begin data processing
  • FETCH NEXT statements - Assign the specific values from the cursor to the variables
    • NOTE - This logic is used for the initial population before the WHILE statement and then again during each loop in the process as a portion of the WHILE statement
  • WHILE statement - Condition to begin and continue data processing
  • BEGIN...END statement - Start and end of the code block
    • NOTE - Based on the data processing multiple BEGIN...END statements can be used
  • Data processing - In this example, this logic is to backup a database to a specific path and file name, but this could be just about any DML or administrative logic
  • CLOSE statement - Releases the current data and associated locks, but permits the cursor to be re-opened
  • DEALLOCATE statement - Destroys the cursor

Monday, 23 June 2014

What is Normalization in SQL ?


Defination : Normalization is the process of efficiently organizing data in a database. There are two goals of the normalization process: eliminating redundant data (for example, storing the same data in more than one table) and ensuring data dependencies make sense (only storing related data in a table). Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is logically stored. There are several benefits for using Normalization in Database.
Benefits :
  1. Eliminate data redundancy
  2. Improve performance
  3. Query optimization
  4. Faster update due to less number of columns in one table
  5. Index improvement
There are diff. - diff. types of Normalizations form available in the Database. Lets see one by one.
1. First Normal Form (1NF)
 First normal form (1NF) sets the very basic rules for an organized database:
  • Eliminate duplicative columns from the same table.
  • Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).
    • Remove repetative groups
    • Create Primary Key

      
NameStateCountryPhone1Phone2Phone3
John1011488-511-3258781-896-9897425-983-9812
Bob1021861-856-6987  
Rob2012587-963-8425425-698-9684 
 PK                 [ Phone Nos ]
   ?? 
IDNameStateCountryPhone 
1John1011488-511-3258 
2John1011781-896-9897 
3John1011425-983-9812 
4Bob1021861-856-6987 
5Rob2012587-963-8425 
6Rob2012425-698-9684 
      
2. Second Normal Form (2NF)Second normal form (2NF) further addresses the concept of removing duplicative data:
·         Meet all the requirements of the first normal form.
·         Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
·         Create relationships between these new tables and their predecessors through the use of foreign keys.   
Remove columns which create duplicate data in a table and related a new table with Primary Key – Foreign Key relationship

IDNameStateCountryPhone
1John1011488-511-3258
2John1011781-896-9897
3John1011425-983-9812
4Bob1021861-856-6987
5Rob2012587-963-8425
6Rob2012425-698-9684
IDNameStateCountryPhoneIDIDPhone
1John101 11488-511-3258
2Bob10221781-896-9897
3Rob20131425-983-9812
42587-963-8425
53587-963-8425
63425-698-9684

3. Third Normal Form (3NF)
Third normal form (3NF) goes one large step further:
·         Meet all the requirements of the second normal form.
·         Remove columns that are not dependent upon the primary key.
  Country can be derived from State also… so removing country
  ID  Name  State  Country
  1  John   101      1
  2  Bob   102      1
  3  Rob   201      2

4. Fourth Normal Form (4NF)
Finally, fourth normal form (4NF) has one additional requirement:
·         Meet all the requirements of the third normal form.
·         A relation is in 4NF if it has no multi-valued dependencies.

If PK is composed of multiple columns then all non-key attributes should be derived from FULL PK only. If some non-key attribute can be derived from partial PK then remove it

The 4NF also known as BCNF NF

   TeacherIDStudentIDSubjectID StudentName
     101  1001  1  John
     101  1002  2  Rob
     201  1002  3  Bob
     201  1001  2  Rob
   TeacherID   StudentID  SubjectID  StudentName
  101  1001  1         X
  101  1002  2         X
  201  1001  3         X
  201  1002  2        X


SQL SERVER – Disk Space Monitoring – Detecting Low Disk Space on Server

CREATE PROCEDURE [CSMSDVLP].[DiskSpaceMonitor] @mailProfile nvarchar(500), @mailto nvarchar(4000), @threshold INT, @logfile nvarchar(40...