Sunday, 24 August 2014

SQL Server 2012 in-built functions

Hi,

In this blog, I have discussed Sql Server in-built functions to be used in T-sql, queries, DML's. I have detailed them and have provided example of each function. Any suggestion,correction would be highly appriciated.



--Math Functions
--Average function
Select avg(Person_id) avg from db_owner.person

--CharIndex function to find index of a particular alphabet. In example
--CHARTINDEX finds 'a' in first_name column of person starting from 1st character of
--string
Select CHARINDEX('a',First_Name,1)a,first_name from db_owner.Person

Use MS2012;
go

declare @avar xml
RETURN @avar.query('Product/Prices')


truncate table db_owner.person


delete * from db_owner.person where middle_name is null

create table t1(id int identity(1,1) default 1)

select * from db_owner.person

select sum(person_id),count(*) ,first_name from db_owner.person


select top(10)
desc sp_settrigger

declare @var1 nvarchar(20);
select @var1 ='My Example';
Select CharIndex ('m',@var1,1) Col1,
CharIndex('P',@var1,1) COl2,
CharIndex ('x',@var1,1) Col3
GO

---Concat Function

Select ConCat ('Mr.',' Jaidev ',' sharma')

Select Concat(First_name,'--',Last_name) from db_owner.Person


---Left function shows number of character from left side of string
-- in example first four character from left would be displayed
Select Left('jaidev sharma',4)

--Len function to get the total length of a string
Select len('jaidev')
Select len(first_name) leth,First_Name from db_owner.person
---Lowever and upper
Select lower('Jaidev'),upper('jaidev')
Select lower(first_name),upper(Last_Name) from db_owner.person
---Ltrim,Rtrim,trim removes blank spaces

select Ltrim('   jaidev')y,Rtrim('Jaidev ')x

Declare @var1 nvarchar(50);
set @var1 = '   My Name is Jaidev Sharma';
Select Ltrim(@var1)

--Substring to get a portion of a string starting from 2nd parameter and upto
--3rd parameter
Select substring('Jaidev',3,3)

Select First_Name,SubString(First_name,1,len(first_name)) nm from db_owner.person

--Find a particular characher left and right of a string
Select PATINDEX(lower('%S%'),'Australia')

Select First_name from db_owner.person

Select PatIndex('%o%',First_Name),First_Name from db_owner.Person

----Replace find a particular character give in 2nd parameter and replaces it
---a parameter give in 3 parameter. In example in 'jai' string 'a' is replaced with 'u'
---

Select Replace('jai','a','u')

Select First_name,Replace(First_name,'a','h') from db_owner.person

Select count(person_id) emp_count from db_owner.person

---++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
---TSQL Date and Time Data Types and Functions

   Select @@DATEFIRST
  
   --to get current_Timestamp
  Select CURRENT_TIMESTAMP

--to add date in one particular date. In example 1, I have added DAY,41 (no. of days) in to 16-Jun-2014 to see counting 41 from 16-jun what date
--would fall.
---eg 1
Select DATEADD(day,41,'2014-06-16')
--eg 2 in this I have added Month 1 (one month) in todays date
Select DateAdd(Month,1,SysDateTime())
--eg3 In this I have added Year 1 ( one year) in the today's date.

Select DateAdd(Year,1,SysDateTime())

---DateDiff function tells us the difference between to dates.

Select DateDiff(day,'2014-06-16','2014-07-28') --42 days

Select DateDiff(Month,'2014-06-16','2014-07-28') --1 month

Select DateDiff(Year,'2014-06-16','2014-07-28')

---DATEFROMPARTS

Select DateFromparts(1,2,3)

----DATENAME return a specific information (eg. Year,Month,Day, Day of Year or week) of a date. To get just year portion of date use year,

Select DateName(Year,CURRENT_TIMESTAMP) year
Select DateName(Month,CURRENT_TIMESTAMP) Month
Select DateName(day,Current_TimeStamp) Day
Select DateName(DayOfYear,Current_TimeStamp) Day_Of_Year
Select DateName(Week,Current_TimeStamp) WeekNo

----DATEPART  Similar to DateName
Select DatePart(Month,Current_TimeStamp) Month
Select DatePart(Year,Current_Timestamp) Year
Select DatePart(Day,Current_TimeStamp) Day

