Pages

Saturday, March 23, 2013

Sybase - Store Procedure for finding any text in entire database

Today, I am gonna show how to search any text in entire database in Sybase.

sybase sybase, sybase
CREATE PROCEDURE "DBA"."sp_get_data"(as_search   varchar(2000))

BEGIN
    DECLARE cur_test cursor for select table_Name, column_name from Table_List ;
   
    DECLARE ls_sql varchar(20000);
    DECLARE ls_table_name varchar(80);
    DECLARE ls_column_name varchar(80);
    DECLARE ld_count Numeric(10,0);   

    If Exists (SELECT 1 FROM sysobjects WHERE name ='Table_List' AND Type='U') Then
        Drop Table Table_List;
     End IF;

    SELECT
        sys.SYSTABLE.table_name,
        sys.SYSTABCOL.column_name    
    Into Table_List
    FROM
        sys.SYSTABLE
    Inner Join
         sys.SYSTABCOL on sys.SYSTABLE.table_id = sys.SYSTABCOL.table_id
    Where
        sys.SYSTABLE.table_type = 'BASE'
        AND sys.SYSTABLE.creator <> 0
        AND (sys.SYSTABCOL.base_type_str like '%char%' OR sys.SYSTABCOL.base_type_str like '%binary%' OR sys.SYSTABCOL.base_type_str like '%bit%' OR sys.SYSTABCOL.base_type_str like '%xml%');  

    If Exists (SELECT 1 FROM sysobjects WHERE name ='table_track' AND Type='U') Then
        Drop Table table_track;
     End IF;

    CREATE TABLE table_track
    (
        Table_name varchar(200),
        Column_name varchar(200)
    );

    If Exists (SELECT 1 FROM sysobjects WHERE name ='sql_execution_script' AND Type='U') Then
        Drop Table sql_execution_script;
     End IF;

    CREATE TABLE sql_execution_script
    (
        Sql_syntax varchar(200)
    );

    Open cur_test;
    lp: loop
     Fetch Next cur_test into ls_table_name, ls_column_name;  
        If SQLCODE <> 0 Then
            LEAVE lp
        End If;
   
   
    Set ls_sql = 'select count(*) into ld_count from ' + ls_table_name + ' where "' + ls_column_name + '" like ''%'+ as_search + '%''';
    INSERT INTO sql_execution_script VALUES (ls_sql);  
    EXECUTE (ls_sql);
    If SQLCODE <> 0 Then
        return -1;
    End If;

    If ld_count <> '' and ld_count is not null  Then
        If ISNULL(ld_count,0) > 0 Then
            Insert into table_track values (ls_table_name,ls_column_name)
        END If;
    END If ;
   
    End Loop;
    Close cur_test;
    Deallocate cur_test;

    Select * from table_track;
END;

------------------------------------------

For finding executed SQL Script: 

Select * from sql_execution_script;

Monday, October 29, 2012

Soundex in SQL ( Phonetic algorithm)

Introduction

phonetic algorithm matches two different words with similar pronunciation to the same code, which allows phonetic similarity based word set comparison and indexing.

Often it is quite difficult to find atypical name (or surname) in the database, for example:

— Hey, John, look for Adolf Schwarzenegger.
— Adolf Shwardseneger? There is no such person!
In this case, the use of phonetic algorithms can significantly simplify the problem.

Soundex algorithm is very useful for searching in lists of people in databases, as well as for using in a spell checker. They are often used in combination with the algorithms of fuzzy search, providing users with a handy search by name and surname in databases, lists of people and so on.

soundex sql soundex sql soundex sql soundex sql soundex in sql soundex in sql soundex in sql soundex in sql

Soundex

One of the first algorithms was Soundex invented in the 1910s by Robert Russell. This algorithm (its American version) matches words to the numerical index like A126. Its working principle is based on the partition of consonants in groups with ordinal numbers, which are then compiled to the resulting value. Later several improvements was suggested.


Soundex Coding Guide


Number Represents the Letters
1 B, F, P, V
2 C, G, J, K, Q, S, X, Z
3 D, T
4 L
5 M, N
6 R

Disregard the letters A, E, I, O, U, H, W, and Y. 


