Pages

Monday, June 30, 2014

SQL Security- How to hide your SQL Server Instance from network–TIP 22

 

To make your SQL server secure  one of the option is to hide the instance from network.

To achieve this you need to do following things

1) Go to Configuration tool folder and select configuration manager of SQL server as shown in below fig

SQL_server_Configuration_manager

2) Once Configuration manager is open select SQL Server Network configuration option. In this you will find the option protocols for MS SQL Server  as shown in fig

sql_Server_Configuration

3) Now select property of protocol of MSSQLSERVER. You will get below screen now in this screen you are seeing there is a property called hide instance by default it is false. make it true to hide instance from network.

hideinstance

I hope this article will help you to make your sql server instance hide from book,

 

Thanks

Rajat Jaiswal

Wednesday, June 25, 2014

How to open SQL Server Management studio from command prompt - TIP#21


It is just a simple tip nothing fancy.

On command prompt you just type SSMS and hit OK.






Your SQL server Management studio will be open.

You can get other option also by typing ssms/? .



You will get below options



Now if you want to login to particular SQL Server Instance you can choose option as mention in above screen.


For example to open SQL Express version on my machine, I wrote query as shown in below image


Enjoy!!!


Monday, June 23, 2014

Looping through GO statement - TIP # 19


Although, I did not get chance to use this facility in my projects but it is good to know feature.

Suppose you want looping and want to insert data in table then in such situation you can use this GO lopping feature.

In below example We are inserting data into debug table with GO statement





Now  when you see the debug table you will find 1000 records are there.


 


 Enjoy !!!

Wednesday, June 18, 2014

Performance tips - Check no of logical & Physical reads TIP #18

Sometimes, we need to understand what exactly going on with particular statement, stored procedure why it is slow?

To check performance of statement what exactly going on we also interested how many logical reads, physical reads a particular statement taking.

To achieve this we need to write SET STATISTICS IO ON

For example
GO
SET STATISTICS IO ON 
SELECT * FROM [dbo].[RequestMaster]
SET STATISTICS IO OFF
GO



If your statement having large number of physical reads & logical reads then you need to think for optimization.

Enjoy

Monday, June 16, 2014

SET NOCOUNT ON – count in performance tip #17


Sometimes some small things which we ignore can impact more.

By default when you run any statement you will see in the message like no of records affected.
As shown in below figure.



Although, most of the time we do not require such information but still it is overhead.

So to stop such overhead which impact performance just use  SET NOCOUNT ON in your stored procedures.

For example
GO
SET NOCOUNT ON ;
 SELECT * FROM RequestMaster

GO

When you use SET NOCOUNT ON you will get a simple message
“ Command(s) completed successfully.”



Enjoy !!!

Thursday, June 12, 2014

Determine current date with different option TIP #16

We can determine date with different option in SQL server

Option 1:- with GETDATE() we can get current date & time
Go
  SELECT GETDATE()
GO

Option 2:- With sysDateTime()  , we can get current date & time (with nano seconds)
GO
   SELECT SYSDATETIME()
GO


Option 3:- with SYSDATETIMEOFFSET() , We can get current date & time with timezone offset
Go
  SELECT SYSDATETIMEOFFSET()
Go



Option 4:- with SYSUTCDATETIME() , We can get current date & time with UTC (Coordinated Universal Time)

GO
  SELECT SYSUTCDATETIME()
GO



Option 5:-
with GETUTCDATE() , We can get current date & Time with UTC
GO
  SELECT GETUTCDATE()
GO


 
Option 6:- with CURRENT_TIMESTAMP , we can get current date & Time
GO
  SELECT CURRENT_TIMESTAMP
GO



Enjoy !!!

Monday, June 9, 2014

How to determine when Last backup taken of a database n SQL Server? TIP #15


Sometime we require to know when we have taken last backup.
So let me share here all the backup, log shipping related detail you can find in the MSDB database which is system database.
SQL server maintain all the backup related detail in the MSDB database table
As Show in the figure

