format / convert

September 17, 2014

Tuning a piece of code today I managed to get the execution time down from 32 minutes to 2 minutes by replacing a format function with a convert function. Format is a clr function and doesnt perform well against large datasets.

sidestep view/funct​ion limitation​s

September 16, 2014

Problem: need to create an object that can be used in a select from statement but due to limitations such as not being allowed to use variables in views or use side effecting operators in functions no object can be easily created.

Solution:

Create a stored procedure which returns the required dataset
Ensure a self referencing linked server exists on the server
Create view as a select * from openquery(SERVER,’exec stored_proc_name’)

in some cases you may need to modify the openquery statement

if your proc uses dml use the nocount on clause
if your proc uses temp table use the set fmtonly on clause

eg: Create view as a select * from openquery(SERVER,’SET NOCOUNT ON;SET FMTONLY OFF;exec stored_proc_name’)

View running jobs

September 5, 2014

EXEC master.dbo.xp_sqlagent_enum_jobs 1,’xxx’ will output a table where running = 1 indicates a job is running

the following will return a list of running jobs

SELECT b.name
FROM msdb.dbo.sysjobactivity a
INNER JOIN msdb.dbo.sysjobs b ON a.job_id=b.job_id
WHERE session_id = (SELECT MAX(session_id) FROM msdb.dbo.syssessions)
AND start_execution_date IS NOT NULL AND stop_execution_date IS NULL

add a row details group to a SSRS table where all other groups have been deleted

September 2, 2014

View code, search for TablixRowHierarchy

replace {TablixMember /}

with

{TablixMember}
{Group Name=”Details” /}
{TablixMembers}
{TablixMember /}
{/TablixMembers}
{/TablixMember}

and switch back to your report, make sure to replace curly brackets with less than / greater than symbols (which are not allowed by wordpress on this page)

Capturing dead lock graph using extended event

September 1, 2014

event: xml_deadlock_report

output the data to a file so that when you view target data you will be presented a deadlock graph which give you the visual deadlock graph, this isnt visible if you output to ring buffer

Allow non admin users to connect to SSIS remotely

August 26, 2014

http://msdn.microsoft.com/en-us/library/aa337083.aspx

When a user without sufficient rights attempts to connect to an instance of Integration Services on a remote server, the server responds with an “Access is denied” error message. You can avoid this error message by ensuring that users have the required DCOM permissions.

If the user is not a member of the local Administrators group, add the user to the Distributed COM Users group. You can do this in the Computer Management MMC snap-in accessed from the Administrative Tools menu.
Open Control Panel, double-click Administrative Tools, and then double-click Component Services to start the Component Services MMC snap-in.

Expand the Component Services node in the left pane of the console. Expand the Computers node, expand My Computer, and then click the DCOM Config node.
Select the DCOM Config node, and then select SQL Server Integration Services 11.0 in the list of applications that can be configured.

Right-click on SQL Server Integration Services 11.0 and select Properties.

In the SQL Server Integration Services 11.0 Properties dialog box, select the Security tab.
Under Launch and Activation Permissions, select Customize, then click Edit to open the Launch Permission dialog box.

In the Launch Permission dialog box, add or delete users, and assign the appropriate permissions to the appropriate users and groups. The available permissions are Local Launch, Remote Launch, Local Activation, and Remote Activation. The Launch rights grant or deny permission to start and stop the service; the Activation rights grant or deny permission to connect to the service.

Click OK to close the dialog box.

Under Access Permissions, repeat steps 7 and 8 to assign the appropriate permissions to the appropriate users and groups.

Close the MMC snap-in.

Restart the Integration Services service.

What is filling up the transaction log?

August 14, 2014

SELECT a.transaction_id ,
DB_NAME(database_id) dbase_name,
database_transaction_begin_time ,
database_transaction_log_record_count ,
database_transaction_log_bytes_used ,
database_transaction_log_bytes_reserved ,
b.session_id
FROM sys.dm_tran_database_transactions a
LEFT JOIN sys.dm_tran_session_transactions b ON a.transaction_id = b.transaction_id
ORDER BY database_transaction_log_record_count DESC

Optimizing (replacing) recursive cte’s

July 24, 2014

Recursive cte’s do not perform well when you are dealing with millions of records.

The following approach is not particularly elegant but performs so much better.

–Create table to store results

CREATE TABLE #RESULTS(ID INT NOT NULL, PARENTID UNIQUEIDENTIFIER NOT NULL, CHILDID UNIQUEIDENTIFIER, LEVEL INT IDENTITY(1,1) NOT NULL);

SET IDENTITY_INSERT #RESULTS ON;

INSERT #RESULTS(ID,PARENTID,CHILDID,LEVEL)
SELECT ID,PARENTID,CHILDID,1
FROM #SOURCE
WHERE PARENTID=@START;

WHILE @@ROWCOUNT>0
INSERT #RESULTS(ID,PARENTID,CHILDID,LEVEL)
SELECT S.ID,S.PARENTID,S.CHILDID,SCOPE_IDENTITY()+1
FROM #RESULTS AS R
JOIN #SOURCE AS S
ON S.PARENTID = R.CHILDID
WHERE LEVEL = SCOPE_IDENTITY();

The scope indentity function tells us the value of the last identity that was inserted within the current scope.
The while will discontinue when no more rows are inserted.
You could achieve similar results using an int variable rather than an identity field if you prefer

create a sql server linked server with a name different to the server that is being linked to

July 1, 2014

Provider = sql server native client
datasource = server name
catalog = database

key is not to use server type = sql server in the gui

delete file older than a given date with xp_delete_file

May 28, 2014

example

EXECUTE master.dbo.xp_delete_file 0,N’C:\SQLServer\Backup’,N’bak’,’2014-05-27T17:29:04′

this will delete all files in the specified location with a .bak extension which are older than the specified date


Follow

Get every new post delivered to your Inbox.