--DATETIMEFROMPARTS converts integers in Data Format
Select DateTimeFromParts(2014,3,22,13,22,13,14) Dat1
--DATETIME2FROMPARTS
---Select DATETIME2FROMPARTS(2014,3,12,12,03,20,12,1)
SELECT DATETIME2FROMPARTS(2014, 2, 22, 8, 53, 39, 7, 1) as Date_1
--DAY to know current day of the month
 Select day(current_timestamp)
 --EOMONTH end of month
Select EoMonth(CURRENT_TIMESTAMP)
--  GETDATE()
  Select GetDate()
--GETUTCDATE()
  Select GETUTCDATE()
---ISDATE checks the format of date. If format is right than it return 1 otherwise 0
  Select IsDate('14-03-02')-- Returns 0 as date format is wrong
  Select SYSDATETIME()
  Select IsDate(CURRENT_TIMESTAMP) --Returns 1 as date format provided by Current_TImeStamp is right
---- MONTH
Select MONTH(current_timestamp)
Select year(CURRENT_TIMESTAMP)

--SmallDateTimeFromParts
Select SMALLDATETIMEFROMPARTS(2014,07,29,1,04)

    --SWITCHOFFSET
---    SYSDATETIME returnes system time stamp
Select SysDateTime()

 --   SYSDATETIMEOFFSET
 --   SYSUTCDATETIME
--    TIMEFROMPARTS
 --   TODATETIMEOFFSET
  
----TSQL System Functions

---
  select  @@CONNECTIONS,getdate();

--@@Error to knowif error any came

  Use Ms2012;
  Go
  Select * from db_owner.gender;
  Update db_owner.Gender set Gender='UnKnown' where Gender_id=3;
  delete from db_owner.gender where Gender_id=4;
  if @@ERROR <> 0 Print N'There is no Gender Id 4'
  Select * from db_owner.gender;
  GO


-- @@IDENTITY to know last value IDENTITY value inserted
-- @@ROWCOUNT to see number of rows effected by a T-Sql Statement

use MS2012;
go
Update db_owner.gender set Gender='Male' where Gender_id=1;
Update Db_owner.Gender set gender='Female' Where Gender_id=2;
Update Db_owner.Gender set gender='Unkown' Where Gender_id=3;
if @@ROWCOUNT<> 0
  print Concat('The number of rows affected are ' , @@Rowcount);
if @@ROWCOUNT=0
    Print Concat(@@ROWCOUNT, ' no row effected' );
Select * from db_owner.gender;
go

-- COALESCE equal to NVL function of Oracle
Alter Table Db_owner.person
alter column middle_name nvarchar(20) null

Select * from db_owner.person
Update db_owner.Person set Middle_name=null where Person_id=103



Select First_name,Coalesce(Middle_name,null,'UnKown') from db_owner.Person

--ERROR_LINE returns the error at particular line when using with Try and Catch block
USE model;
GO
BEGIN TRY
   SELECT 8/0;
END TRY
BEGIN CATCH
   SELECT ERROR_LINE() AS ErrorLine;
   Select ERROR_MESSAGE () As ErrorMessage
   Select ERROR_NUMBER () As ErrorNumber
   Select ERROR_PROCEDURE() as Proce
   Select ERROR_SEVERITY () as Sever
   Select ERROR_STATE() as Stat
END CATCH;
GO
/*  ERROR_MESSAGE
    ERROR_NUMBER
    ERROR_PROCEDURE
    ERROR_SEVERITY
    ERROR_STATE*/

---Host_Id
Select HOST_ID() as HostId
---HOST_NAME
Select Host_name() HostName

---IsNull
Select ISNULL(NUll,'Jai')
Select * from db_owner.person
select IsNull(Middle_name,'Kumar (replaced)'),Middle_name from db_owner.person
---ISNUMERIC either 1 or 0 if 1 than parameter is number
Select ISNUMERIC('a')
  
--- NULLIF checks if two expressions are same or not. If same than returns
--null otherwise value of first parameter

Select NullIf('a','a') col1,NullIf('a','b'),NullIf('jai','jai')

----Security Functions
---   CURRENT_USER if connect by windows authentication or sa than its show DBO.
 Select Current_user as currentUser
 Select ORIGINAL_LOGIN() as OrgLogin

--Session user shows the user of sesison. normally it s same as current user function.
Select SESSION_USER

--Shows the user at OS level connected to sql server
Select SYSTEM_USER

--User_Name tell the name of user at sql server level   
Select USER_NAME()