Every soundex code consists of a letter and three numbers, such as W-252. The letter is always the first letter of the surname. The numbers are assigned to the remaining letters of the surname according to the soundex guide shown below. Zeroes are added at the end if necessary to produce a four-character code. Additional letters are disregarded. 

Examples:
  • Washington is coded W-252 (W, 2 for the S, 5 for the N, 2 for the G, remaining letters disregarded).
  • Lee is coded L-000 (L, 000 added).
image  
Examples :
soundex sql server soundex sql server soundex code soundex code soundex code soundex code
F234 → Fusedale.
G535 → Genthner, Gentner, Gianettini, Gunton.
G640 → Garlee, Garley, Garwell, Garwill, Gerrell, Gerrill, Giral, Gorelli, Gorioli, Gourlay, Gourley, Gourlie, Graal, Grahl, Grayley, Grealey, Greally, Grealy, Grioli, Groll, Grolle, Guerola, Gurley.
H326 → Hadcroft, Hadgraft, Hatchard, Hatcher, Hatzar, Hedger, Hitscher, Hodcroft, Hutchcraft.
P630 → Parade, Pardew, Pardey, Pardi, Pardie, Pardoe, Pardue, Pardy, Parradye, Parratt, Parrett, Parrot, Parrott, Pearde, Peart, Peaurt, Peert, Perdue, Peret, Perett, Perot, Perott, Perotti, Perrat, Perrett, Perritt, Perrot, Perrott, Pert, Perutto, Pirdue, Pirdy, Pirot, Pirouet, Pirt, Porrett, Porritt, Port, Porte, Portt, Prate, Prati, Pratt, Pratte, Pratty, Preddy, Preedy, Preto, Pretti, Pretty, Prewett, Priddey, Priddie, Priddy, Pride, Pridie, Pritty, Prott, Proud, Prout, Pryde, Prydie, Purdey, Purdie, Purdy.


soundex number soundex number soundex number soundex number soundex sql server soundex sql server 

Soundex in SQL:

ALTER FUNCTION fn_soundex(@Getname VARCHAR(500))
RETURNS VARCHAR(500)
BEGIN

    -- To Store First Letter

    DECLARE @GetFirstLetter VARCHAR(20);    
    
    -- To Store Soundex Code
    DECLARE @SetSoundexCode VARCHAR(20);    
    
    -- To Find length of string
    DECLARE @Code_len INT;                    

    -- Default Value

    SET @SetSoundexCode = '0'                        
    
    -- Get First Letter
    SET @GetFirstLetter = substring(@Getname,1,1)    
    
    -- Copy Remain Character to another String
    SET @Getname = substring(@Getname,2,len(@Getname))    
    
    -- Assign Code according to Letter
    
    SET @Getname = replace(@Getname,'A',0)
    SET @Getname = replace(@Getname,'E',0)
    SET @Getname = replace(@Getname,'I',0)
    SET @Getname = replace(@Getname,'O',0)
    SET @Getname = replace(@Getname,'U',0)
    SET @Getname = replace(@Getname,'H',0)
    SET @Getname = replace(@Getname,'W',0)
    SET @Getname = replace(@Getname,'Y',0)
    
    SET @Getname = replace(@Getname,'B',1)
    SET @Getname = replace(@Getname,'F',1)
    SET @Getname = replace(@Getname,'P',1)
    SET @Getname = replace(@Getname,'V',1)

    SET @Getname = replace(@Getname,'C',2)

    SET @Getname = replace(@Getname,'G',2)
    SET @Getname = replace(@Getname,'J',2)
    SET @Getname = replace(@Getname,'K',2)
    SET @Getname = replace(@Getname,'Q',2)
    SET @Getname = replace(@Getname,'S',2)
    SET @Getname = replace(@Getname,'X',2)
    SET @Getname = replace(@Getname,'Z',2)

    SET @Getname = replace(@Getname,'D',3)

    SET @Getname = replace(@Getname,'T',3)
    
    SET @Getname = replace(@Getname,'L',4)
    
    SET @Getname = replace(@Getname,'M',5)
    SET @Getname = replace(@Getname,'N',5)
    
    SET @Getname = replace(@Getname,'R',6)
    
    -- Get Length of String
    SET @Code_len = len(@Getname)            
    
    -- Remove Space from both side
    SET @Getname = ltrim(rtrim(@Getname))    

    -- Check if length is greater then zero

    WHILE @Code_len > 0
    BEGIN
        -- Compare two letter at same time to check duplicate
        -- If character is repeating character then make it Zero...
        -- We will remove Zero at the End
        IF charindex(substring(@Getname,@Code_len,1),substring(@Getname,@Code_len-1,1)) > 0  
            SET @SetSoundexCode = @SetSoundexCode + '0'
        ELSE
            SET @SetSoundexCode = @SetSoundexCode + substring(@Getname,@Code_len,1)
        
        SET @Code_len = @Code_len - 1
    END

    SET @SetSoundexCode = @GetFirstLetter + reverse(replace(@SetSoundexCode,'0',''))+'000'


    RETURN LEFT(@SetSoundexCode,4)


