Simon's SQL

SQL,DBA,tuning,Trouble Shooting,Performance

Posts Tagged ‘SQL’

Alert email sending

Posted by Simon Cho on 06/30/2014

Email send when @objName has data. @objName should include DB name as well.
Ex) @objName = ‘DBA.dbo.vw_Job_Status’

CREATE PROCEDURE [dbo].[dmp_send_email_HTML_format]
@objName VARCHAR(255)
, @Subject VARCHAR(255)
, @PreDescription varchar(8000) = NULL
, @Recipients VARCHAR(8000)
, @importance VARCHAR(255) = ‘High’
, @profile VARCHAR(255) = ‘Public_Profile’ –eMail profile name
as
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

/*
Return value @r
10001 : object is wrong
10002 : column is wrong
0 : Done or Nodata.
*/

DECLARE @tableHTML NVARCHAR(max)
, @HTML_TH NVARCHAR(max)
, @HTML_TD NVARCHAR(max)
, @sql NVARCHAR(4000)
, @param NVARCHAR(4000)
, @DBName VARCHAR(255)
, @rowcnt INT
, @r INT

DECLARE @tbl_datacheck TABLE (i INT)

SET @DBName = PARSENAME(@objName,3)

SET @r = -1

IF OBJECT_ID(@objName) IS NULL BEGIN
SET @r = 10001
GOTO ERROR;
END

IF @DBName = ” OR @DBName IS NULL BEGIN
SET @DBName = DB_NAME()
END
SET @sql = ‘select top 1 1 from ‘+ @objName

INSERT INTO @tbl_datacheck
EXEC (@sql)
SET @rowcnt = @@ROWCOUNT

IF @rowcnt <=0 BEGIN
SET @r = 0
GOTO ERROR;
END

IF OBJECT_ID('tempdb.dbo.#tbl_column') IS NOT NULL
DROP TABLE #tbl_column
CREATE TABLE #tbl_column (idx INT IDENTITY(1,1), NAME VARCHAR(255))

set @sql = '
INSERT #tbl_column
SELECT name
FROM '+@DBName+'.SYS.syscolumns
WHERE ID = OBJECT_ID(@objName)
ORDER BY colorder
'
set @param = ' @objName varchar(255) '
exec sp_executesql @sql, @param, @objName

SET @rowcnt = @@ROWCOUNT
–PRINT @rowcnt

IF @rowcnt = 0 BEGIN
SET @r = 10002
GOTO ERROR;
END

SELECT @HTML_TH =
CAST ((SELECT th = isnull(NAME,' ')
FROM #tbl_column
FOR XML PATH(''), TYPE
) AS NVARCHAR(MAX) )

–print @HTML_TH
SET @HTML_TD = ''
SELECT @HTML_TD = @HTML_TD + 'td = isnull('+NAME+','' ''), '''', '
FROM #tbl_column

SET @HTML_TD = SUBSTRING(@HTML_TD, 1, LEN(@HTML_TD)-1)

SET @sql = 'SELECT @HTML_TD = CAST ((SELECT '+ REPLACE(@HTML_TD,'','''') +'
FROM '+@objName+'
FOR XML PATH(''tr''), TYPE
) AS NVARCHAR(MAX) )
'

–PRINT @sql

EXEC sp_executesql @sql, N' @HTML_TD NVARCHAR(MAX) OUTPUT ', @HTML_TD OUTPUT

–PRINT @HTML_TD

set @Recipients = isnull(@Recipients,'NowcomDBDevTeam@nowcom.com')

set @subject = @@servername +' – ' + convert(varchar(255), getdate(), 101) + ' ' + convert(varchar(5), getdate(), 108)+' ' + @Subject

SET @tableHTML = '

h1{
font-size:17px;
}
body{
font-size:15px;
}

table{
border-collapse:collapse;
border:1px solid black;
font-size:12px;
}

table th{
font:bold;
border:1px solid black;
padding-left:2px;padding-right:2px;padding-top:1px;padding-bottom:1px;
background-color:lightblue;
font-size:12px;
}

table td{
border:1px solid black;
padding-left:2px;padding-right:2px;padding-top:1px;padding-bottom:1px
font-size:12px;
}

‘+ @subject +’
‘+ ISNULL(@PreDescription,”) + ‘

‘+ @HTML_TH +’


+ @HTML_TD

set @tableHTML = @tableHTML + ‘

SELECT @tableHTML = ‘From Server :’ + ISNULL(@@SERVERNAME, ”) + char(13) + char(10) + ISNULL(@tableHTML, ”);

EXEC msdb.dbo.sp_send_dbmail @profile_name = @profile
, @body_format = ‘HTML’
, @recipients = @Recipients
, @importance = @importance
, @copy_recipients =”
, @subject = @subject
, @body = @tableHTML
IF @@error 0 BEGIN
RAISERROR(‘Error in dmp_send_email_HTML_format. Failed to send Database Mail.’, 16, 1);
END
RETURN 0
ERROR:
return @r
END
Go

Posted in Common | Tagged: , | Leave a Comment »

SSMS tool pack and other useful free tools

Posted by Simon Cho on 07/11/2012

http://www.ssmstoolspack.com/

http://www.sqlserverutilities.com/

Posted in Common | Tagged: , , | Leave a Comment »

concatenate string value in SQL

Posted by Simon Cho on 03/08/2011