TSQL Metadata Functions

--App_Name() gives the name of the applicaiton.In following example it gives  'Microsoft Sql Server Management Stuido - Query' output
Select   APP_NAME ()

--DB_ID returns the number assigned to a database
Select DB_ID() 
--DB_Name name of database
Select  DB_NAME() DBNAMe

---Object_Definition gets the definition of an object
Select * from sys.objects
where name='PERSON'

select * from ms2012.db_owner.gender

Select OBJECT_DEFINITION(object_ID('ms2012.db_owner.gender'))

--Object_id to know the object id

Select Object_id('ms2012.db_owner.person')

--Ojbect Name to know the object name

Select OBJECT_NAME(437576597),Object_id('ms2012.db_owner.person')

Select OBJECT_SCHEMA_NAME(437576597,8)

Select SCHEMA_ID('db_owner')

Select SCHEMA_NAME(16384)


---TSQL Configuration Functions

    Select @@LOCK_TIMEOUT as TimeOutOfLog

    Select @@MAX_CONNECTIONS as MaxConn

    Select @@SERVERNAME as SrvrNm

    Select @@SERVICENAME as SrvcNm

    select @@SPID as SPID

Tuesday, 22 July 2014

Table Recovery



Table recovery in Oracle 10g

Many a times it happens that a developer or sometimes you as a DBA, accidently drops a table and want to correct things by restoring it back.
Oracle9i Database introduced the concept of a Flashback Query option to retrieve data from a point in time in the past, but it can’t flash back DDL operations such as dropping a table. The only recourse is to use table space point-in-time recovery in a different database and then recreate the table in the current database using export/import or some other method. This procedure demands significant DBA effort as well as precious time, not to mention the use of a different database for cloning

However Oracle 10g came with excellent feature table recovery is as simple as that running few very simple commands. 
We discuss this feature in this blog:

SQL> Select banner from v$version ;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 – Production

SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
T1                                       TABLE
TEST_NULL                      TABLE


SQL> Drop Table T1;
Table dropped.

SQL> select * from tab;
BIN$/sNVxevp5A/gQKjAEQAYwA==$0 TABLE

Notice! We used “Drop Table T1” statement and expected that table is dropped and spaced is freed. But this is not so. Instead a new table has come with some bizarre name. This is the name that Oracle gave to our table T1.
What happened is Oracle tossed a new terminology called RECYCLEBIN just like the recycle bin that we have in our Windows machines. Instead of dropping table and freeing space, Oracle kept table in Recyclebin for just in case DBA wants to flashback it.
Let’s see what else RECYCLEBIN has stored there.

SQL> show recyclebin
ORIGINAL NAME    RECYCLEBIN NAME                                       OBJECT TYPE                  DROP TIME
---------------- -------------------------------------------- ------------ -----------------------------------------
CURRENCY_TEST    BIN$/mGWpLuT4DLgQKjAEQAWBg==$0 TABLE                   2014-07-17:16:46:32
CURRENCY_TEST    BIN$/mGWpLuS4DLgQKjAEQAWBg==$0 TABLE                   2014-07-17:16:33:19
T1               BIN$/sNVxevp5A/gQKjAEQAYwA==$0                    TABLE                    2014-07-22:12:24:08


See! Except others I have my T1 table stored in there. Now lets see the magic.

SQL> Flashback table t1 to before drop;
Flashback complete.

SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------
T1                                     TABLE

See. We got over table back at place. J

Now one important question arises how I can drop my table completely if at all we want to drop it. In this case use PURGE option of drop table.

SQL> Drop Table T1 Purge;
Table dropped.

SQL> show recyclebin
ORIGINAL NAME    RECYCLEBIN NAME                                  OBJECT TYPE  DROP TIME
---------------- ------------------------------ -------------------------------------------------- -------------------
CURRENCY_TEST    BIN$/mGWpLuT4DLgQKjAEQAWBg==$0 TABLE        2014-07-17:16:46:32
CURRENCY_TEST    BIN$/mGWpLuS4DLgQKjAEQAWBg==$0 TABLE        2014-07-17:16:33:19

This time, We did not get table listed in RECYCLEBIN

This particular operation  related to PURGE can be done in another way. Once we drop the Table without PURGE clause we can use PURGE Clause as follows to remove table from RECYCLEBIN.

SQL> Purge table t1;
Table purged.

