Wednesday, December 31, 2008

Default user of login mapped to role SQL Server Administrator

The post I m writing is slightly confusing. If a SQL server login is assigned to System Administrator Server role of SQL Server 2000, then it will have rights to perform any activity in SQL Server Installation. That is what, SQL Server says. This may be problematic some times.

 

Now, Lets say there is a login stcsa with mapped to role System Administrator. First, you login to ISQLW with login stcsa and create a table with name tbl_test as below.

 

create table tbl_test (t varchar(10))

 

Now, check the owner of tbl_test, it will be dbo not stcsa. Suprising Not? This is because stcsa is System Adminitrator. But, you can call the table as simply tbl_test without "dbo." in front of the object name. The following select query works.

 

select    * from            tbl_test

 

Now, lets say, you again created a table with same name but like below.

 

create table stcsa.tbl_test ( t varchar(200))

 

Then, there will be two tables with name tbl_test and owners as dbo and stcsa. Now, if you use simply tbl_test, which one object do you think will it call? No not, it will call dbo.tbl_test not stcsa.tbl_test despite you are logged in as stcsa. This is where problem comes. In normal case, if you are logged in with a sql login and that login is mapped with user of same name, then if you call sql objects without prefix username, then it will search for username.objectname.

 

I am not sure that whether this can be called a bug in SQL Server or not. I just want to specify that, it's always useful to call sql objects by using username.objectname. It becomes more useful, when the login used is mapped to the role like SQL Server Administrator.

 

Read more...

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...