Skip to content

Clive Roberts

SQL DBA

Menu
  • Setup
  • Performance
  • Health Check
  • Troubleshooting
  • T-SQL

Category: T-SQL

Database Engine Stored Procedures (Transact-SQL)

May 8, 2015
| No Comments
| SQL Server, T-SQL

Database Engine Stored Procedures (Transact-SQL) SQL Server 2014 QL Server supports the following system stored procedures that are used for general maintenance of an instance of SQL Server. sp_add_data_file_recover_suspect_db sp_executesql sp_add_log_file_recover_suspect_db sp_getapplock sp_addextendedproc sp_getbindtoken sp_addextendedproperty sp_help sp_addmessage sp_helpconstraint sp_addtype sp_helpdb sp_addumpdevice sp_helpdevice sp_altermessage sp_helpextendedproc sp_attach_db sp_helpfile sp_attach_single_file_db sp_helpfilegroup sp_autostats sp_helpindex sp_bindefault sp_helplanguage sp_bindrule sp_helpserver sp_bindsession…

Read More »

Posted in SQL Server, T-SQL

SET TRANSACTION ISOLATION LEVEL (Transact-SQL)

May 7, 2015
| No Comments
| SQL Server, T-SQL

READ UNCOMMITTED Specifies that statements can read rows that have been modified by other transactions but not yet committed. Transactions running at the READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction. READ UNCOMMITTED transactions are also not blocked by exclusive locks that would…

Read More »

Posted in SQL Server, T-SQL

CODE:CONCAT Function

May 1, 2015
| No Comments
| SQL Server, T-SQL

CONCAT Function Prior to SQL Server 2012, we used to use the “+” operator to combine or concatenate two or more string values but starting with SQL Server 2012, we can use CONCAT function to perform this type of operation more neatly. For example, the below script and screenshot shows usage of the “+” operator…

Read More »

Posted in SQL Server, T-SQL

CODE: STUFF Function

May 1, 2015
| No Comments
| SQL Server, T-SQL

STUFF Function We looked at using the CONCAT function to concatenate or combine two or more string values. With this function, string values are appended at the end of the prior string but there are times when you want to insert a string value into another string value or replace a part of one string…

Read More »

Posted in SQL Server, T-SQL

CODE: SET XACT_ABORT

March 30, 2015
| No Comments
| SQL Server, T-SQL

SET XACT_ABORT is helpful in many ways. example: suppose you are working on a stored procedure which is doing a complex data manipulation. You though this query will run with in 1 minute when you run it from .NET or other code , but some how it took quite long time and with in one…

Read More »

Posted in SQL Server, T-SQL

Locking hint

March 7, 2015
| No Comments
| SQL Server, T-SQL

Locking hint Description HOLDLOCK Hold a shared lock until completion of the transaction instead of releasing the lock as soon as the required table, row, or data page is no longer required. HOLDLOCK is equivalent to SERIALIZABLE. NOLOCK Do not issue shared locks and do not honor exclusive locks. When this option is in effect,…

Read More »

Posted in SQL Server, T-SQL

MS SQL pivit table

June 9, 2014
| No Comments
| SQL Server, T-SQL

select pvt.abc,pvt.def,pvt.ghi from ( SELECT ConnectionName, Connection.ConnectionString FROM dbo.Global_Applications_ConnectionString AS Connection ) AS SourceTable pivot ( min(ConnectionString) for ConnectionName in (abc,def,ghi) ) as pvt

Read More »

Posted in SQL Server, T-SQL

CODE: last_value

January 15, 2014
| No Comments
| SQL Server, T-SQL

SELECT CustomerID , SalesOrderID , CAST(OrderDate AS DATE) AS OrderDate , TotalDue , LAST_VALUE(TotalDue) OVER ( PARTITION BY CustomerID ORDER BY SalesOrderID ) AS TD FROM Sales.SalesOrderHeader ORDER BY CustomerID

Read More »

Posted in SQL Server, T-SQL

CODE: Parse XML to table

December 15, 2013
| No Comments
| SQL Server, T-SQL

