Monday, September 22, 2014

Script to get elapsed time from start of current day in SQL Server

How to get elapsed time in Seconds, Minutes, Hours from start of current day?


SELECT DATEDIFF(SECOND, 0, Cast(GETDATE() AS TIME)) -- In Seconds

SELECT DATEDIFF(MINUTE, 0, Cast(GETDATE() AS TIME)) -- In Minutes

SELECT DATEDIFF(HOUR, 0, Cast(GETDATE() AS TIME)) -- In Hours

Checking Email log in SQL Server

--SENT MESSAGES LOG
SELECT TOP 20 *
FROM [msdb].[dbo].[sysmail_sentitems]
ORDER BY [send_request_date] DESC

--FAILED MESSAGES LOG
SELECT TOP 20 *
FROM [msdb].[dbo].[sysmail_faileditems]
ORDER BY [send_request_date] DESC

--ALL MESSAGES – REGARDLESS OF STATUS
SELECT TOP 20 *
FROM [msdb].[dbo].[sysmail_allitems]
ORDER BY [send_request_date] DESC

Thursday, September 18, 2014

SQL Server Endpoint







Configuring endpoint security





A SQL Server endpoint is a door opened from or to SQL Server. Everything that can go from your SQL Server to the network, or from the network to SQL Server, goes through an endpoint. Endpoints can be system or user-defined. The system endpoints allow the usual T-SQL connections to SQL Server to send queries.
The endpoints are defined for a specific protocol, which were either HTTP or TCP in the
previous versions. In SQL Server 2012, the HTTP endpoints previously used for the native
web services feature are removed. Now you only have TCP endpoints.
You can create a user-defined endpoint for three purposes: TCP requests, service broker, or
database mirroring. Here we will talk about the first one, which is useful to set up a dedicated
and secured connection to SQL Server for administrative purposes or distant access.


How to do it...


To configure endpoint security, follow these steps:
1. In a SQL query window, type the following T-SQL command:
CREATE ENDPOINT myTSQLEndpoint
STATE = started
AS TCP (
LISTENER_PORT = 8080,
LISTENER_IP = (127.0.0.1)
)
FOR TSQL ();
2. You will receive a message saying that it will cause the revocation of any Public
connect permissions on the TSQL default TCP endpoint. This effectively means that
all logins will lose their permission to connect to SQL Server through the default
T-SQL endpoint. If you still want to allow connection permissions to the default TCP
endpoint, issue the following command:
GRANT CONNECT ON ENDPOINT::[TSQL Default TCP] to [public];
3. You can view the state of the endpoints with this command:
SELECT * FROM sys.tcp_endpoints;
Chapter 1
45
4. You can start or stop the endpoints with the ALTER ENDPOINT command.
For example, you can stop the default TCP endpoint as follows:
ALTER ENDPOINT [TSQL Default TCP]
STATE = STOPPED;
The state we just creat@ed will remain even after a service restart.




How it works...





When SQL Server is installed, a system endpoint is created for each network protocol used
in SQL Server. The permission to access these endpoints is given to the Public server role.
Every login declared in SQL Server is a member of this role, and permissions on the Public
server role can be changed, unlike other fixed server roles. You can grant, revoke, or deny
permissions to connect to an endpoint to all the logins through the Public role, or to specific
logins by revoking CONNECT permissions to the Public role, and by granting specific privileges
as follows:
REVOKE CONNECT ON ENDPOINT::[TSQL Default TCP] to [public];
GRANT CONNECT ON ENDPOINT::[TSQL Default TCP] to [a_specific_login];
If you want to allow connections to SQL Server from only a specific client IP address, you can
stop the default endpoint, or deny access to it, and create a user-defined T-SQL endpoint, with
a client IP address and a TCP port.
Stopping default endpoints has the same effect as disabling them in SQL Server
Configuration Manager.




There's more...



In SQL Server 2012, you can create user-defined server roles. We will detail this functionality
later. This interests us for now, because a server role could be used to grant CONNECT
permissions on an endpoint to a group of logins.
The following code creates a user-defined server role, adds a login as a member, and grants
the CONNECT privilege on the default TCP endpoint to the role:
USE [master];
CREATE SERVER ROLE [TCPRole];
ALTER SERVER ROLE [TCPRole] ADD MEMBER [my_login];
GRANT CONNECT ON ENDPOINT::[TSQL Default TCP] TO [TCPRole];





