Monday, May 20, 2013

Way to search string in all tables in database

 

Some time we get requirement to find a particulare sting across tables in a data base.
One of my friend was trapped in same situation this the first solution we found on net


Search string in all tables-Ist Solution

but not tried yet we'll continue work on this and let you know once we get something new.
Here is a way to search string in all tables in database this is one of the solution tried by my friend Rohit Rao and it is working solution all credit goes to him.


DECLARE @SearchStr NVARCHAR(100) 

SET @SearchStr = 'F500P' 

CREATE TABLE #results 
  ( 
     columnname  NVARCHAR(370), 
     columnvalue NVARCHAR(3630) 
  ) 

SET nocount ON 

DECLARE @TableName  NVARCHAR(256), 
        @ColumnName NVARCHAR(128), 
        @SearchStr2 NVARCHAR(110) 

SET @TableName = '' 
SET @SearchStr2 = Quotename('%' + @SearchStr + '%', '''') 

WHILE @TableName IS NOT NULL 
  BEGIN 
      SET @ColumnName = '' 
      SET @TableName = (SELECT Min(Quotename(TABLE_SCHEMA) + '.' 
                                   + Quotename(TABLE_NAME)) 
                        FROM   INFORMATION_SCHEMA.TABLES 
                        WHERE  TABLE_TYPE = 'BASE TABLE' 
                               AND Quotename(TABLE_SCHEMA) + '.' 
                                   + Quotename(TABLE_NAME) > @TableName 
                               AND Objectproperty(Object_id(Quotename(TABLE_SCHEMA) + '.' 
                                                            + Quotename(TABLE_NAME)), 'IsMSShipped') = 0) 

      WHILE ( @TableName IS NOT NULL ) 
            AND ( @ColumnName IS NOT NULL ) 
        BEGIN 
            SET @ColumnName = (SELECT Min(Quotename(COLUMN_NAME)) 
                               FROM   INFORMATION_SCHEMA.COLUMNS 
                               WHERE  TABLE_SCHEMA = Parsename(@TableName, 2) 
                                      AND TABLE_NAME = Parsename(@TableName, 1) 
                                      AND DATA_TYPE IN ( 'char', 'varchar', 'nchar', 'nvarchar', 
                                                         'int', 'decimal' ) 
                                      AND Quotename(COLUMN_NAME) > @ColumnName) 

            IF @ColumnName IS NOT NULL 
              BEGIN 
                  INSERT INTO #results 
                  EXEC ( 'SELECT ''' + @TableName + '.' + @ColumnName + ''', LEFT(' + @ColumnName + ', 3630) FROM ' + @TableName + ' (NOLOCK) ' + ' WHERE ' + @ColumnName + ' LIKE ' + @SearchStr2 ) 
              END 
        END 
  END 

SELECT columnname, 
       columnvalue 
FROM   #results 

DROP TABLE #results 


Happy Living...
Happy Concepts...
Happy Programming...
 

 

Tuesday, April 16, 2013

Refresh or Reload showModalDialog

The process of Refreshing or Reloading showModalDialog is simple if know how to do that. But if you are looking for a solution it make be pain full task. so this blog is to help you how can you refresh or reload popup window with minimal code.

First of all showModalDialog doesn't feel that way.
steps:

1. Add
<html>
<head>
< base target="_self" />
.....
.....
</head>
By default target is set "_bank".
2.
add link to page as hidden
<a href=”" id=”PageLink” style=”display:none;”>
3.
add url to this page
document.getElementById('PageLink').href = 'test.html';
document.getElementById('PageLink').click();
 
 
Happy Living....
Happy Concepts....
Happy Programming ..... 

Friday, January 25, 2013

Align Sql script in Any Database

Scenario:
Today , while i was working with a large Stored Procedure i felt need of a way to auto align in sql server.

Approach:
 I searched for same on internet and got to know there isn't any short-key provided by Microsoft Sql server. While searching for an add-in there wasnt anything suggested by microsoft site of community provided. At the and i decided to use one of online formater and i got to know http://www.dpriver.com/pp/sqlformat.htm . It is good enough to handle most of the things.

Steps to Use:
1. Visit Link http://www.dpriver.com/pp/sqlformat.htm
2. Select Database from dropdown MSSQL
3. Copy text of a valid T-SQL script (SP or Table) needed to be formated and paste on First Text Area.
4. Check default settings at right side of page (you can leave everything as it is ) . There is one thing defiantly required to change is value in last Textbox (80 to 500 in my case). otherwise it will break one statement into multiple lines.
5. Click on Format button (First button below Text Area).
6. There is output will be displayed on second (destination) TextArea.
7. Now you can Copy Text of this TextArea and paste on you SSMS querywindow and run.

 Thanks dpriver.com for providing us this tool.

Updated: 25/10/2013
http://poorsql.com/ is also a good formater of sql script and we can use it in both ways either online or through adding Add-in to Sql server (SSMS ) and it works with only Ctrl+K,Ctrl+F. we found that is a fast way to format sql scripts.

www.ssmstoolspack.com/‎‎  , SSMS tools pack provide us a large number of functionality including formatting, running
script on different database at once, to store script change history so we can retrieve previous version of our scripts. It also include Insert script generator tool,find data in database/Table/Views and so on.
But after doing all that it makes us to compromise with speed of SSMS while i was trying it my sqlserver hanged 3 times so preferred to go with "Poorsql" instead of "SSMS tool pack".



Happy Living...
Happy Concepts...
Happy Programming...


Sunday, January 20, 2013

Search records in database using dynamic number of parameters

Search records in database using dynamic number of parameters

Scenario :
In a search form if there are multiple fields available as search criteria and user can search on the basics of a single field or without fields or a set of criteria fields.And we are calling same SP by sending entire criteria without taking consideration what criteria user has selected. In this case SP is responsible to use optimize technique to provide result dataset. It becomes extremely important if searching is done on set of tables or large set of data.

Approaches:
So to resolve above issue we have 2 reproaches described below:
1. Create dynamic query based on criteria sent by user
2. Create SP to smart enough to handle Null or empty fields.

// under construction


Happy Living...
Happy Concepts...
Happy Programming...

Friday, November 9, 2012

Use Variable with Top in Select In Sql


 Use Variable with Top in Select In Sql:

This is something i knew but forget to logged in my blog. But when my some one asked me for same I googled and found answer this time i am going to log it for myself and who ever comes to check my blog.

declare @i int =10

select  top (@i) * from dbo.tblviewsource



It will return top 10 records from table dbo.tblviewsource.

Happy Living....
Happy Concepts....
Happy Programming....

Tuesday, November 6, 2012

Speed Up Sql Query


Speed Up Sql Query

1. If there is a large Table going to participate in Query, it is better to filter out records based on some filters and reduce the no of records going to participate in Query.

2. If in a query ,joins include few inner joins and left joins than it is better approach to first apply all inner joins and then on the result of inner join performed query, apply left join.

3. Temp table if something that is very large query  and we want to take some portion of query out side and wants to use table variable or temp table then tere are 2 cases:

   for small data set:
                            if the result of queries are smaller than we should use table variable it doesn't has overhead of dropping it in the end of query.

   for large data-set:
                            In the case of large result set it is better to use temp table because there isn't over head of writing it to table(by default in the case of temp table simply result sets are referenced by some pointer all temp table). One more advantage we have here is of Indexes we can apply indexes on temp table and it get dropped when this temp table dropped out.

4. Fields of table should not be used in function, in where clause or select clause If we do this it will be slower, so good will be not to use them in function. We can use constant variable or parameter in function it wont affect query very much.

5. Make sure we have proper indexes on table going to participate specially on large table.

6. Drop temp tables just after the if not usable any longer. In the case of single time use i prefer to use CTE tables.


Happy Living....
Happy Concepts....
Happy Programming....





Sunday, October 28, 2012

How to remove Special Character in database output

A few months back i was caught in situation where we had special characters in data base like shift+enter, tab , space , enter etc.

That time we spend 3 hours to detect issue and next 3 hours to resolve it. But a few days back one of my colleague get caught in same situation we helped her to detected the issue and suggested the same solution we put at that time, but somehow she managed to get more easy approach to resolve the issue.

so if a string has special characters in it, then it is better approach to change value to string and than perform a trim on the charcter we want to remove.

for eg:

string s= dr.GetString("column1");

assuming s contains a tab and enter character in it.

s=s.ToString().Trim("\t"); // it will remove tab character form string
s=s.ToString().Trim("\n"); // it will remove enter character from string

so it is better to use Trim instead of performing looping on character and using ASCII to remove character.

Note: But remember, you need to use ToString method before using Trim method. If we wont do that there wont be any affect on string.

Happy Living....
Happy Concepts....
Happy Programming....