Wednesday, 25 June 2014

CASE Statement in WHERE Clause




So many times we come across such situations where user can pass all the parameters defined in the Stored procedure or he can pass the parameter of his choice. For example, In search screens where user  can select all the parameters or any one of the given Parameters.

We have two choices to implement this logic at DB.

       1. Constructing dynamic queries depending on the parameters passed to stored procedure
       2. By writing the CASE statement in WHERE clause.

 In this article we will look at the second option.
 Use AdventureWorks2008R2
 go


 CREATE PROC SEARCHPERSON
 (
 @BUSINESSENTITYID INT=-1,
 @PERSONTYPE VARCHAR(2)='',
 @FIRSTNAME VARCHAR(50)='',
 @MIDDLENAME VARCHAR(50)=''
 )
 AS
 BEGIN

   SELECT * FROM PERSON.PERSON WHERE
   BUSINESSENTITYID= CASE(@BUSINESSENTITYID)
   WHEN -1 THEN BUSINESSENTITYID
   ELSE @BUSINESSENTITYID
   END
   AND PERSONTYPE=CASE(@PERSONTYPE)
   WHEN '' THEN PERSONTYPE
   ELSE @PERSONTYPE
   END
   AND FIRSTNAME=CASE(@FIRSTNAME)
   WHEN '' THEN FIRSTNAME
   ELSE @FIRSTNAME
   END
   AND MIDDLENAME=CASE(@MIDDLENAME)
   WHEN '' THEN MIDDLENAME
   ELSE @MIDDLENAME
   END
 END

 Now we will execute the stored procedure with different combinations of input parameters and see the         result set



 When I don’t pass any parameter it will return all the records from table
 


 When I pass the only person type parameter value

 When I pass combination of different parameters 





Saturday, 21 June 2014

Finding out Table in each Database of a Server


Below query is used to find out a particular table in each Database of a server.
It will loop through each database and checks for the particular table.

EXEC sp_Msforeachdb "use [?];select '[?]' as DatabaseName, * from sys.tables where name='TableName' "

Monday, 16 June 2014

Finding Out the Oldest Active Transaction



Following query is used to check the oldest active transactions on a DB. Open Transactions blocks tables and queries will not return any result.

Let’s look at example.

CREATE TABLE [dbo].[emp](
      [empid] [int] IDENTITY(1,1) NOT NULL,
      [empname] [varchar](10) NULL,
      [role] [varchar](10) NULL
) ON [PRIMARY]

Lets begin a transaction…

begin tran

insert into emp
values ('Pranay','MGR')

without firing commit or roll back fire the below query

dbcc opentran

This command will give us the oldest active transaction in DB.


Saturday, 14 June 2014

System level Stored Procedure





All system level stored procedures are stored in MSDB or MASTER Databases, and most begins with characters SP_  . Here is synopsis of the more common system stored procedures.

Sp_tables
List down all the tables and views
Sp_stored_procedures
Lists down all the stored procedures
Sp_server_Info
Provides the server level information such as
Version,character set…
Sp_databases
List down all availbale databases on server
Sp_start_job
To start sql agent job
Sp_stop_job
To stop sql agent job
Sp_monitor
Gives a quick snapshot of how server is doing.
How much RAM is in use,how busy processor is…
Sp_who
Used to check blocks,who is using a database…
Sp_rename
To change name of any object
Sp_renamedb
Change the name of DB
Sp_help
To find out information about any object in database.
Sp_helptext
This is used to display the actual text that was used to create an object in database.
Sp_addlogin
To add a standard login to the server
Sp_grantlogin
To grant access on sql server to a windows account
Sp_password
Used to change password for standard login