Hi all,

Have you ever need a concatenation string function for group by clause?

Here is the solution.

http://www.projectdmx.com/tsql/rowconcatenate.aspx

And I like this process.

Ex)

set nocount on;

if OBJECT_ID(‘test’) is not null

drop table test

go

create table test

(

i int identity(1,1)

,c varchar(255)

)

go

insert into test (c)

select ‘a’

union all

select ‘b’

go 100

select i%2, replace(replace(max(b.list),,),,) as sum_c

from test a

cross apply (

select c + ‘,’ as c

from test

where i%2 = a.i%2

for xml path()

) b (list)

group by a.i%2

go

 

Posted in Common | Tagged: , , , | Leave a Comment »

MSDN Technical Articles for SQL servers.

Posted by Simon Cho on 03/07/2011

http://technet.microsoft.com/en-us/library/bb545450.aspx

<SQL 2008 R2>

http://technet.microsoft.com/en-us/library/bb418445%28SQL.10%29.aspx

<SQL 2008>

http://technet.microsoft.com/en-us/library/dd631815%28SQL.10%29.aspx

<SQL 2005>

http://technet.microsoft.com/en-us/library/ee229559%28SQL.10%29.aspx

http://technet.microsoft.com/en-us/library/ff928326%28SQL.10%29.aspx

Posted in Common | Tagged: , , | Leave a Comment »

all information about SQL “Memory To Leave” area (also called MTL or “Memory To Reserve”).

Posted by Simon Cho on 03/07/2011

The Database Administrator’s Guide to the SQL Server Database Engine .NET Common Language Runtime Environment
http://www.sqlskills.com/resources/Whitepapers/SQL%20Server%20DBA%20Guide%20to%20SQLCLR.htm

If you use linked server queries, you need to read this….
http://blogs.msdn.com/psssql/archive/2009/09/22/if-you-use-linked-server-queries-you-need-to-read-this.aspx

How It Works: DBCC MemoryStatus Locked Pages Allocated and SinglePageAllocator Values
http://blogs.msdn.com/psssql/archive/2009/05/15/how-it-works-dbcc-memorystatus-locked-pages-allocated-and-singlepageallocator-values.aspx

I had log backup jobs setup which started to fail intermittently for last couple of weeks. The SQL server complained of “insufficient system memory”. The jobs were backed up as part of maintenance plan – maxtransfersize option
http://www.sqlservercentral.com/Forums/Topic362968-24-1.aspx

How to find who is using / eating up the Virtual Address Space on your SQL Server
http://blogs.msdn.com/sqlserverfaq/archive/2010/02/16/how-to-find-who-is-using-eating-up-the-virtual-address-space-on-your-sql-server.aspx

VirtualAlloc Function
http://msdn.microsoft.com/en-us/library/aa366887%28VS.85%29.aspx

SQL Server encounters memory pressure and generate 701 Error due to incorrect configuration of Service Broker
http://blogs.msdn.com/sqlserverfaq/archive/2010/03/25/sql-server-encounters-memory-pressure-and-generate-701-error-due-to-incorrect-configuration-of-service-broker.aspx

SQLOS’s memory manager and SQL Server’s Buffer Pool
http://blogs.msdn.com/slavao/archive/2005/02/11/371063.aspx

FAIL_VIRTUAL_RESERVE
http://www.sqlservercentral.com/Forums/Topic432976-360-1.aspx

Fix for “Failed Virtual Allocate Bytes” error? – Service Broker problem
http://social.msdn.microsoft.com/Forums/en/sqlservicebroker/thread/1d527f9e-5497-4e02-a2b2-f0ee3a386326

FIX: Error message when you use SQL Server Service Broker in SQL Server 2005: “Failed Virtual Allocate Bytes: FAIL_VIRTUAL_RESERVE 589824”
http://support.microsoft.com/default.aspx?scid=kb;en-us;959007

INF: Using DBCC MEMORYSTATUS to Monitor SQL Server Memory Usage
http://support.microsoft.com/kb/271624

Server Memory Options
http://msdn.microsoft.com/en-us/library/ms178067.aspx

Various memory errors are logged to SQL Server error log when using SQL CLR objects
http://support.microsoft.com/kb/969962

FIX: The memory usage of a SQL Server service increases quickly when you run a query that uses a linked server in SQL Server 2005 or in SQL Server 2008
http://support.microsoft.com/kb/971622

AppDomain marked for unload due to memory pressure- Jonathan Kehayias

http://social.msdn.microsoft.com/Forums/en/sqlnetfx/thread/e5ca2988-df87-4ce4-8fb7-b338a81a390e

help with this AppDomain event- Jonathan Kehayias

http://social.msdn.microsoft.com/forums/en-US/sqlnetfx/thread/cc1b3e43-0db8-4e75-b5ab-bc2a4c93b12b/

SQL Server 2005: CLR Integration

http://blogs.msdn.com/sqlclr/archive/2006/03/24/560154.aspx

Using the SQL Server Service Startup Options
http://msdn.microsoft.com/en-us/library/ms190737.aspx

Come on 64bit so we can leave the mem…. – Bob Ward

http://blogs.msdn.com/b/psssql/archive/2009/08/26/come-on-64bit-so-we-can-leave-the-mem.aspx

 

Simon Cho

Posted in Common | Tagged: , , , , | 2 Comments »