Sometimes it happens that you dropped lots of tables from one particular tablespace. So instead of doing PURGE one by one, you can use following command simply to remove all entries in one go.
In this example, I had all of my tables in USERTBS tablespace. So I used following command to remove all tables from RECYCLEBIN.

SQL> Purge Tablespace USERTBS;
Tablespace purged.

Similarly, If you had dropped tables related to one particular user than you can use variant of this particular command.

SQL> Purge Tablespace UserTBS user test;
Tablespace purged.
                               
                                                                OR
Connect to TEST user and run following command.

SQL> Purge Recyclebin;
Recyclebin purged.

You as DBA can use following command to remove list of tables related to every tablespace.
SQL> Purge DBA_RECYCLEBIN;


This is not all. Using Flashback Table <> to Drop we can flashback table to particular time or a scn.

FLASHBACK TABLE T1 TO SCN 285954520;

Renaming is also possible.

FLASHBACK TABLE TEST TO BEFORE DROP RENAME TO TEST2;
FLASHBACK TABLE TEST TO BEFORE DROP RENAME TO TEST1;


This feature has definitely saved us from lots of trouble in situation like this.

Any comment, suggestion would be appreciated.

Sunday, 20 July 2014

Microsoft Sql Server tables and constraints Part 2


This is my third blog about Microsoft Sql Server database administration. In last blog we talked about keys in Sql Server and found that there is no difference between syntax's of defining these keys in Oracle and Sql Server.
In this blog we will discuss auto generation of key values. Just like Synonym of Oracle database. I have demonstrated this by doing T-Sql script. and you can also achive this using Studio as well.

Comments added in between each step would help you to understand concept easily.


First I have created IDNTY_TAB2 table in SAMPLE Database using SAMPLE schema and defined ID as Primary key and set it to a IDENTITY column

USE [Sample]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [sample].[Idnty_Tab2](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nm] [nchar](50) NOT NULL
) ON [PRIMARY]

GO

Now! I have inserted following rows in the table. Notice that I have not provided value of ID primary Key column. As this column is IDENTITY column. So its generating values its own starting from 1.

Insert into idnty_tab2 values ('James')
Insert into idnty_tab2 values ('Davis')
Insert into idnty_tab2 values ('Smith')
Insert into idnty_tab2 values ('Samantha')
Insert into idnty_tab2 values ('Nancy')
Insert into idnty_tab2 values ('Deran')

Select and confirm that you get to see all the rows.
Select * from idnty_tab2


Now! Delete the row and query again to see the result
Delete from Idnty_tab2 where id=3

Select * from idnty_tab2

Now try to insert new row and query the table and see the value of ID column
Insert into idnty_tab2 values ('Smith')

Select * from idnty_tab2
You will notice that I has not used missing 3 as the value of ID. So to use deleted ID value 3 we need to set
IDENTITY_INSERT on table..Following statement would set this parameter ON.

Set Identity_Insert Idnty_Tab2 On;

After setting this Parameter we need to provide columns name as well. without column name INSERT statement would fail.

Insert into idnty_tab2 values ('Harris'); --Would Fail with following message.
--Msg 545, Level 16, State 1, Line 2
--Explicit value must be specified for identity column in table 'Idnty_Tab2' either when
--IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.

Following statement would be successfull.
Insert into idnty_tab2 (id,nm) values (3,'Harris')

Confirm it once again
Select * from idnty_tab2 order by 1


Once you have inserted the missing values you should take this parameter OFF using follwoing parameter.
Set Identity_Insert Idnty_tab2 off;

Now start inserting the values again.
Insert into idnty_tab2 values ('James')

Select * from idnty_tab2 order by 1

Suppose we delete all the rows and again insert it.After insert when we query table and get to know that
instead from 1 ID is started from the higest value we had before we deleted all the rows. Sql Server instead of setting to 1 have started it from that value.

Delete from Idnty_Tab2

Insert into idnty_tab2 values ('James')

Select * from idnty_tab2 order by 1

Now to restart the feed from start we need to reset the seed by using following procedure

dbcc checkident('idnty_tab2',reseed,0)
Checking identity information: current identity value '9', current column value '0'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

After using above procedure we insert values again and get to see that this time Sql Server has used 1 as starting value

Insert into idnty_tab2 values ('James')
Insert into idnty_tab2 values ('Davis')
Insert into idnty_tab2 values ('Smith')
Insert into idnty_tab2 values ('Samantha')
Insert into idnty_tab2 values ('Nancy')
Insert into idnty_tab2 values ('Deran')