CREATE PROCEDURE dbo.ParseXMLtoTable @strXML AS XML ,@rootnode NVARCHAR(255) AS BEGIN SET NOCOUNT ON DECLARE @strText AS NVARCHAR(MAX) ,@idoc INT ,@id INT ,@parentid INT IF OBJECT_ID(‘tempdb..#ChildList’) IS NOT NULL DROP TABLE #ChildList CREATE TABLE #ChildList ( [RowNum] INT IDENTITY(1,1) NOT NULL, [parentid] INT NULL, [id] INT NULL, PRIMARY KEY (RowNum)) IF OBJECT_ID(‘tempdb..#NodeList’) IS NOT NULL DROP…

Read More »

Posted in SQL Server, T-SQL

CODE: Search a string in a text with number of occurrences

November 15, 2013
| No Comments
| SQL Server, T-SQL

IF EXISTS (SELECT * FROM sys.objects WHERE type = ‘P’ AND name = ‘Usp_findstring’) DROP PROCEDURE Usp_findstring GO CREATE PROC Usp_findstring (@INPUTSTRING VARCHAR(max), @SEARCHSTRING VARCHAR(500)) AS BEGIN DECLARE @OCCURENCES AS INT, @POSITION AS INT, @RETURNVALUE INT SET @OCCURENCES = 0 SET @POSITION = 0 WHILE @POSITION < Len(@INPUTSTRING) BEGIN IF Charindex(@SEARCHSTRING, @INPUTSTRING, @POSITION) > 0…

Read More »

Posted in SQL Server, T-SQL

CODE: Calculating the Difference (Duration) Between Two DATETIMES

March 15, 2013
| No Comments
| SQL Server, T-SQL

— “DT” is an abbreviation for “DATETIME”, not “DATE” DECLARE @StartDT DATETIME ,@EndDT DATETIME ; SELECT @StartDT = ‘2000-01-01 10:30:50.780’ ,@EndDT = ‘2000-01-02 12:34:56.789’ ; –===== Display the dates and the duration SELECT StartDT = @StartDT ,EndDT = @EndDT ,Duration = @EndDT-@StartDT ;

Read More »

Posted in SQL Server, T-SQL

Code:Output a file from a stored procedures in MS SQL

September 19, 2011
| No Comments
| SQL Server, T-SQL

How to output a file from a stored procedures in MS SQL, this is how I did it a few years ago exec master..xp_cmdShell ‘bcp “SELECT url.loc, url.publication_date, url.keywords FROM ICONews..vw_googlenews url For XML Auto,Elements” queryout C:\Inetpub\wwwroot\website\SQL\google.xml -c -t, -T -S IP address’ Link: http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell

Read More »

Posted in SQL Server, T-SQL

Code: MS SQL To find the start of the month

March 2, 2010
| No Comments
| SQL Server, T-SQL

MS SQL To find the start of the month This code: DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) was excuted on 2/3/2010 at 16:11 the result: 01/03/2010 00:00:00 Software engineers do the job better!

Read More »

Posted in SQL Server, T-SQL

Categories

Sidebar

  • October 2026 (25)
  • November 2017 (1)
  • March 2017 (1)
  • October 2016 (13)
  • July 2016 (1)
  • May 2016 (1)
  • April 2016 (2)
  • March 2016 (1)
  • February 2016 (1)
  • November 2015 (1)
  • October 2015 (1)
  • September 2015 (1)
  • August 2015 (1)
  • July 2015 (7)
  • June 2015 (1)
  • May 2015 (6)
  • April 2015 (2)
  • March 2015 (2)
  • February 2015 (1)
  • January 2015 (1)
  • December 2014 (1)
  • October 2014 (1)
  • September 2014 (1)
  • August 2014 (1)
  • July 2014 (1)
  • June 2014 (1)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (1)
  • January 2014 (1)
  • December 2013 (1)
  • November 2013 (1)
  • March 2013 (1)
  • January 2013 (1)
  • May 2012 (1)
  • April 2012 (1)
  • October 2011 (1)
  • September 2011 (1)
  • March 2010 (1)
  • Setup
  • Performance
  • Health Check
  • Troubleshooting
  • T-SQL

Precious Lite 2025 | All Rights Reserved. Precious Lite theme by Flythemes