Now we can run the following command to determine the database backups
GO
SELECT Name,
       database_creation_date,
       backup_start_date ,
       backup_finish_date,
       database_name,
      bmf.physical_device_name
FROM MSDB.dbo.backupset bs
INNER JOIN msdb.dbo.backupmediafamily bmf  ON bmf.media_set_id = bs.media_set_id
WHERE database_name='DBayDB'

GO
WHERE DBAY is database name when you run this command you will get all the backup taken for this database.


Enjoy !!!

Thursday, June 5, 2014

How to update statistics ? TIP #14

b

In last TIP tip#13, We learn how to find last updated statistics status.

Now we know when it last updated so it may be require we need to update statistics for some of table.

So to update statistics we need to write following command ( if we want to update  statistics of entire tables objects)

Go

EXEC sp_updatestats;

GO

Update_Statistics_All

Now if we want to  update statistics of particular table then we need to write following command

GO

UPDATE STATISTICS tblProductStock

GO

Now if we want to update statistics of particular index of a table then we need to write following command

GO

UPDATE STATISTICS tblCustomer PK_tblCustomer;

GO

WHERE tblcustomer is table name and pk_tblCustomer is primary key

 

So enjoy !!!

GO

How to determine Last statistics update? TIP #13

To determine last statistics update we need to run following command.

Go
SELECT o.name, i.name AS [Index Name], 
       STATS_DATE(i.[object_id], i.index_id) AS [Statistics Date],
       s.auto_created, s.no_recompute, s.user_created
FROM sys.objects AS o WITH (NOLOCK)
INNER JOIN sys.indexes AS i WITH (NOLOCK)
ON o.[object_id] = i.[object_id]
INNER JOIN sys.stats AS s WITH (NOLOCK)
ON i.[object_id] = s.[object_id]
AND i.index_id = s.stats_id
WHERE o.[type] = 'U'
ORDER BY STATS_DATE(i.[object_id], i.index_id) ASC;   
GO







OR

We can use following statement

Go
DBCC SHOW_STATISTICS ('users',PK_User);
GO




Enjoy !!!





Monday, June 2, 2014

How to find what is currently running on your SQL SERVER? TIP # 12


When we are keen to know that what is currently running on SQL SERVER for analysis purpose then with the help of below SQL statement we can easily find those statements, Stored procedure, status etc.

Go
SELECT [Spid] = session_Id
, ecid
, [Database] = DB_NAME(sp.dbid)
, [User] = nt_username
, [Status] = er.status
, [Wait] = wait_type
, [Individual Query] = SUBSTRING (qt.text,
er.statement_start_offset/2,
(CASE WHEN er.statement_end_offset = -1
THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
ELSE er.statement_end_offset END -
er.statement_start_offset)/2)
,[Parent Query] = qt.text
, Program = program_name
, Hostname
, nt_domain
, start_time
FROM sys.dm_exec_requests er
INNER JOIN sys.sysprocesses sp ON er.session_id = sp.spid
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle)as qt
WHERE session_Id > 50 — Ignore system spids.
AND session_Id NOT IN (@@SPID) — Ignore this current statement.
ORDER BY 1, 2
Go

Enjoy !!!


Sunday, June 1, 2014

How to set database as multi user TIP #11

 

As we mention in  TIP 10  to set database user as Single user now to make this available again for all the user or multi user we need to do following

ALTER DATABASE FriendsDB
SET MULTI_USER 
WITH  NO_WAIT;

GO

Here when we run above command  FriendsDB again changed to MULTI USER.

Here NO_WAIT means not wait for statement with immediate response.

When we run this command database again change to multi user as shown in below fig

Multi_User_Db

 

Enjoy !!!

How to set database as Single User TIP #10

Sometimes it is require to  set database as single user for maintenance or other recovery purpose.

We need to write following statement

GO

ALTER DATABASE FriendsDB
SET SINGLE_USER 
WITH  ROLLBACK IMMEDIATE;

GO

Here ROLLBACK IMMEDIATE  means if any transaction going on by other user then Rollback that data.

See below snap for more detail

Single_User_DB

SEt_Single_User_Result

Enjoy !!!