END

GO


There are lot many variance in Soundex Search algorithm. 
Referenced from SOUNDEX (Transact-SOL)     soundex sql server soundex sql server
Useful Link:
http://www.archives.gov/research/census/soundex.html
soundex function soundex function soundex function soundex function sql soundex sql soundex sql soundex sql soundex

Saturday, October 20, 2012

SQLDbx Step by Step

Hello Guys,

As I promised you, Today I will give you detail information on SQLDbx Tool step by step.
  • SQLDbx is too much portable.
  • With SQLDbx, you can connect to any database listed below in image.
 
  • As showing in Image, It will show you connected Server name in Server Panel.
  • It allows you to change database directly from Database Selection Panel and Search in Text area from Search Panel.
  • It allows you to filter table and other objects from left panel where filter icon is available for selected objects.
  • It also provides Auto Intellisense.

  • It allows to transpose data in Result tab and to export data from Result tab.
 
  • It allows to Freeze column in Result tab.
  • It shows data type for each column in Result tab and it allows to sort data by clicking on Column header.

  • It allows to export data directly from Result grid as well as allows to single click data export in various format.

  • It allows to generate various scripts from Result tab directly like Select, Insert, Delete, Update etc...


  • It allows to change date format and other format into result tab directly.
 
  • It can generate Select, Insert, Update script with single click based on table.



These is all about SQLDbx what i know.
Please feel free to email me for any problem or concern.
what is sql server            what is sql server            what is sql server                 what is sql server       software database           software database             software database                 software database            database software           database software             database software                database software      sql tools       sql tools       sql tools       sql tools       tool for sql     tool for sql     tool for sql     tool for sql     

    Thursday, October 18, 2012

    Nice Tool for Database Developer

    SQLDbx

    SqlDbx is a fast and easy to use database SQL development software.

    You can connect to any database with the help of SQLDbx.

    Features:

    • You can connect major Database Servers including Microsoft, Sybase ASE, Oracle and IBM DB2/UDB
    • Auto Intellisense
    • SQLDbx have facility to auto complete word and Intellisense to find Table and Column accordingly.
    • Automatically highlight matching parenthesis for BEGIN/END, IF/END IF, CASE/END, ... SQL blocks
    • It supports x86 and x64 Versions
    • It can generate Select, Insert, Update, Create data modification script from result grid.
    • It allows to sort data directly in result grid and even we can export data to Excel and CSV format directly from result grid.
    • It stored all SQL executed statements.
    • With SQLDbx, It is very easy to filter Table, Store Procedure, Views, System Objects.
    • We can directly transpose data in Result Tab.
    • It allows you to freeze column in Result Tab.
    • It allows to update data directly in Result Grid.
    • It allows you to see result in text format or Tabular Grid
    • SQLDbx is very Portable (Max 2MB) 
    I am lover of this software...

    Link to Download SQLDbx Personal Edition...
    SQLDbx Personal Edition

    I will give more information on SQLDbx with screen shot in later post.
    what is sql server            what is sql server            what is sql server                 what is sql server       software database           software database             software database                 software database            database software           database software             database software                database software      sql tools       sql tools       sql tools       sql tools       tool for sql     tool for sql     tool for sql     tool for sql