Select * from idnty_tab2 order by 1

This is the behavior when we use DELETE statement, However if we use TRUNCATE statement than the counter is  automatically set to its starting value 1; Lets use Truncate statement and see what happens

Truncate table Idnty_Tab2;

Select * from idnty_tab2 order by 1

Insert into idnty_tab2 values ('James')
Insert into idnty_tab2 values ('Davis')
Insert into idnty_tab2 values ('Smith')
Insert into idnty_tab2 values ('Samantha')
Insert into idnty_tab2 values ('Nancy')
Insert into idnty_tab2 values ('Deran')

Select * from idnty_tab2 order by 1

Using TRUNCATE command we have no need to reset the counter. It happens with DELETE Statement only


I hope this would clear any doubt.
If have any doubt please write back to me.Any suggestion/comments would be appriciated.

Thanks

Friday, 11 July 2014

Microsoft Sql Server table and constraints Part 1

This is first part of a series of blog discussing tables,constraints in Microsoft Sql Server database. I have
created on database to store person information. For this I have used MSS 2008R2 version.


Drop table sample.gender

Create table sample.gender
(
id int primary key,
gender nvarchar(10) not null
)

Insert into Sample.gender values (1,'Male')
Insert into Sample.gender values (2,'Female')
Insert into sample.gender values (3,'Not Sure')

select * from sample.Gender

Drop table sample.person

Create table sample.person
(
id        int                primary key,
Name    nvarchar(30)    Not null,
LastName    nvarchar(30)    Not null,
Email    nvarchar(15)        not null,
Nationality nvarchar(20)    not null,
gender       int              not null
)

Alter table sample.person add constraint fk_con foreign key (gender) references sample.gender (id)

truncate table sample.person

Insert into sample.person values (1,'Jai','Sharma','vat@gmail.com','Indian',1)
Insert into sample.person values (2,'Ram','Jain','RJ@gmail.com','Srilankan',1)
Insert into sample.person values (3,'Hariharn','Dev','hd@gmail.com','Singapore',1)
Insert into sample.person values (4,'Anmol','Sethi','AS@gmail.com','Indian',1)
Insert into sample.person values (5,'Rajan','Chawala','RC@gmail.com','Srilankan',1)
Insert into sample.person values (6,'Anamika','Mukharji','AM@gmail.com','Bangladesh',2)
Insert into sample.person values (7,'Rupali','Sen','RS@gmail.com','Aussie',2)
Insert into sample.person(id,name,lastname,email,nationality) values (8,'Radhika','Reddy','RR@gmail.com','Aussie')

select * from sample.person
--Adding a default constraint
Alter table Sample.Person
add constraint dflt_tblPerson_gender default 3 for gender

---Adding an column in table
Alter table sample.person
add  city nvarchar(20)
constraint dflt_person_city default 'Melbourne'

---Drop a column from table
Alter table sample.person
drop column city



----Constraint cascade


---Check constraint

Alter table sample.person
add constraint chk_person_aga check (age > 0 and age <100)

Any comment,suggestions or correction is welcome.

Some RnD in Microsoft Sql Server 2008R2

Many new Sql Server DBAs would need to do their RnD with the MSS database. In previous blog I have given few options available while creating database.

In this blog we would discuss more operations once the database is created.

1:-   Rename a database

To rename the database, we have GUI and command line. First we discuss the GUI method which is really simple.

Using Management Studio (GUI), Select the database you want to rename and in option select
"RENAME"

Another method is run query in query area.
Alter database <nm> modify name =  sample3

Third method is to use sp_renameDB procedure to achive same.

Execute sp_renameDB 'oldnm','newnm'

2:-   Drop a database
Before we proceed with dropping the database. Following point should be kept in mind otherwise
our DROP statement would fail.

Database should be running in sigle user mode. No other user should be connected to it otherwise
database would not be dropped.

Once database is running in Single user mode. We have following options to drop it.
1:- Using management studio, select the database you want to drop. Right click on it and select delete
option.

2:- On Query window write following command while connected to MASTER or any other database but the database
being dropped.

Drop database '<Name of the Database>';

Note:- If users are connected with database being dropped than run following command to bring it in single user
mode.
Alter Database <Name of the database> Set single_user with rollback immediate;

Drop database <Name of the database>;

We cannot system databases.


Any correction,suggestion are welcome.