Friday, December 26, 2008

Remove Single Line Comments from SQL Server Procedure

If you follow this blog regularly, you would have noticed that I have written logic as well as code to remove multi-line comments from SQL Server Procedure. You also can follow the link to get to that. Here, my concern is to remove single line comment from SQL Server procedure. It's a much simple compared to removing multi-line comment.

 

Single line comment in SQL Server is preceded by double dash like "—". So, you just consider following two points.

1.      You need to remove the lines beginning with double dash.

2.      If a line contains double dash at the middle, then remove part of line after the double dash.

 

An example will clarify more on this. First, I created a test procedure with name sp_helptext.

 

create proc sp_proc1  

as   

begin   

 create table #tt  

 (  

  col1 varchar(200)  

 )  

 

-- insert into ##tt values ('test data 1')

insert into #tt values ('test data 2')  -- test comments

-- insert into ##tt values ('test data 3')

  -- test comments

 

select *

from #tt 

end   

 

Then, I created a temporary table with name #tbl_sp_text to populate the help text of procedure on that. Finally, I applied above mentioned logic to retrieve the lines without any single line comments. Here is the complete code.

 

create table #tbl_sp_text ( id int identity(1,1), sp_text varchar(8000))

insert into #tbl_sp_text

exec sp_helptext  sp_proc1 /* procedure name */

 

 

select      case when charindex('--',sp_text) > 0 then substring(sp_text,0,charindex('--',sp_text))

      else sp_text end sp_text

from  #tbl_sp_text

 

Read more...

Wednesday, December 17, 2008

How to calculate rank using SQL trick?

Suppose you have data as shown below in a table. It contains marks obtained by students in different subjects. Here, student_id and subject_id makes the primary key of the table.

 

student_id

subject_id

obtd_marks

rank

5

2

99.1

0

7

2

98.1

0

8

2

98.1

0

1

2

92.5

0

2

2

92.1

0

3

2

91.1

0

4

2

85.1

0

6

2

56.1

0

8

9

98.1

0

2

9

91.1

0

 

At first, rank is 0 for all. Now, you have to write a SQL query to rank the students on the basis of their obtained marks. Here is the query.

 

update      tbl_marks

set   rank  = (

      select      count('1')

      from  tbl_marks b

      where a.subject_id = b.subject_id

      and   b.obtd_marks > a.obtd_marks

      ) + 1

from  tbl_marks a

 

where tbl_marks is the name of table.

 

This query considers the case of multiple students having same marks giving them same rank. It also considers ranking of different subjects separately. The output thus becomes as shown below. Note that, student 7 and 8 have same rank in subject 2.

 

student_id

subject_id

obtd_marks

rank

5

2

99.1

1

7

2

98.1

2

8

2

98.1

2

1

2

92.5

4

2

2

92.1

5

3

2

91.1

6

4

2

85.1

7

6

2

56.1

8

8

9

98.1

1

2

9

91.1

2

 

Read more...

Tuesday, December 16, 2008

Easy way to search for database objects

 If you are a database administrator, you come across thousands of database objects like stored procedures, tables, views, functions, triggers, etc. So, it's not possible to remember name of each of these objects. So, how will you find out a particular object?

 

In SQL Server 2000, there is good search facility for searching database objects. It can be launched by pressing F4 in SQL Query Analyzer or from Tools -> Object Search -> New.

The screen shot of Object Search window is shown below.

 

 

You can type name of object or part of name of object included between % signs in Object name filed and check the type of objects like User table, Stored procedures and finally click Find Now to get the search results. The interesting thing in search result window is that you can right click in particular object and select option to delete or script object.

 

You can search for stored procedures and functions by using keyword sp_stored_procedures like given below. It works for current selected database only.

 

sp_stored_procedures '%rep_proc%'

 

This command when executed in query browser will list out stored procedures and functions with name containing word rep_proc. If sp_stored_procedures is executed alone without any arguments, then it will list out all stored procedures and functions of current database.

 

Also, you can search for tables and views by using keyword sp_tables like shown below. It also works for current selected database only.

 

sp_tables '%tmp%'

 

This command when executed in query browser lists out tables and views with name containing word tmp. If sp_tables is executed alone without any arguments, then it will list out all tables and views of current database.

 

Read more...

Monday, December 15, 2008

Small trick to speed up your select query

If you want to select data from a database table, but somebody else is inserting data in same table, then in general case, you will have to wait till the insert completes. Your query is deadlocked.

 

In many cases, the dead lock is justified. It is because, the select data will be more correct after the insertion completes. But it may not be the case always. If you want to select sales data from 2005 to 2007, but someone is inserting data of date 2008, then you will have nothing to do with what other is inserting. In such case, you can use nolock keyword of SQL Server, to perform select operation. It simply allows performing raw read in the table. It ignores locks and also does not lock the table. So, the select operation becomes faster.

 

It is used like this.

 

Select *

From    CSAVatInvoiceHdr a (nolock)

Where  csavatinvoicedt >= '2005-01-01'

And      csavatinvoicedt <= '2007-01-01'

Read more...