Reference: Microsoft SQL Server 2012 Security
Cookbook

AES algorith is stronger algorthm than 3DES/Triple-DES

AES is an algorithm adopted as a Federal U.S. government standard in 2002 and approved
by the NSA. It is a stronger algorithm than Triple-DES. When you choose the algorithm,
you need to balance between security and performances. AES_128 is gradually becoming
more vulnerable as new attacks are discovered but it is still considered reasonably safe. If your database contains classified
information, you should go for a bigger key, which is harder to crack. But the bigger the
key, the higher the performance impact will be. This being said, the performance impact
of TDE is relatively low.


Reference: Microsoft SQL Server 2012 Security Cookbook

List of services started automatically by Windows that you could consider stopping

# DHCP Client: On most servers, IP addresses are fixed and DHCP is not needed.

#DNS Client: This caches DNS names locally.

#Network Location Awareness: This collects and stores the configuration information
for the network and notifies the programs when this information is modified. You can't
disable it unless you use the Windows 2008 Advanced Firewall.

#Print Spooler: This loads the files to the memory for printing later. It can be disabled
if you don't print from the server.

#Windows Error Reporting Service: This allows errors to be reported when programs
stop working or responding, and allows existing solutions to be delivered. It can safely
be disabled.

#Windows Firewall: You can disable this service, if you are using a network firewall.

#Shell Hardware Detection: This activates AutoPlay for removable devices.
On Windows Server 2008 R2, this service stops when nobody is logged in,

to minimize risks. You can disable it.

Reference: Microsoft SQL Server 2012 Security
Cookbook

Ports and Protocols Used by Microsoft SQL Server

Service / PurposeProtocolPort
Analysis Services connections via HTTP (default)TCP80
Analysis Services connections via HTTPS (default)TCP443
ClusteringUDP135
ClusteringTCP135 (RPC) / 3343 (Cluster Network Driver) / 445 SMB / 139 NetBIOS / 5000-5099 (RPC) / 8011-8031 (RPC)
Database MirroringTCPThere is no default port for this service. Use the following T-SQL statements to identify which ports are in use: SELECT name, port FROM sys.tcp_endpoints.
Dedicated Administrator ConnectionTCP1434 by default (local port). But this port is assigned dynamically by SQL Server during startup.
FilestreamTCP139 y 445.
Microsoft Distributed Transaction Coordinator (MS DTC)TCP135
Reporting services Web ServicesTCP80
Reporting Services configured for use through HTTPSTCP1433
Service BrokerTCP4022
SQL Server 2008 Analysis ServicesTCP2382 (SQL Server Browser Services for SSAS port)
2383 (Clusters will listen only on this port)
SQL Server Browser Service (Database Engine)UDP1434. Might be required when using named instances.
SQL Server Browser ServiceTCP2382
SQL Server default instance running over an HTTPS endpoint.TCP443
SQL Server Instance (Database Engine) running over an HTTP endpoint.TCP80 y 443 (SSL).
SQL Server Integration ServicesTCP135 (DCOM).
SQL over TCP (default instance)TCP1433
Transact-SQL DebuggerTCP135
Windows Management InstrumentationTCP135 (DCOM)



References:


Configuring the Windows Firewall to Allow SQL Server Access.
http://msdn.microsoft.com/en-us/library/cc646023.aspx 

Enable a network for cluster use.
http://technet.microsoft.com/en-us/library/cc728293(WS.10).aspx


Friday, September 12, 2014

Script to list data types of all columns in a Database in SQL Server

SQL Server tables displays the data type of
each column inside the table. You can do this dozens of ways, but a popular method shown in
the following example joins the sys.objects table with the sys.columns table. There are two
functions that you may not be familiar with in the following code. The TYPE_NAME() function
translates the data type id into its proper name. To go the opposite direction, you could use the
TYPE_ID() function. The other function of note is SCHEMA_ID(), which is used to return the
identity value for the schema. This is useful primarily when you want to write reports against the
SQL Server metadata.


USE AdventureWorks
GO

SELECT o.NAME AS ObjectName
 ,c.NAME AS ColumnName
 ,TYPE_NAME(c.user_type_id) AS DataType
FROM sys.objects o
INNER JOIN sys.columns c ON o.object_id = c.object_id
WHERE o.NAME = 'Department'
 AND o.Schema_ID = SCHEMA_ID('HumanResources')