XML TAGS simplifies Excel Download

Standard

Source:https://wiki.scn.sap.com/wiki/display/Snippets/XML+TAGS+simplifies+Excel+Download?original_fqdn=wiki.sdn.sap.com 

In this post I will be explaining how we can utilize starndard xml tags for formatting data in excel.The use of this approach would eliminate the need of external apis. With standard xml approach we could download data into excel, but we could not do any formatting in the excel file. But this approach also provides lots of flexibility in formatting the excel file. The first thing comes into mind when we hear the word formatting is changing font of the text. But this time we would try to do something beyond the  traditional way of formatting e.g. merging of cells, adding drop downs, fixing width of a cell etc.

Note!

Below hierarchy contains only list of tags which we can use in XML document, but doesn’t mention about attributes available for each to use. For full reference on this please visit XML Spreadsheet Reference.

XML Tags Hierarchy

<ss:Workbook>
    <ss:Styles>
        <ss:Style>
            <ss:Alignment/>
            <ss:Borders>
                <ss:Border/>
            </ss:Borders>
            <ss:Font/>
            <ss:Interior/>
            <ss:NumberFormat/>
            <ss:Protection/>
        </ss:Style>
    </ss:Styles>
    <ss:Names>
        <ss:NamedRange/>
    </ss:Names>
    <ss:Worksheet>
        <ss:Names>
            <ss:NamedRange/>
        </ss:Names>
        <ss:Table>
            <ss:Column/>
            <ss:Row>
                <ss:Cell>
                    <ss:NamedCell/>
                    <ss:Data>
                        <Font/>
                        <B/>
                        <I/>
                        <U/>
                        <S/>
                        <Sub/>
                        <Sup/>
                        <Span/>
                    </ss:Data>
                    <x:PhoneticText/>
                    <ss:Comment>
                        <ss:Data>
                            <Font/>
                            <B/>
                            <I/>
                            <U/>
                            <S/>
                            <Sub/>
                            <Sup/>
                            <Span/>
                        </ss:Data>
                    </ss:Comment>
                    <o:SmartTags>
                        <stN:SmartTag/>
                    </o:SmartTags>
                </ss:Cell>
            </ss:Row>
        </ss:Table>
        <c:WorksheetOptions>
            <c:DisplayCustomHeaders/>
        </c:WorksheetOptions>
        <x:WorksheetOptions>
            <x:PageSetup>
                <x:Layout/>
                <x:PageMargins/>
                <x:Header/>
                <x:Footer/>
            </x:PageSetup>
        </x:WorksheetOptions>
        <x:AutoFilter>
            <x:AutoFilterColumn>
                <x:AutoFilterCondition/>
                <x:AutoFilterAnd>
                    <x:AutoFilterCondition/>
                </x:AutoFilterAnd>
                <x:AutoFilterOr>
                    <x:AutoFilterCondition/>
                </x:AutoFilterOr>
            </x:AutoFilterColumn>
        </x:AutoFilter>
    </ss:Worksheet>
    <c:ComponentOptions>
        <c:Toolbar>
            <c:HideOfficeLogo/>
        </c:Toolbar>
    </c:ComponentOptions>
    <o:SmartTagType/>
</ss:Workbook> 


All of the formatting needed in the excel sheet are encapsulated in the <ss:Styles></ss:Styles> section or they can be specified individually for each cell within the tag <ss:Cell></ss:Cell>.

Example

XML CODE 

<?xml version=”1.0″?>
<?mso-application progid=”Excel.Sheet”?>
<Workbook xmlns=”urn:schemas-microsoft-com:office:spreadsheet”
 xmlns:o=”urn:schemas-microsoft-com:office:office”
 xmlns:x=”urn:schemas-microsoft-com:office:excel”
 xmlns:ss=”urn:schemas-microsoft-com:office:spreadsheet”
 xmlns:html=”http://www.w3.org/TR/REC-html40″> <Styles>
  <Style ss:ID=”Default” ss:Name=”Normal”>
   <Alignment ss:Vertical=”Bottom”/>
   <Borders/>
   <Font/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID=”s1″>
   <Alignment ss:Vertical=”Center”/>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
  </Style>
  <Style ss:ID=”s2″>
   <Alignment ss:Vertical=”Center”/>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
  </Style>
  <Style ss:ID=”s3″>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
  </Style>
  <Style ss:ID=”s4″>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
   <Font x:Family=”Swiss” ss:Bold=”1″/>
   <Interior ss:Color=”#CCFFFF” ss:Pattern=”Solid”/>
  </Style>
  <Style ss:ID=”s5″>
   <Alignment ss:Vertical=”Bottom” ss:WrapText=”1″/>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
   <Font x:Family=”Swiss” ss:Bold=”1″/>
   <Interior ss:Color=”#CCFFFF” ss:Pattern=”Solid”/>
  </Style>
  <Style ss:ID=”s6″>
   <Alignment ss:Vertical=”Bottom” ss:WrapText=”1″/>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
  </Style>
  <Style ss:ID=”s7″>
   <Alignment ss:Vertical=”Bottom” ss:WrapText=”1″/>
  </Style>
  <Style ss:ID=”s8″>
   <Alignment ss:Horizontal=”Center” ss:Vertical=”Bottom”/>
   <Borders>
    <Border ss:Position=”Bottom” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Left” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Right” ss:LineStyle=”Continuous” ss:Weight=”1″/>
    <Border ss:Position=”Top” ss:LineStyle=”Continuous” ss:Weight=”1″/>
   </Borders>
   <Font x:Family=”Swiss” ss:Size=”24″ ss:Bold=”1″/>
  </Style>
 </Styles>
 <Worksheet ss:Name=”Sheet1″>
  <Table ss:ExpandedColumnCount=”4″ ss:ExpandedRowCount=”9″ x:FullColumns=”1″
   x:FullRows=”1″>
   <Column ss:AutoFitWidth=”0″ ss:Width=”135″/>
   <Column ss:Index=”3″ ss:StyleID=”s7″ ss:AutoFitWidth=”0″ ss:Width=”66.75″/>
   <Row ss:Height=”30″>
    <Cell ss:MergeAcross=”3″ ss:StyleID=”s8″><Data ss:Type=”String”>Title</Data></Cell>
   </Row>
   <Row>
    <Cell ss:StyleID=”s4″><Data ss:Type=”String”>Column1</Data></Cell>
    <Cell ss:StyleID=”s4″><Data ss:Type=”String”>Column2</Data></Cell>
    <Cell ss:StyleID=”s5″><Data ss:Type=”String”>Column3</Data></Cell>
    <Cell ss:StyleID=”s4″><Data ss:Type=”String”>Column4</Data></Cell>
   </Row>
   <Row ss:Height=”76.5″>
    <Cell ss:MergeDown=”1″ ss:StyleID=”s1″><Data ss:Type=”String”>Row 3 & Row 4 Merged</Data></Cell>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Red</Data></Cell>
    <Cell ss:StyleID=”s6″><Data ss:Type=”String”>Wrap text when width of the text exceeds the specified width</Data></Cell>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:Index=”2″ ss:StyleID=”s3″><Data ss:Type=”String”>Black</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Row 4</Data></Cell>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Black</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Row 5</Data></Cell>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Red</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Row 6</Data></Cell>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Black</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:MergeDown=”1″ ss:StyleID=”s2″><Data ss:Type=”String”>Row 8 & 9 Merged</Data></Cell>
    <Cell ss:StyleID=”s3″><Data ss:Type=”String”>Green</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
   <Row>
    <Cell ss:Index=”2″ ss:StyleID=”s3″><Data ss:Type=”String”>Red</Data></Cell>
    <Cell ss:StyleID=”s6″/>
    <Cell ss:StyleID=”s3″/>
   </Row>
  </Table>  <DataValidation xmlns=”urn:schemas-microsoft-com:office:excel”>
   <Range>R2C2:R1000C2</Range>
   <Type>List</Type>
   <CellRangeList/>
   <Value>”Red, Black, Green”</Value>
  </DataValidation>
 </Worksheet></Workbook> 

Save this code into an .xml file and open with excel to see whether your code is correct.Now all you have to do is to generate a xml string programmatically and download into .xml file. 

Reference

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

Python 入门

Standard

python入门

​ http://www.imooc.com/learn/177

实验楼简单实验

​ 200行Python代码实现2048

​ Python 图片转字符画

​ Python3 & OpenCV 视频转字符动画

python(进阶)

​ http://www.imooc.com/learn/317

实验楼有难度的实验+python爬虫

​ k-近邻算法实现手写数字识别系统–《机器学习实战 》

​ 深度学习初探——入门DL主流框架

​ 川普撞脸希拉里(基于 OpenCV 的面部特征交换

​ Python3 实现火车票查询工具

​ 神经网络实现手写字符识别系统

条形码code128编码规则

Standard

条形码或称条码barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成的平行线图案。条形码可以标出物品的生产国、制造厂家、商品名称、生产日期、图书分类号、邮件起止地点、类别、日期等信息,因而在商品流通、图书管理、邮政管理、银行系统等许多领域都得到了广泛的应用。

条形码分类

条形码按类型可分为:线性条形码矩阵(二维)条形码

code128编码

Code 128是ISO/IEC 15417:2007[1]定义的条形码规范。

Code 128条码可以对全部128个ASCII字符(包括数字、大小写字母、符号和控制符)进行编码。

code128码是广泛应用在企业内部管理、生产流程、物流控制系统方面的条码码制,由于其优良的特性在管理信息系统的设计中被广泛使用,CODE128码是应用最广泛的线性条形码制之一。

code128编码分类

  • code128 A字符集:包括大写字母、数字、常用标点符号和一些控制符。
  • code128B 字符集:包括大小写字母、数字、常用标点符号。
  • code128C 字符集: 为纯数字序列。
  • code128Auto:是将上述三种字符集最佳优化组合。

code128编码构成

一个Code 128条形码由六部分组成。

  1. 空白区域
  2. 起始标记
  3. 数据区
  4. 校验符
  5. 终止符
  6. 空白区域

code128编码表

IDCode128ACode128BCode128CBandCode图案
0SPSP021222211011001100
1!!122212211001101100
2222222111001100110
3##312122310010011000
4$$412132210010001100
5%%513122210001001100
6&&612221310011001000
7712231210011000100
8((813221210001100100
9))922121311001001000
10**1022131211001000100
11++1123121211000100100
12,,1211223210110011100
131312213210011011100
14..1412223110011001110
15//1511322210111001100
16001612312210011101100
17111712322110011100110
18221822321111001110010
19331922113211001011100
20442022123111001001110
21552121321211011100100
22662222311211001110100
23772331213111101101110
24882431122211101001100
25992532112211100101100
26::2632122111100100110
27;;2731221211101100100
28<<2832211211100110100
29==2932221111100110010
30>>3021212311011011000
31??3121232111011000110
32@@3223212111000110110
33AA3311132310100011000
34BB3413112310001011000
35CC3513132110001000110
36DD3611231310110001000
37EE3713211310001101000
38FF3813231110001100010
39GG3921131311010001000
40HH4023111311000101000
41II4123131111000100010
42JJ4211213310110111000
43KK4311233110110001110
44LL4413213110001101110
45MM4511312310111011000
46NN4611332110111000110
47OO4713312110001110110
48PP4831312111101110110
49QQ4921133111010001110
50RR5023113111000101110
51SS5121311311011101000
52TT5221331111011100010
53UU5321313111011101110
54VV5431112311101011000
55WW5531132111101000110
56XX5633112111100010110
57YY5731211311101101000
58ZZ5831231111101100010
59[[5933211111100011010
60\\6031411111101111010
61]]6122141111001000010
62^^6243111111110001010
63__6311122410100110000
64NUL`6411142210100001100
65SOHa6512112410010110000
66STXb6612142110010000110
67ETXc6714112210000101100
68EOTd6814122110000100110
69ENQe6911221410110010000
70ACKf7011241210110000100
71BELg7112211410011010000
72BSh7212241110011000010
73HTi7314211210000110100
74LFj7414221110000110010
75VTk7524121111000010010
76FFI7622111411001010000
77CRm7741311111110111010
78SOn7824111211000010100
79SIo7913411110001111010
80DLEp8011124210100111100
81DC1q8112114210010111100
82DC2r8212124110010011110
83DC3s8311421210111100100
84DC4t8412411210011110100
85NAKu8512421110011110010
86SYNv8641121211110100100
87ETBw8742111211110010100
88CANx8842121111110010010
89EMy8921214111011011110
90SUBz9021412111011110110
91ESC{9141212111110110110
92FS|9211114310101111000
93GS}9311134110100011110
94RS~9413114110001011110
95USDEL9511411310111101000
96FNC3FNC39611431110111100010
97FNC2FNC29741111311110101000
98SHIFTSHIFT9841131111110100010
99CODECCODEC9911314110111011110
100CODEBFNC4CODEB11413110111101110
101FNC4CODEACODEA31114111101011110
102FNC1FNC1FNC141113111110101110
103StartAStartAStartA21141211010000100
104StartBStartBStartB21121411010010000
105StartCStartCStartC21123211010011100
106StopStopStop23311121100011101011

code128检验位计算

(开始位对应的ID值 + 每位数据在整个数据中的位置×每位数据对应的ID值)% 103

Code128编码示例

以 PZ1704946715 为例,开始为code128A,开始位对应的ID为103,第1位数据P对应的ID为48,第2位数据Z对应的ID为58,依此类推,可以计算。

检验位 =(103 + 1*48 + 2*58 + 3*17 + 4*23 + 5*16 + 6*20 +7*25 + 8*20 + 9*22 + 10*23 + 11*17 + 12*21)%103 = 61​

即检验位的ID为61。

对照编码表,PZ1704946715 编码表示为:开始位Start Code A(11010000100)+ 数据位[P(11101110110)+ Z(11101100010)+1(10011100110)+ 7(11101101110)+ 0(10011101100)+ 4(11001001110)+ 9(11100101100)+ 4(11001001110)+ 6(11001110100)+ 7(11101101110)+ 1(10011100110)+ 5(11011100100)]+ 检验位61(11001000010)+ 结束位Stop(1100011101011)

若要打印,只需将1用黑色线标出,0用白色线标出,就完成一个简单的条形码生成。

http://liuchang.men/2019/04/09/%E6%9D%A1%E5%BD%A2%E7%A0%81code128%E7%BC%96%E7%A0%81%E8%A7%84%E5%88%99/

How to receive Deadlock information automatically via email

Standard

By Geoff Albin, 2014/01/03 (first published: 2010/12/13)

Receive Deadlock detail via email.

I had spent many hours on Google trying to find the best way to send a notification on deadlock occurrences. While it was a fairly straight forward process on how we get notified a deadlock has occurred, I wanted a bit more. I not only wanted to be told when the Deadlock occurred, I wanted to also be emailed the actual Deadlock information.

Every time a deadlock occurs in SQL Server, the detailed info about things like the SPID, the statement that was running, who the victim was, and so on does get logged. But getting the generic alert that SQL Server can create simply will tell you “something” has occurred. It would be the dutiful DBA’s job to log into the server in question and dig into the Error Log to get the Deadlock details.

Since capturing Deadlock info is not turned on by default. We do have to do two things in order to make the scripts I have written work properly.

Requirement number one

The first requirement is to turn on the appropriate Trace Flags. We do that by running the following command.

DBCC TRACEON (3605,1204,1222,-1)

A brief overview to what those flags do is listed below.

  • 3605 = write what we want to the error log.
  • 1204 = Capture Deadlock Events.
  • 1222 = Capture Deadlock Events with more info (SQL 2005 and higher)

It is important to note that setting trace flags this way will only work until the next time SQL Server is restarted. If you want to make sure your trace flags are always set this way, you can edit the startup options of SQL Server by using the -T switch or creating a SQL Agent job to run that code when the Agent starts.

Requirement number two

The second requirement is to ensure you have DB Mail setup and working. I will provide no detail on how to accomplish that. It will just be assumed that you have a working DB Mail profile.

Lets get started.

So now we have trace flags set and DB Mail is working we are ready to get into how we send the Deadlock information to an email address when a Deadlock occurs.

Since the structure of the error log changed in SQL 2005, we have two ways of doing this. Each method is basically the same, however, pay attention to where you deploy this script. I have included detailed comments so you can follow along.

For SQL Server 2000

--== This is for SQL 2000. ==--
--== We will create a temporary table to hold the error log detail. ==--
--== Before we create the temporary table, we make sure it does not already exist. ==--
 IF OBJECT_ID('tempdb.dbo.ErrorLog') IS Not Null
 BEGIN
 DROP TABLE tempdb.dbo.ErrorLog
 END
 --== We have checked for the existence of the temporary table and dropped it if it was there. ==--
 --== Now, we can create the table called tempdb.dbo.ErrorLog ==--
 CREATE TABLE tempdb.dbo.ErrorLog (Id int IDENTITY (1, 1) NOT NULL,
 ERRORLOG VARCHAR(4000), ContRow int)
--== We create a 3 column table to hold the contents of the SQL Server Error log. ==--
--== Then we insert the actual data from the Error log into our newly created table. ==--
 INSERT INTO tempdb.dbo.ErrorLog
 EXEC master.dbo.sp_readerrorlog
--== With our table created and populated, we can now use the info inside of it. ==--
 BEGIN
--== Set a variable to get our instance name. ==--
--== We do this so the email we receive makes more sense. ==--
 declare @servername nvarchar(150)
 set @servername = @@servername
--== We set another variable to create a subject line for the email. ==-- 
 declare @mysubject nvarchar(200)
 set @mysubject = 'Deadlock event notification on server '+@servername+'.'
 --== Now we will prepare and send the email. Change the email address to suite your environment. ==-- 
 exec master.dbo.xp_sendmail @recipients = 'DBA_Super_Hero@email.com',
 @subject = @mysubject,
 @message = 'Deadlock has occurred. View attachment to see the deadlock info',
 @query = 'select ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''%Deadlock encountered%'' order by Id DESC)',
 @width = 600,
 @attach_results = 'True',
 @no_header = 'True'
 END
 --== Clean up our process by dropping our temporary table. ==--
 DROP TABLE tempdb.dbo.ErrorLog

And for all other version, (2005, 2008, 2008 R2)

--== This is for SQL 2005 and higher. ==--
--== We will create a temporary table to hold the error log detail. ==--
--== Before we create the temporary table, we make sure it does not already exist. ==--
 IF OBJECT_ID('tempdb.dbo.ErrorLog') IS Not Null
 BEGIN
 DROP TABLE tempdb.dbo.ErrorLog
 END
 --== We have checked for the existence of the temporary table and dropped it if it was there. ==--
 --== Now, we can create the table called tempdb.dbo.ErrorLog ==--
CREATE TABLE tempdb.dbo.ErrorLog (Id int IDENTITY (1, 1) NOT NULL, 
logdate DATETIME, procInfo VARCHAR(10), ERRORLOG VARCHAR(MAX))
--== We create a 3 column table to hold the contents of the SQL Server Error log. ==--
--== Then we insert the actual data from the Error log into our newly created table. ==--
 INSERT INTO tempdb.dbo.ErrorLog
 EXEC master.dbo.sp_readerrorlog
--== With our table created and populated, we can now use the info inside of it. ==--
 BEGIN
--== Set a variable to get our instance name. ==--
--== We do this so the email we receive makes more sense. ==--
 declare @servername nvarchar(150)
 set @servername = @@servername
--== We set another variable to create a subject line for the email. ==-- 
 declare @mysubject nvarchar(200)
 set @mysubject = 'Deadlock event notification on server '+@servername+'.'
 --== Now we will prepare and send the email. Change the email address to suite your environment. ==-- 
 EXEC msdb.dbo.sp_send_dbmail @recipients='DBA_Super_Hero@email.com',
 @subject = @mysubject,
 @body = 'Deadlock has occurred. View attachment to see the deadlock info',
 @query = 'select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''%Deadlock encountered%'' order by Id DESC)',
 @query_result_width = 600,
 @attach_query_result_as_file = 1
 END
 --== Clean up our process by dropping our temporary table. ==--
 DROP TABLE tempdb.dbo.ErrorLog

Next Steps

In order to get those to work every time SQL Server encounters a Deadlock, we have to create a SQL Server Agent Job and a SQL Server Agent Alert. The basic approach is to create a Job that is called by the Alert service.

Let’s first create a SQL Agent Job. The job will have no schedule. Note that the script below needs to be edited. If you do not run your SQL Server in mixed mode, you will need to change @owner_login_name=N’sa’ to a user that can run the job. Also, note that the script contains an email address. You will have to enter a valid email address. This would be the email address of the person that will be troubleshooting the Deadlock occurrences. You will create this SQL Agent job on every instance you want to receive Deadlock info for.

SQL Server 2000 Job

USE [msdb]
GO
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Deadlock Job', 
 @enabled=1, 
 @notify_level_eventlog=0, 
 @notify_level_email=0, 
 @notify_level_netsend=0, 
 @notify_level_page=0, 
 @delete_level=0, 
 @description=N'No description available.', 
 @category_name=N'[Uncategorized (Local)]', 
 @owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Deadlock has occurred.', 
 @step_id=1, 
 @cmdexec_success_code=0, 
 @on_success_action=1, 
 @on_success_step_id=0, 
 @on_fail_action=2, 
 @on_fail_step_id=0, 
 @retry_attempts=0, 
 @retry_interval=0, 
 @os_run_priority=0, @subsystem=N'TSQL', 
 @command=N'--== This is for SQL 2000. ==--
--== We will create a temporary table to hold the error log detail. ==--
--== Before we create the temporary table, we make sure it does not already exist. ==--
 IF OBJECT_ID(''tempdb.dbo.ErrorLog'') IS Not Null
 BEGIN
 DROP TABLE tempdb.dbo.ErrorLog
 END
 --== We have checked for the existence of the temporary table and dropped it if it was there. ==--
 --== Now, we can create the table called tempdb.dbo.ErrorLog ==--
 CREATE TABLE tempdb.dbo.ErrorLog (Id int IDENTITY (1, 1) NOT NULL,
 ERRORLOG VARCHAR(4000), ContRow int)
--== We create a 3 column table to hold the contents of the SQL Server Error log. ==--
--== Then we insert the actual data from the Error log into our newly created table. ==--
 INSERT INTO tempdb.dbo.ErrorLog
 EXEC master.dbo.sp_readerrorlog
--== With our table created and populated, we can now use the info inside of it. ==--
 BEGIN
--== Set a variable to get our instance name. ==--
--== We do this so the email we receive makes more sense. ==--
 declare @servername nvarchar(150)
 set @servername = @@servername
--== We set another variable to create a subject line for the email. ==-- 
 declare @mysubject nvarchar(200)
 set @mysubject = ''Deadlock event notification on server ''+@servername+''.''
 --== Now we will prepare and send the email. Change the email address to suite your environment. ==-- 
 exec master.dbo.xp_sendmail @recipients = ''DBA_Super_Hero@email.com'',
 @subject = @mysubject,
 @message = ''Deadlock has occurred. View attachment to see the deadlock info'',
 @query = ''select ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''''%Deadlock encountered%'''' order by Id DESC)'',
 @width = 600,
 @attach_results = ''True'',
 @no_header = ''True''
 END
 --== Clean up our process by dropping our temporary table. ==--
 DROP TABLE tempdb.dbo.ErrorLog
', 
 @database_name=N'master', 
 @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
 IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

SQL Server 2005 and higher Job

Note that the script below needs to be edited. If you do not run your SQL Server in mixed mode, you will need to change @owner_login_name=N’sa’ to a user that can run the job. Also, note that the script contains an email address. You will have to enter a valid email address. This would be the email address of the person that will be troubleshooting the Deadlock occurrences. You will create this SQL Agent job on every instance you want to receive Deadlock info for.

USE [msdb]
GO
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Deadlock Job', 
 @enabled=1, 
 @notify_level_eventlog=0, 
 @notify_level_email=0, 
 @notify_level_netsend=0, 
 @notify_level_page=0, 
 @delete_level=0, 
 @description=N'No description available.', 
 @category_name=N'[Uncategorized (Local)]', 
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Deadlock has occurred.', 
 @step_id=1, 
 @cmdexec_success_code=0, 
 @on_success_action=1, 
 @on_success_step_id=0, 
 @on_fail_action=2, 
 @on_fail_step_id=0, 
 @retry_attempts=0, 
 @retry_interval=0, 
 @os_run_priority=0, @subsystem=N'TSQL', 
 @command=N'--== This is for SQL 2005 and higher. ==--
--== We will create a temporary table to hold the error log detail. ==--
--== Before we create the temporary table, we make sure it does not already exist. ==--
 IF OBJECT_ID(''tempdb.dbo.ErrorLog'') IS Not Null
 BEGIN
 DROP TABLE tempdb.dbo.ErrorLog
 END
 --== We have checked for the existence of the temporary table and dropped it if it was there. ==--
 --== Now, we can create the table called tempdb.dbo.ErrorLog ==--
CREATE TABLE tempdb.dbo.ErrorLog (Id int IDENTITY (1, 1) NOT NULL, 
logdate DATETIME, procInfo VARCHAR(10), ERRORLOG VARCHAR(MAX))
--== We create a 3 column table to hold the contents of the SQL Server Error log. ==--
--== Then we insert the actual data from the Error log into our newly created table. ==--
 INSERT INTO tempdb.dbo.ErrorLog
 EXEC master.dbo.sp_readerrorlog
--== With our table created and populated, we can now use the info inside of it. ==--
 BEGIN
--== Set a variable to get our instance name. ==--
--== We do this so the email we receive makes more sense. ==--
 declare @servername nvarchar(150)
 set @servername = @@servername
--== We set another variable to create a subject line for the email. ==-- 
 declare @mysubject nvarchar(200)
 set @mysubject = ''Deadlock event notification on server ''+@servername+''.''
 --== Now we will prepare and send the email. Change the email address to suite your environment. ==-- 
 EXEC msdb.dbo.sp_send_dbmail @recipients=''DBA_Super_Hero@email.com'',
 @subject = @mysubject,
 @body = ''Deadlock has occurred. View attachment to see the deadlock info'',
 @query = ''select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''''%Deadlock encountered%'''' order by Id DESC)'',
 @query_result_width = 600,
 @attach_query_result_as_file = 1
 END
 --== Clean up our process by dropping our temporary table. ==--
 DROP TABLE tempdb.dbo.ErrorLog
', 
 @database_name=N'master', 
 @flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
 IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO

Final step

Now we have to create a SQL Agent alert to call the job we created. Open SSMS and log into the instance we are capturing Deadlock information for and expand the SQL Server Agent. Right click on the word Alerts and choose “New Alert…”

On the General page it should look like this;

On the Response page it should look like this:

On the Options page it should look like this:

SQLDeadlockpic3

That’s all there is to it. The next time a Deadlock occurs on the instance you are monitoring you will you receive an email. The email you receive will have an attachment that will actually tell you what the deadlock was.

File management from inside SQL code

Standard

Writing a log, dumping data, looking for file to be used in Bulk insert…

There are many situations when you would want to have access to files from inside your SQL code on Microsoft SQL Server.

Did you know that you actually can do this? No? Check below for code snippets to perform various operations on files. It is presented in form of the functions but you are actually not limited to that

Prerequisites and assumptions

  1. Your script should have sufficient rights to perform required access to files (not necessarily local).
  2. Scripting.FileSystemObject should be present at your SQL Server location and accessible.

Check if file exists

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
CREATE FUNCTION [dbo].[ufn_IsFileExists] (
@FilePath VARCHAR(255)
)
RETURNS INT
AS
BEGIN
DECLARE
  @objFileSystem int,
  @hr int,
  @i int
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem out
IF @HR = 0
BEGIN
  EXEC sp_OAMethod @objFileSystem, 'FileExists', @i out, @FilePath
  EXEC sp_OADestroy @objFileSystem
END
ELSE
BEGIN
  SET @i = -1
END
RETURN @i
END

Write string into file

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
CREATE FUNCTION [dbo].[ufn_WriteStringToFile] (
  @CreateFile int,
  @FilePath varchar(500),
  @String varchar(4000) )
RETURNS varchar(200)
AS
BEGIN
DECLARE
  @objFileSystem int,
  @objTextStream int,
  @objErrorObject int,
  @strErrorMsg varchar(1000),
  @Command varchar(1000),
  @HR int,
  @fileAndPath varchar(80)
SET @strErrorMsg = 'opening the File System Object'
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUT
IF @HR = 0
BEGIN
  SET @objErrorObject = @objFileSystem
  IF (@CreateFile = 1)
  BEGIN
SET @strErrorMsg= 'Creating file "' + @FilePath + '"'
EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 2, True
  END
  ELSE
  BEGIN
  SET @strErrorMsg = 'Opening file "' + @FilePath + '"'
  EXEC @hr = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 8, True
  END
END
IF @HR = 0
BEGIN
  SET @objErrorObject = @objTextStream
  SET @strErrorMsg = 'Writing to the file "' + @FilePath + '"'
  EXEC @hr = sp_OAMethod @objTextStream, 'WriteLine', Null, @String
END
IF @HR=0
BEGIN
  SET @objErrorObject = @objTextStream
  SET @strErrorMsg = 'Closing the file "' + @FileAndPath + '"'
  EXEC @hr = sp_OAMethod @objTextStream, 'Close'
END
IF @HR <> 0
BEGIN
  DECLARE
  @Source varchar(255),
  @Description varchar(255),
  @Helpfile varchar(255),
  @HelpID int
EXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT
  SET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')
END
EXEC sp_OADestroy @objTextStream
RETURN @strErrorMsg
END

Read File As Table

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
CREATE FUNCTION [dbo].[ufn_ReadFileAsTable] (
@FilePath VARCHAR(255)
)
RETURNS @File TABLE ([LineNo] int identity(1,1), [Line] varchar(8000))
AS
BEGIN
DECLARE
@objFileSystem int,
@objTextStream int,
@objErrorObject int,
@strErrorMsg varchar(1000),
  @hr int,
  @String VARCHAR(8000),
@YesOrNo INT
SET @strErrorMsg = 'opening the File System Object'
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @objFileSystem OUT
IF @HR=0
BEGIN
SET @objErrorObject = @objFileSystem
SET @strErrorMsg = 'Opening file "' + @FilePath + '"'
--Open for reading, FormatASCII
EXEC @HR = sp_OAMethod @objFileSystem, 'OpenTextFile', @objTextStream OUT, @FilePath, 1, False, 0
END
WHILE @HR = 0
BEGIN
  IF @HR=0
  BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Check if there is more to read in "' + @FilePath + '"'
EXEC @HR = sp_OAGetProperty @objTextStream, 'AtEndOfStream', @YesOrNo OUTPUT
IF @YesOrNo <> 0 BREAK
  END
  IF @HR=0
  BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Reading from the output file "' + @FilePath + '"'
EXEC @HR = sp_OAMethod @objTextStream, 'Readline', @String OUTPUT
    INSERT INTO @file(line) SELECT @String
  END
END
IF @HR=0
BEGIN
SET @objErrorObject = @objTextStream
SET @strErrorMsg = 'Closing the output file "' + @FilePath + '"'
EXEC @HR = sp_OAMethod @objTextStream, 'Close'
END
IF @hr <> 0
BEGIN
  DECLARE
@Source varchar(255),
@Description varchar(255),
@Helpfile varchar(255),
@HelpID int
EXEC sp_OAGetErrorInfo @objErrorObject, @Source OUTPUT, @Description OUTPUT, @Helpfile OUTPUT, @HelpID OUTPUT
SET @strErrorMsg = 'Error: ' + coalesce(@strErrorMsg, 'Unknown') + ', ' + coalesce(@Description, '')
INSERT INTO @File(line) select @strErrorMsg
END
EXEC sp_OADestroy @objTextStream
-- Fill the table variable with the rows for your result set
RETURN
END

Enjoy.

https://blog.dragonsoft.us/2008/02/15/sql-manage-files-from-inside-sql-code/

Script to Monitor SQL Server Memory Usage

Standard

udayarumilli.com/script-to-monitor-sql-server-memory-usage/

Introduction:

This post will takes you through the T-SQL Script to monitor SQL Server Memory Usage. In previous blog post we have explained the parameters involved in understanding sql server memory usage. There are total 7 scripts to monitor SQL Server Memory Usage.

  • Buffer Pool Usage
  • System Memory Information
  • SQL Server Process Memory Usage Information
  • Buffer Usage by Database
  • Object Wise Buffer Usage
  • Top 25 Costliest Stored Procedures – Logical Reads
  • Top Performance Counters

Script to Monitor SQL Server Memory Usage: Buffer Pool Usage

Results:

BPool_Committed_MB: Actual memory committed/used by the process (SQL Server).

BPool_Commit_Tgt_MB: Actual memory SQL Server tried to consume.

BPool_Visible_MB: Number of 8-KB buffers in the buffer pool that are directly accessible in the process virtual address space (SQL Server VAS).

Analysis:

BPool_Commit_Tgt_MB > BPool_Committed_MB: SQL Server Memory Manager tries to obtain additional memory

BPool_Commit_Tgt_MB < BPool_Committed_MB: SQL Server Memory Manager tries to shrink the amount of memory committed

If the value of BPool_Visible_MB is too low: We might receive out of memory errors or memory dump will be created.

 

Script to Monitor SQL Server Memory Usage: System Memory Information

Results:

total_physical_memory_mb: Actual Physical Memory installed in OS

available_physical_memory_mb: Available Physical Memory

total_page_file_mb: Pagefile size on OS

available_page_file_mb: Available page file size

Percentage_Used: Physical Memory Percentage used

system_memory_state_desc: Memory current Health status

Analysis:

available_physical_memory_mb: Should be some positive sign based on total physical memory

available_page_file_mb: Should be some positive sign based on your total page file

Percentage_Used: 100% for a long time indicates a memory pressure

system_memory_state_desc: should be Available physical memory is high / steady

 

Script to Monitor SQL Server Memory Usage: SQL Server Process Memory Usage

Results:

physical_memory_in_use: Indicates the process working set in KB, as reported by operating system, as well as tracked allocations by using large page APIs

locked_page_allocations: Specifies memory pages locked in memory

virtual_address_space_committed: Indicates the amount of reserved virtual address space that has been committed or mapped to physical pages.

available_commit_limit: Indicates the amount of memory that is available to be committed by the process (SQL server)

page_fault_count: Indicates the number of page faults that are incurred by the SQL Server process

Analysis:

physical_memory_in_use: We can’t figure out the exact amount of physical memory using by sqlservr.exe using task manager but this column showcase the actual amount of physical memory using by SQL Server.

locked_page_allocations: If this is > 0 means Locked Pages is enabled for SQL Server which is one of the best practice

available_commit_limit: This indciates the available amount of memory that can be committed by the process sqlservr.exe

page_fault_count: Pages fetching from the page file on the hard disk instead of from physical memory. Consistently high number of hard faults per second represents Memory pressure.

 

Script to Monitor SQL Server Memory Usage: Database Wise Buffer Usage

Results:

db_name: Name of the database in the given SQL server Instance

db_buffer_pages: Total number of corresponding database pages that are in buffer pool

db_buffer_Used_MB: Database wise Buffer size used in MB

db_buffer_Free_MB: Database wise Buffer Size Free (sum of free space on all pages) in MB.

db_buffer_percent: Database wise percentage of Buffer Pool usage

Analysis:

We can quickly find out the top databases which are consuming more Memory / Buffer Pool from the given SQL server Instance

 

Script to Monitor SQL Server Memory Usage: Object Wise Buffer Usage

Results:

Object: Name of the Object

Type: Type of the object Ex: USER_TABLE

Index: Name of the Index

Index_Type: Type of the Index “Clustered / Non Clustered / HEAP” etc

buffer_pages: Object wise number of pages is in buffer pool

buffer_mb: Object wise buffer usage in MB

Analysis:

From the previous script we can get the top databases using memory. This script helps you out in finding the top objects that are using the buffer pool. Top objects will tell you the objects which are using the major portion of the buffer pool.If you find anything suspicious then you can dig into it.

Script to Monitor SQL Server Memory Usage: Top 25 Costliest Stored Procedures by Logical Reads

Results:

SP Name: Stored Procedure Name

TotalLogicalReads: Total Number of Logical Reads since this stored procedure was last compiled

AvgLogicalReads: Average Number of Logical Reads since this stored procedure was last compiled

execution_count: Number of Times SP got executed since it was compiled

total_elapsed_time: Total elapsed time for this proc since last time compiled

avg_elapsed_time: Average elapsed time

cached_time: Time at which the stored procedure was added to the cache.

Analysis:

  • This helps you find the most expensive cached stored procedures from a memory perspective
  • You should look at this if you see signs of memory pressure
  • More number of logical reads means you need to check execution plan to find the bottleneck

 

Script to Monitor SQL Server Memory Usage: Top Performance Counters – Memory

Results:

Total Server Memory: Shows how much memory SQL Server is using. The primary use of SQL Server’s memory is for the buffer pool, but some memory is also used for storing query plans and keeping track of user process information.

Target Server Memory: This value shows how much memory SQL Server attempts to acquire. If you haven’t configured a max server memory value for SQL Server, the target amount of memory will be about 5MB less than the total available system memory.

Connection Memory (GB): The Connection Memory specifies the total amount of dynamic memory the server is using for maintaining connections

Lock Memory (GB): Shows the total amount of memory the server is using for locks

SQL Cache Memory: Total memory reserved for dynamic SQL statements.

Optimizer Memory: Memory reserved for query optimization.

Granted Workspace Memory: Total amount of memory currently granted to executing processes such as hash, sort, bulk copy, and index creation operations.

Cursor memory usage: Memory using for cursors

Free pages: Amount of free space in pages which are commited but not currently using by SQL Server

Reserved Pages: Shows the number of buffer pool reserved pages.

Stolen pages (MB): Memory used by SQL Server but not for Database pages.It is used for sorting or hashing operations, or “as a generic memory store for allocations to store internal data structures such as locks, transaction context, and connection information.

Cache Pages: Number of 8KB pages in cache.

Page life expectancy: Average how long each data page is staying in buffer cache before being flushed out to make room for other pages

Free list stalls / sec: Number of times a request for a “free” page had to wait for one to become available.

Checkpoint Pages/sec: Checkpoint Pages/sec shows the number of pages that are moved from buffer to disk per second during a checkpoint process

Lazy writes / sec: How many times per second lazy writer has to flush dirty pages out of the buffer cache instead of waiting on a checkpoint.

Memory Grants Outstanding: Number of processes that have successfully acquired workspace memory grant.

Memory Grants Pending: Number of processes waiting on a workspace memory grant.

process_physical_memory_low: Process is responding to low physical memory notification

process_virtual_memory_low: Indicates that low virtual memory condition has been detected

Min Server Memory: Minimum amount of memory SQL Server should acquire

Max Server Memory: Maximum memory that SQL Server can acquire from OS

Buffer cache hit ratio: Percentage of pages that were found in the buffer pool without having to incur a read from disk.

Analysis:

Total Server Memory is almost same as Target Server Memory: Good Health

Total Server Memory is much smaller than Target Server Memory: There is a Memory Pressure or Max Server Memory is set to too low.

Connection Memory: When high, check the number of user connections and make sure it’s under expected value as per your business

Optimizer Memory: Ideally, the value should remain relatively static. If this isn’t the case you might be using dynamic SQL execution excessively.

Higher the value for Stolen Pages: Find the costly queries / procs and tune them

Higher the value for Checkpoint Pages/sec: Problem with I/O, Do not depend on Automatic Checkpoints and use In-direct checkpoints.

Page life expectancy: Usually 300 to 400 sec for each 4 GB of memory. Lesser the value means memory pressure

Free list stalls / sec: High value indicates that the server could use additional memory.

Memory Grants Outstanding: Higher value indicates peak user activity

Memory Grants Pending: Higher value indicates SQL Server need more memory

process_physical_memory_low & process_virtual_memory_low: Both are equals to 0 means no internal memory pressure

Min Server Memory: If it is 0 means default value didnt get changed, it’ll always be better to have a minimum amount of memory allocated to SQL Server

Max Server Memory: If it is default to 2147483647, change the value with the correct amount of memory that you can allow SQL Server to utilize.

Buffer cache hit ratio: This ratio should be in between 95 and 100. Lesser value indicates memory pressure

Script to Monitor SQL Server Memory Usage: DBCC MEMORYSTATUS

Finally DBCC MemoryStatus:

  • It gives as much as memory usage information based on object wise / component wise.
  • First table gives us the complete details of server and process memory usage details and memory alert indicators.
  • We can also get memory usage by buffer cache, Service Broker, Temp tables, Procedure Cache, Full Text, XML, Memory Pool Manager, Audit Buffer, SQLCLR, Optimizer, SQLUtilities, Connection Pool etc.

Summary:

These Scripts will help you in understanding the current memory usage by SQL Server. To maintain a healthy database management system:

  • Monitor the system for few business days in peak hours and fix the baselines
  • Identify the correct required configurations for your database server and make the required changes
  • Identify top 10 queries / procedures based on Memory and CPU usage
  • Fine tune these top 10 queries / procedures

Note:

These scripts are tested on SQL Server 2008, 2008 R2, 2012 and 2014. As we always suggests please test these scripts on Dev/Test environment before using them on production systems.

References:

Would like to thank famous MVPs / MCM / bloggers (Glenn Berry, Brent Ozar, Jonathan Kehayias, John Sansom) for the tremendous explanation on sql server internals. Their articles are very informative and helpful in understanding SQL Server internals.

Also Check:

SQL Server Memory Usage

SQL Server CPU Utilization

SQL Server 2000 system tables and their equivalent DMV in SQL Server 2005

Standard

Those who have been working with SQL Server administration for a while now undoubtedly have at times referred to the old SQL Server system tables in order to automate some processes, or document their tables by for example combining the sysobjects and syscolumns tables. As per SQL Server 2005 and onwards, Microsoft added a number of Dynamic Management Views (DMV) that take simplify all kinds of management tasks.

List of SQL Server 2000 system tables and their 2005 equivalent management views, as well as a brief description what kind of information to find in the views.

Dynamic Management Views existing in the Master database

SQL Server 2000 SQL Server2005 Description
sysaltfiles sys.master_files Contains a row per file of a database as stored in the master database.
syscacheobjects sys.dm_exec_cached_plans Returns a row for each query plan that is cached by SQL Server for faster query execution.
sys.dm_exec_plan_attributes Returns one row per plan attribute for the plan specified by the plan handle.
sys.dm_exec_sql_text Returns the text of the SQL batch that is identified by the specified sql_handle.
sys.dm_exec_cached_plan_dependent_objects Returns a row for each Transact-SQL execution plan, common language runtime (CLR) execution plan, and cursor associated with a plan.
syscharsets sys.syscharsets Contains one row for each character set and sort order defined for use by the SQL Server Database Engine.
sysconfigures sys.configurations Contains a row per server-wide configuration option value in the system.
syscurconfigs sys.configurations Contains a row per server-wide configuration option value in the system.
sysdatabases sys.databases Contains one row per database in the instance of Microsoft SQL Server.
sysdevices sys.backup_devices Contains a row for each backup-device registered by using sp_addumpdevice or created in SQL Server Management Studio.
syslanguages sys.syslanguages Contains one row for each language present in the instance of SQL Server.
syslockinfo sys.dm_tran_locks Returns information about currently active lock manager resources
syslocks[ sys.dm_tran_locks Returns information about currently active lock manager resources
syslogins sys.server_principals Contains a row for every server-level principal.
sys.sql_logins Returns one row for every SQL login.
sysmessages sys.messages Contains a row for each message_id or language_id of the error messages in the system, for both system-defined and user-defined messages.
sysoledbusers sys.linked_logins Returns a row per linked-server-login mapping, for use by RPC and distributed queries from local server to the corresponding linked server.
sysopentapes sys.dm_io_backup_tapes Returns the list of tape devices and the status of mount requests for backups.
sysperfinfo sys.dm_os_performance_counters Returns a row per performance counter maintained by the server.
sysprocesses sys.dm_exec_connections Returns information about the connections established to this instance of SQL Server and the details of each connection.
sys.dm_exec_sessions Returns one row per authenticated session on SQL Server.
sys.dm_exec_requests Returns information about each request that is executing within SQL Server.
sysremotelogins sys.remote_logins Returns a row per remote-login mapping. This catalog view is used to map incoming local logins that claim to be coming from a corresponding server to an actual local login.
sysservers sys.servers Contains a row per linked or remote server registered, and a row for the local server that has server_id = 0.

Dynamic Management Views existing in every database.

SQL Server 2000 SQL Server 2005 Description
fn_virtualfilestats sys.dm_io_virtual_file_stats Returns I/O statistics for data and log files.
syscolumns sys.columns Returns a row for each column of an object that has columns, such as views or tables.
syscomments sys.sql_modules Returnsa row for each object that is an SQL language-defined module. Objectsof type ‘P’, ‘RF’, ‘V’, ‘TR’, ‘FN’, ‘IF’, ‘TF’, and ‘R’ have an associated SQL module.
sysconstraints sys.check_constraints Contains a row for each object that is a CHECK constraint, with sys.objects.type = ‘C’.
sys.default_constraints Contains a row for each object that is a default definition (created as part of a CREATE TABLE or ALTER TABLE statement instead of a CREATE DEFAULT statement), with sys.objects.type = ‘D’.
sys.key_constraints Contains a row for each object that is a primary key or unique constraint. Includes sys.objects.type ‘PK’ and ‘UQ’.
sys.foreign_keys Contains a row per object that is a FOREIGN KEY constraint, with sys.object.type = ‘F’.
sysdepends sys.sql_expression_dependencies Contains one row for each by-name dependency on a user-defined entity in the current database. A dependency between two entities is created when one entity, called the referenced entity, appears by name in a persisted SQL expression of another entity, called the referencing entity.
sysfilegroups sys.filegroups Contains a row for each data space that is a filegroup.
sysfiles sys.database_files Contains a row per file of a database as stored in the database itself. This is a per-database view.
sysforeignkeys sys.foreign_key_columns Contains a row for each column, or set of columns, that comprise a foreign key.
sysindexes sys.indexes Contains a row per index or heap of a tabular object, such as a table, view, or table-valued function.
sys.partitions Contains a row for each partition of all the tables and most types of indexes in the database. Special index types like Fulltext, Spatial, and XML are not included in this view. All tables and indexes in SQL Server contain at least one partition, whether or not they are explicitly partitioned.
sys.allocation_units Contains a row for each allocation unit in the database.
sys.dm_db_partition_stats Returns page and row-count information for every partition in the current database.
sysindexkeys sys.index_columns Contains one row per column that is part of a sys.indexes index or unordered table (heap).
sysmembers sys.database_role_members Returns one row for each member of each database role.
sysobjects sys.objects Contains a row for each user-defined, schema-scoped object that is created within a database.
syspermissions sys.database_permissions Returns a row for every permission or column-exception permission in the database. For columns, there is a row for every permission that is different from the corresponding object-level permission. If the column permission is the same as the corresponding object permission, there will be no row for it and the actual permission used will be that of the object.
sys.server_permissions Returns one row for each server-level permission.
sysprotects sys.database_permissions Returns a row for every permission or column-exception permission in the database. For columns, there is a row for every permission that is different from the corresponding object-level permission. If the column permission is the same as the corresponding object permission, there will be no row for it and the actual permission used will be that of the object.
sys.server_permissions Returns one row for each server-level permission.
sysreferences sys.foreign_keys Contains a row per object that is a FOREIGN KEY constraint, with sys.object.type = ‘F’.
systypes sys.types Contains a row for each system and user-defined type.
sysusers sys.database_principals Returns a row for each principal in a database.
sysfulltextcatalogs sys.fulltext_catalogs Contains a row for each full-text catalog.
原文:

Script to retrieve SQL Server database backup history and no backups

Standard

原文:Script to retrieve SQL Server database backup history and no backups

Problem

There is a multitude of data to be mined from within the Microsoft SQL Server system views. This data is used to present information back to the end user of the SQL Server Management Studio (SSMS) and all third party management tools that are available for SQL Server Professionals. Be it database backup information, file statistics, indexing information, or one of the thousands of other metrics that the instance maintains, this data is readily available for direct querying and assimilation into your “home-grown” monitoring solutions as well. This tip focuses on that first metric: database backup information. Where it resides, how it is structured, and what data is available to be mined.

Solution

The msdb system database is the primary repository for storage of SQL Agent, backup, Service Broker, Database Mail, Log Shipping, restore, and maintenance plan metadata. We will be focusing on the handful of system views associated with database backups for this tip:

  • dbo.backupset: provides information concerning the most-granular details of the backup process
  • dbo.backupmediafamily: provides metadata for the physical backup files as they relate to backup sets
  • dbo.backupfile: this system view provides the most-granular information for the physical backup files

Based upon these tables, we can create a variety of queries to collect a detailed insight into the status of backups for the databases in any given SQL Server instance.


Database Backups for all databases For Previous Week

--------------------------------------------------------------------------------- 
--Database Backups for all databases For Previous Week 
--------------------------------------------------------------------------------- 
SELECT 
CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
msdb.dbo.backupset.database_name, 
msdb.dbo.backupset.backup_start_date, 
msdb.dbo.backupset.backup_finish_date, 
msdb.dbo.backupset.expiration_date, 
CASE msdb..backupset.type 
WHEN 'D' THEN 'Database' 
WHEN 'L' THEN 'Log' 
END AS backup_type, 
msdb.dbo.backupset.backup_size, 
msdb.dbo.backupmediafamily.logical_device_name, 
msdb.dbo.backupmediafamily.physical_device_name, 
msdb.dbo.backupset.name AS backupset_name, 
msdb.dbo.backupset.description 
FROM msdb.dbo.backupmediafamily 
INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id 
WHERE (CONVERT(datetime, msdb.dbo.backupset.backup_start_date, 102) >= GETDATE() - 7) 
ORDER BY 
msdb.dbo.backupset.database_name, 
msdb.dbo.backupset.backup_finish_date

Note: for readability the output was split into two screenshots.


Most Recent Database Backup for Each Database

------------------------------------------------------------------------------------------- 
--Most Recent Database Backup for Each Database 
------------------------------------------------------------------------------------------- 
SELECT  
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
   msdb.dbo.backupset.database_name,  
   MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date 
FROM   msdb.dbo.backupmediafamily  
   INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id  
WHERE  msdb..backupset.type = 'D' 
GROUP BY 
   msdb.dbo.backupset.database_name  
ORDER BY  
   msdb.dbo.backupset.database_name 

 


Most Recent Database Backup for Each Database – Detailed

You can join the two result sets together by using the following query in order to return more detailed information about the last database backup for each database. The LEFT JOIN allows you to match up grouped data with the detailed data from the previous query without having to include the fields you do not wish to group on in the query itself.

------------------------------------------------------------------------------------------- 
--Most Recent Database Backup for Each Database - Detailed 
------------------------------------------------------------------------------------------- 
SELECT  
   A.[Server],  
   A.last_db_backup_date,  
   B.backup_start_date,  
   B.expiration_date, 
   B.backup_size,  
   B.logical_device_name,  
   B.physical_device_name,   
   B.backupset_name, 
   B.description 
FROM 
   ( 
   SELECT   
       CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
       msdb.dbo.backupset.database_name,  
       MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date 
   FROM    msdb.dbo.backupmediafamily  
       INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id  
   WHERE   msdb..backupset.type = 'D' 
   GROUP BY 
       msdb.dbo.backupset.database_name  
   ) AS A 
    
   LEFT JOIN  

   ( 
   SELECT   
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
   msdb.dbo.backupset.database_name,  
   msdb.dbo.backupset.backup_start_date,  
   msdb.dbo.backupset.backup_finish_date, 
   msdb.dbo.backupset.expiration_date, 
   msdb.dbo.backupset.backup_size,  
   msdb.dbo.backupmediafamily.logical_device_name,  
   msdb.dbo.backupmediafamily.physical_device_name,   
   msdb.dbo.backupset.name AS backupset_name, 
   msdb.dbo.backupset.description 
FROM   msdb.dbo.backupmediafamily  
   INNER JOIN msdb.dbo.backupset ON msdb.dbo.backupmediafamily.media_set_id = msdb.dbo.backupset.media_set_id  
WHERE  msdb..backupset.type = 'D' 
   ) AS B 
   ON A.[server] = B.[server] AND A.[database_name] = B.[database_name] AND A.[last_db_backup_date] = B.[backup_finish_date] 
ORDER BY  
   A.database_name

Note: for readability the output was split into two screenshots.


Databases Missing a Data (aka Full) Back-Up Within Past 24 Hours

At this point we’ve seen how to look at the history for databases that have been backed up. While this information is important, there is an aspect to backup metadata that is slightly more important – which of the databases you administer have not been getting backed up. The following query provides you with that information (with some caveats.)

------------------------------------------------------------------------------------------- 
--Databases Missing a Data (aka Full) Back-Up Within Past 24 Hours 
------------------------------------------------------------------------------------------- 
--Databases with data backup over 24 hours old 
SELECT 
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server, 
   msdb.dbo.backupset.database_name, 
   MAX(msdb.dbo.backupset.backup_finish_date) AS last_db_backup_date, 
   DATEDIFF(hh, MAX(msdb.dbo.backupset.backup_finish_date), GETDATE()) AS [Backup Age (Hours)] 
FROM    msdb.dbo.backupset 
WHERE     msdb.dbo.backupset.type = 'D'  
GROUP BY msdb.dbo.backupset.database_name 
HAVING      (MAX(msdb.dbo.backupset.backup_finish_date) < DATEADD(hh, - 24, GETDATE()))  

UNION  

--Databases without any backup history 
SELECT      
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,  
   master.dbo.sysdatabases.NAME AS database_name,  
   NULL AS [Last Data Backup Date],  
   9999 AS [Backup Age (Hours)]  
FROM 
   master.dbo.sysdatabases LEFT JOIN msdb.dbo.backupset 
       ON master.dbo.sysdatabases.name  = msdb.dbo.backupset.database_name 
WHERE msdb.dbo.backupset.database_name IS NULL AND master.dbo.sysdatabases.name <> 'tempdb' 
ORDER BY  
   msdb.dbo.backupset.database_name

 

Now let me explain those caveats, and this query. The first part of the query returns all records where the last database (full) backup is older than 24 hours from the current system date. This data is then combined via the UNION statement to the second portion of the query. That second statement returns information on all databases that have no backup history. I’ve taken the liberty of singling tempdb out from the result set since you do not back up that system database. It is recreated each time the SQL Server services are restarted. That is caveat #1. Caveat #2 is the arbitrary value I’ve assigned to the aging value for databases without any backup history. I’ve set that value at 9999 hours because in my environment I want to place a higher emphasis on those databases that have never been backed up.

SQL Server Management Studio Tips

Standard

原文地址:http://www.sqlservercentral.com/articles/SSMS/160267/?utm_source=SSC&utm_medium=pubemail

By Konstantin Taranov, 2017/08/14

Most tips works for SSMS higher 2008 but some of them only for SSMS 2016 and above.

Great thanks to:

  • Kendra Little
  • Slava Murygin
  • Mike Milligan
  • Kenneth Fisher
  • William Durkin
  • John Morehouse
  • Phil Factor
  • Klaus Aschenbrenner
  • Latish Sehgal
  • Arvind Shyamsundar
  • SQLMatters
  • MSSQLTips
  • Anthony Zanevsky, Andrew Zanevsky and Katrin Zanevsky
  • Andy Mallon
  • Aaron Bertrand

Import and Export Settings

Tools > Options > Environment > Import and Export Settings

You can configure so many settings in SSMS and then export it and use on all your computers. Below link provide detailed instruction and awesome Dark theme configuration: Making SSMS Pretty: My Dark Theme

Also you can create shared team settings file and use it from network location. Detailed information you can find in this article Symbolic Links for Sharing Template Files or “How I Broke Management Studio with Symbolic Links”

Import and Export Settings

SSMS Shortcuts

All 957 shortcuts you can find here

Most useful are:

Shortcut Description
Ctrl + U Change Selected Database
Ctrl + R Toggle Results Pane
Ctrl + Space Activate Autocomplete
Ctrl + Shift + V Cycle through clipboard ring
Ctrl + ] Navigate to matching parenthesis
Ctrl + – Navigate to last cursor location
Ctrl + Shift + – Navigate forward to cursor location
Ctrl + K, Ctrl + C Comments selected text
Ctrl + K, Ctrl + U Uncomments selected text
Ctrl + K, Ctrl + K Toggle Bookmark
Ctrl + K, Ctrl + N Go to Next Bookmark
Ctrl + L Display Estimated Query Execution plan
Shift + Alt + Enter View Code Editor in Full Screen
Ctrl + I Quick Search
Ctrl + F4 Close the current MDI child window
Ctrl + F5 Parse query to check for errors
Shift + F10 Simulate right mouse button
Ctrl + Alt + T Display Template Explorer
Ctrl + Shift + M Specify values for template parameters

Keyboard Shortcuts for Favorite Stored Procedures

Tools > Options > Environment > Keyboard > Query Shortcuts

Keyboard Shortcuts for Favorite Stored Procedures

3 Shortcuts can not be changed: Alt + F1Ctrl + 1 and Ctrl + 2. For another 9 shortcuts my recommendation awesome open source Brent Ozar teams procedures and with some limitations Adam Machanic sp_WhoIsActive:

Query Shortcut Stored Procedure
Alt + F1 sp_help
Ctrl + F1 sp_WhoIsActive
Ctrl + 1 sp_who
Ctrl + 2 sp_lock
Ctrl + 3 sp_Blitz
Ctrl + 4 sp_BlitzCache
Ctrl + 5 sp_BlitzWho
Ctrl + 6 sp_BlitzQueryStore
Ctrl + 7 sp_BlitzFirst
Ctrl + 8 usp_BulkUpload
Ctrl + 9 sp_BlitzTrace
Ctrl + 0 sp_foreachdb

Also recommended:

Script any object with data

Right click on database name > Tasks > Generate Scripts …

Script any object with data

Selecting a block of text using the ALT Key

By holding down the ALT key as you select a block of text you can control the width of the selection region as well as the number of rows. Also you can activate multi line mode with Shift + Alt keys and using keyboard arrows to format multi line code.

Script Table and Column Names by Dragging from Object Explorer

Save keystrokes by dragging Drag the Columns folder for a table in to auto-type all column names in the table in a single line.

  • Warning: this doesn’t include [brackets] around the column names, so if your columns contain spaces or special characters at the beginning, this shortcut isn’t for you
  • Dragging the table name over will auto-type the schema and table name, with brackets.

Disable Copy of Empty Text

  • Select a block of text to copy;
  • Move the cursor the place where you want to paste the code;
  • Accidentally press Ctrl+C again instead of Ctrl+V;
  • Block of copied text is replaced by an empty block;

This behavior can be disabled in SSMS: go to Tools > Options > Text Editor > All Languages > General > 'Apply Cut or Copy Commands to blank lines when there is no selection' and uncheck the checkbox.

Disable Copy of Empty Text

Client Statistics

When you enable that option for your session, SQL Server Management Studio will give you more information about the client side processing of your query.

The Network Statistics shows you the following information:

  • Number of Server Roundtrips
  • TDS Packets sent from Client
  • TDS Packets received from Server
  • Bytes sent from Client
  • Bytes received from Server

The Time Statistics additionally shows you the following information:

  • Client Processing Time
  • Total Execution Time
  • Wait Time on Server Replies

Configure Object Explorer to Script Compression and Partition Schemes for Indexes

Is this index compressed or partitioned?

By default, you wouldn’t know just by scripting out the index from Object Explorer. If you script out indexes this way to check them into source code, or to tweak the definition slightly, this can lead you to make mistakes.

You can make sure you’re aware when indexes have compression or are partitioned by changing your scripting settings:

  • Click Tools – > Options -> SQL Server Object Explorer -> Scripting
  • Scroll down in the right pane of options and set both of these to True
    • Script Data Compression Options
    • Script Partition Schemes
  • Click OK

Using GO X to Execute a Batch or Statement Multiple Times

The GO command marks the end of a batch of statements that should be sent to SQL Server for processing, and then compiled into a single execution plan. By specifying a number after the ‘GO’ the batch can be run specified number of times. This can be useful if, for instance, you want to create test data by running an insert statement a number of times. Note that this is not a Transact SQL statement and will only work in Management Studio (and also SQLCMD or OSQL). For instance the following SQL can be run in SSMS :

CREATE TABLE TestData(ID INT IDENTITY (1,1), CreatedDate DATETIME)
GO

INSERT INTO TestData(CreatedDate) SELECT GetDate()
GO 10

This will run the insert statement 10 times and therefore insert 10 rows into the TestData table. In this case this is a simpler alternative than creating a cursor or while loop.

SSMS Template Replacement

One under-used feature of Management Studio is the template replacement feature. SSMS comes with a library of templates, but you can also make your own templates for reusable scripts.

In your saved .sql script, just use the magic incantation to denote the parameters for replacement. The format is simple: <label, datatype, default value>

Then, when you open the .sql script, you hit CTRL + Shift + M, and SSMS will give you a pop-up to enter your replacement values.

Color coding of connections

SQL Server Management Studio has the capability of coloring the bar at the bottom of each query window, with the color dependent on which server is connected. This can be useful in order to provide a visual check of the server that a query is to be run against, for instance to color code production instances as red, development as green and amber as test. This can also be used in conjunction with Registered Servers and CMS (Central Management Server). To add a color bar when connecting to the server click on the Options button in the Connect to Database Engine window and then select the Connection Properties window. Select the check box towards the bottom of the window and use the ‘Select…’ button to choose a color.

SQLCMD mode

Switching on SQLCMD mode enables a number of useful extra scripting style commands in SSMS. In particular you can use it to change to the connection credentials within the query window, so that you can run a query against multiple servers from the same query window. There are more details of how to do this here: Changing the SQL Server connection within an SSMS Query Windows using SQLCMD Mode

Script multiple objects using the Object Explorer Details Windows

Individual database objects, such as a table or stored procedure, can be scripted within SSMS by right clicking on the object within Object Explorer and selecting the appropriate item in the drop down menu. However if you have a lot of objects to script that can quickly become time consuming. Fortunately it’s possible to select multiple objects and script them up all together in a single query window. To do this just open the Object Explorer Details window from the View menu (or press the F7 key). If you want to script up multiple (or all) tables, select the Tables item under the relevant database in Object Explorer. A list of all tables appears in the Object Explorer Details window. Select the tables you want to script (using the Control key if necessary) and then right click and select which script option you want – e.g. to create a table create script for all tables

Registered Servers / Central Management Server

If you have a lot of servers then re-entering the details in Object Explorer every time you start SSMS can be frustrating and time consuming. Fortunately there are two facilities within SSMS that enable these details to be entered just once and “remembered” each time you open up SSMS. These two facilities are Registered Servers and Central Management Servers. These were introduced in different versions of SQL Server and work in different ways, each has its own advantages and disadvantages so you may want to use both.

To add a registered server open the Registered Servers window from the View menu (or click CTRL + ALT + G), the window should appear in the top left corner of SSMS. Right click on the Local Server Groups folder and select ‘New Server Registration…’. Enter the server details and close the window. This new server should then appear under Local Server Groups, you can then right click and open up the server in Object Explorer or open a new query window. The server details are stored locally in an XML file and so will appear next time you open SSMS. If you have a lot of servers then you can also create Server Groups to group together similar servers. One advantage of creating groups (other than being able to logically group similar servers together) is that you can run a query against all servers in the group, by right clicking the group and selecting ‘New Group’.

Central Management Server are similar to Registered Servers but with some differences, the main one being that the server details are stored in a database (the Central Management Server) rather than a local file. A significant limitation with CMS is that the CMS server itself can’t be included in the list of servers.

Splitting the Query Window

The query window in SSMS can be split into two so that you can look at two parts of the same query simultaneously. Both parts of the split window can be scrolled independently. This is especially useful if you have a large query and want to compare different areas of the same query. To split the window simply drag the bar to the top right hand side of the window as shown below.

The splitter bar allows you to view one session with two panes. You can scroll in each pane independently. You can also edit in both the top and bottom pane.

Splitting the Query Window

Moving columns in the results pane

It may not be immediately obvious but you can switch columns around in the results pane when using the grid view, by dragging the column headers and dropping them next to another column header. This can be useful if you want to rearrange how the results are displayed without amending the query, especially if you have a lot of columns in your result set. This works only for one column.

Generating Charts and Drawings in SQL Server Management Studio

You don’t have to settle for T-SQL’s monochrome text output. These stored procedures let you quickly and easily turn your SELECT queries’ output into colorized charts and even computer-generated art. To turn your own data into a line, column, area, or bar chart using the Chart stored procedure, you need to design a SELECT query that serves as the first parameter in the stored procedure call.

Detailed article and code here: Generating Charts and Drawings in SQL Server Management Studio

Also you can generate amazing chart using awesome R libraries, detailed article: View R Plots from within SQL Server Management Studio

Additional Connection Parameters

One such change SSMS got for free is the connection resiliency logic within the SqlConnection.Open() method. To improve the default experience for clients which connect to Azure SQL Database, the above method will (in the case of initial connection errors / timeouts) now retry 1 time after sleeping for 10 seconds. These numbers are configurable by properties called ConnectRetryCount (default value 1) and ConnectRetryInterval (default value 10 seconds.) The previous versions of the SqlConnection class would not automatically retry in cases of connection failure.

There is a simple workaround for this situation. It is to add the following parameter string into the Additional Connection Parameters tab within the SSMS connection window. The good news is that you only need to do this once, as the property is saved for future sessions for that SQL Server (until of course it is removed by you later.)

ConnectRetryCount=0

Additional Connection Parameters

Working with tabs headers

You can view SPID in tabs header, quickly open script containing folder or copy script file path.

Working with tabs headers

Hiding tables in SSMS Object Explorer

  1. You can actually hide an object from object explorer by assigning a specific extended property:
EXEC sp_addextendedproperty
@name = N'microsoft_database_tools_support',
@value = 'Hide',
@level0type = N'Schema', @level0name = 'Person',
@level1type = N'Table', @level1name = 'Address';
GO

You can then remove the property (and the object will show back up) like so:

EXEC sp_dropextendedproperty
@name = N'microsoft_database_tools_support',
@level0type = N'Schema', @level0name = 'Person',
@level1type = N'Table', @level1name = 'Address';
GO
  1. DENY VIEW DEFINITION
DENY VIEW DEFINITION ON Schema.Table TO UserName;

Now UserName won’t be able to see Table in Object Explorer. In Fact, they won’t be able to see the table in sys.tables or INFORMATION_SCHEMA.TABLES

VIEW DEFINITION is the ability to see the definition of the object. In the case of SPs the code, same with Views and in the case of Tables it’s the columns definitions etc.

UnDock Tabs and Windows for Multi Monitor Support

From SSMS 2012 and onwards, you can easily dock/undock the query tabs as well as different object windows inside SSMS to make better use of the screen real estate and multiple monitors you have.

UnDock Tabs and Windows for Multi Monitor Support

RegEx-Based Finding and Replacing of Text in SSMS

So often, one sees developers doing repetitive coding in SSMS or Visual Studio that would be much quicker and easier by using the built-in Regular-Expression-based Find/Replace functionality. It is understandable, since the syntax is odd and some features are missing, but it is still well-worth knowing about.

More details and examples you can find here RegEx-Based Finding and Replacing of Text in SSMS.

My favorite regex: replace \t on \n,. It useful in many cases when you have column names copied from, for example, Excel and need quickly get sql query.

Changing what SSMS opens on startup

You can customize SSMS startup behavior in Tools -> Options -> Environment -> Startup and hide system objects in Object Explore:

Changing what SSMS opens on startup

Also you can disable the splash screen – this cuts the time it takes SSMS to load for versions before SSMS 17. Right click your shortcut to SSMS and select properties. Enter the text -nosplash right after the ending quote in the path:

SSMS link nosplash option

It is useful to create a solution of commonly used SQL scripts to always load at start-up.

  1. Display the Solution Explorer by pressing Ctrl+Alt+L or clicking View -> Solution Explorer.
  2. Then right click the Solution "Solution1" (0 projects) text and select Add -> New Project.
  3. Use the default SQL Server Scripts template and give your solution a clever name.
  4. Rename all of your SQL Code Snippets so the extension is .SQL. Drag them into the queries folder within the Solution Explorer.
  5. Open Windows explorer and browse to the location of your solution. Copy file location address to your clipboard. Go back to your SSMS shortcut properties and add within double quotes the location and file name of your solution before the “-nosplash”.

This is the complete text within my shortcut properties:

"C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\Ssms.exe" "C:\Users\taranov\Documents\SQL Server Management Studio\Projects\MySQLServerScripts.ssmssln" -nosplash

Create a solution of commonly used SQL scripts

Modifying New Query Template

You can modified New Query template for any instance SQL Server:

C:\Program Files (x86)\Microsoft SQL Server\[140|130|120|110]\Tools\Binn\ ManagementStudio\SqlWorkbenchProjectItems\Sql\SQLFile.sql

For example, you can add begin transaction statement for preventing ups queries:

BEGIN TRANSACTION;
 
 
-- COMMIT   TRANSACTION;
-- ROLLBACK TRANSACTION;

Thanks for this tip Aaron Bertrand: T-SQL Tuesday #92: Lessons Learned the Hard Way

Query Execution Options

More detailed article here: Knowing the Options

The options represent the SET values of the current session. SET options can affect how the query is execute thus having a different execution plan. You can find these options in two places within SSMS under Tools -> Options -> Query Execution -> SQL Server -> Advanced:

Query Execution Options Advanced

As well as Tools -> Options -> Query Execution -> SQL Server -> ANSI:

Query Execution Options ANSI

Using the interface to check what is set can get tiresome. Instead, you can use the system function @@OPTIONS. Each option shown above has a BIT value for all 15 options indicating whether or not it is enabled.

@@OPTIONS takes the binary representation and does a BITWISE operation on it to produce an integer value based on the sum of which BITS are enabled.

Default value for SELECT @@OPTIONS is 5496. Let’s assume for a moment that the only two options that are enabled on my machine are ANSI_PADDING and ANSI_WARNINGS. The values for these two options are 8 and 16, respectively speaking. The sum of the two is 24.

/***************************************************************
  Author: John Morehouse
  Summary: This script display what SET options are enabled for the current session.
 
  You may alter this code for your own purposes. You may republish altered code as long as you give due credit.
 
  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************/
SELECT 'Disable_Def_Cnst_Chk'    AS 'Option', CASE @@options & 1     WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'IMPLICIT_TRANSACTIONS'   AS 'Option', CASE @@options & 2     WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'CURSOR_CLOSE_ON_COMMIT'  AS 'Option', CASE @@options & 4     WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ANSI_WARNINGS'           AS 'Option', CASE @@options & 8     WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ANSI_PADDING'            AS 'Option', CASE @@options & 16    WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ANSI_NULLS'              AS 'Option', CASE @@options & 32    WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ARITHABORT'              AS 'Option', CASE @@options & 64    WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ARITHIGNORE'             AS 'Option', CASE @@options & 128   WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'QUOTED_IDENTIFIER'       AS 'Option', CASE @@options & 256   WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'NOCOUNT'                 AS 'Option', CASE @@options & 512   WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ANSI_NULL_DFLT_ON'       AS 'Option', CASE @@options & 1024  WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'ANSI_NULL_DFLT_OFF'      AS 'Option', CASE @@options & 2048  WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'CONCAT_NULL_YIELDS_NULL' AS 'Option', CASE @@options & 4096  WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'NUMERIC_ROUNDABORT'      AS 'Option', CASE @@options & 8192  WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled' UNION ALL
SELECT 'XACT_ABORT'              AS 'Option', CASE @@options & 16384 WHEN 0 THEN 0 ELSE 1 END AS 'Enabled/Disabled';

SQL Server Diagnostics Extension

Analyze Dumps – Customers using this extension will be able to debug and self-resolve memory dump issues from their SQL Server instances and receive recommended Knowledge Base (KB) article(s) from Microsoft, which may be applicable for the fix. The memory dumps are stored in a secured and compliant manner as governed by the Microsoft Privacy Policy.

For example, Joe, a DBA from Contoso, Ltd., finds that SQL Server has generated a memory dump while running a workload, and he would like to debug the issue. Using this feature, John can upload the dump and receive recommended KB articles from Microsoft, which can help him fix the issue.

SQL Server Diagnostics Extension

sql server dbcc常用命令使用详解

Standard

常用DBCC命令详解

Transact-SQL 编程语言提供 DBCC 语句作为 SQL Server 的数据库控制台命令。

DBCC 命令使用输入参数并返回值。所有 DBCC 命令参数都可以接受 Unicode 和 DBCS 文字。
维护命令
1、DBCC INPUTBUFFER
功能:显示从客户端发送到 Microsoft SQL Server实例的最后一个语句。
格式:DBCC INPUTBUFFER ( session_id [ , request_id ] )[WITH NO_INFOMSGS ]
执行权限:用户必须是 sysadmin 固定服务器角色的成员。
用户必须具有 VIEW SERVER STATE 权限。
相关命令:SELECT @@spid
SELECT request_id FROM sys.dm_exec_requests WHERE session_id = @@spid

2、DBCC OUTPUTBUFFER
功能:以十六进制和 ASCII 格式返回指定 session_id 的当前输出缓冲区。
格式:DBCC OUTPUTBUFFER ( session_id [ , request_id ] )[ WITH NO_INFOMSGS ]
执行权限:用户必须是 sysadmin 固定服务器角色的成员。
相关命令:SELECT @@spid
SELECT request_id FROM sys.dm_exec_requests WHERE session_id = @@spid

3、DBCC SHOWCONTIG
功能:显示指定的表或视图的数据和索引的碎片信息。
格式:DBCC SHOWCONTIG [ (对象名) ]
[ WITH  { [ , [ ALL_INDEXES ] ] [ , [ TABLERESULTS ] ] [ , [ FAST ] ] [ , [ ALL_LEVELS ] ]  [ NO_INFOMSGS ] } ]
执行权限:用户必须是对象所有者,或是 sysadmin 固定服务器角色、db_owner 固定数据库角色或 db_ddladmin 固定数据库角色的成员。
例:DBCC SHOWCONTIG (‘TableName’)
说明:可使用DBCC SHOWCONTIG 和 DBCC INDEXDEFRAG 对数据库中的索引进行碎片整理

4、DBCC OPENTRAN
功能:如果在指定数据库内存在最早的活动事务和最早的分布式和非分布式复制事务,则显示与之有关的信息。
格式:DBCC OPENTRAN [ ( [ database_name | database_id | 0 ] ) ]
{ [ WITH TABLERESULTS ] [ , [ NO_INFOMSGS ] ] }]
例:DBCC OPENTRAN (DataBaseName) WITH TABLERESULTS

5、DBCC SQLPERF
功能:为所有数据库提供事务日志空间用法的统计信息。也可以用于重置等待和闩锁的统计信息。
格式:DBCC SQLPERF ([ LOGSPACE ]|
[ “sys.dm_os_latch_stats” , CLEAR ] |
[ “sys.dm_os_wait_stats” , CLEAR ])
[WITH NO_INFOMSGS ]
例:DBCC SQLPERF (LOGSPACE)

6、DBCC TRACESTATUS
功能:显示跟踪标志的状态
格式:DBCC TRACESTATUS ( [ [ trace# [ ,…n ] ] [ , ] [ -1 ] ] ) [ WITH NO_INFOMSGS ]

7、DBCC PROCCACHE
功能:以表格格式显示有关过程缓存的信息。
格式:DBCC PROCCACHE [ WITH NO_INFOMSGS ]
执行权限:用户必须是 sysadmin 固定服务器角色、db_owner 固定数据库角色的成员。

8、DBCC USEROPTIONS
功能:返回当前连接的活动(设置)的 SET 选项。
格式:DBCC USEROPTIONS [ WITH NO_INFOMSGS ]
执行权限:要求具有 public 角色成员身份。
例:DBCC USEROPTIONS

9、DBCC SHOW_STATISTICS
功能:显示指定表上的指定目标的当前分发统计信息。

10、DBCC SHOWFILESTATS
功能:显示文件使用情况的,需要通过换算所得
如显示的是extent,一个extent为64k。totalexents*64/1024/1024 换算成gb
验证语句

11、DBCC CHECKALLOC
功能:检查指定数据库的磁盘空间分配结构的一致性。
例:DBCC CHECKALLOC (‘DataBaseName’)
执行权限:要求具有 sysadmin 固定服务器角色或 db_owner 固定数据库角色的成员身份。

12、DBCC CHECKFILEGROUP
功能:检查当前数据库中指定文件组中的所有表和索引视图的分配和结构完整性。
格式:比如:DBCC CHECKFILEGROUP (‘DataBaseName’)

13、DBCC CHECKCATALOG
功能:检查指定数据库内的目录一致性。
比如:DBCC CHECKCATALOG (‘datapeng’)

14、DBCC CHECKIDENT
功能:检查指定表的当前标识值,如有必要,则更改标识值。

比如:DBCC CHECKIDENT (‘datapeng01’)

15、DBCC CHECKCONSTRAINTS
功能:检查当前数据库中指定表上的指定约束或所有约束的完整性。

16、DBCC CHECKTABLE
功能:检查组成表或索引视图的所有页和结构的完整性。

17、DBCC CHECKDB
功能:通过执行下列操作检查指定数据库中所有对象的逻辑和物理完整性:

对数据库运行 DBCC CHECKALLOC。
对数据库中的每个表和视图运行 DBCC CHECKTABLE。
对数据库运行 DBCC CHECKCATALOG。
验证数据库中每个索引视图的内容。
验证数据库中的 Service Broker 数据。

维护语句

18、DBCC CLEANTABLE
功能:回收表或索引视图中已删除的可变长度列的空间。

比如:DBCC cleantable (‘datapeng’,’datapeng01′)

19、DBCC INDEXDEFRAG
功能:指定表或视图的索引碎片整理。

比如:DBCC INDEXDEFRAG (‘datapeng’,’datapeng01′)

Pages Scanned Pages Moved Pages Removed————- ———– ————-359           346         8(1 row(s) affected)

20、DBCC DBREINDEX
功能:对指定数据库中的表重新生成一个或多个索引。

比如:DBCC DBREINDEX ( ‘datapeng’,’datapeng01′)

21、DBCC SHRINKDATABASE
功能:收缩指定数据库中的数据文件和日志文件的大小。

比如:DBCC SHRINKDATABASE (‘datapeng’)

21、DBCC SHRINKFILE
功能:收缩当前数据库的指定数据或日志文件的大小
比如:DBCC SHRINKFILE (‘datapeng’)

22、DBCC FREEPROCCACHE
功能:从过程缓存中删除所有元素。

23、DBCC UPDATEUSAGE
功能:报告目录视图中的页数和行数错误并进行更正。这些错误可能导致 sp_spaceused 系统存储过程返回不正确的空间使用报告。

杂项语句

24、DBCC dllname (FREE)
功能:从内存中上载指定的扩展存储过程 DLL。

25、DBCC HELP
功能:返回指定的 DBCC 命令的语法信息。

比如:DBCC   HELP (‘checkdb’)

26、DBCC FREESESSIONCACHE
功能:刷新针对 Microsoft SQL Server 实例执行的分布式查询所使用的分布式查询连接缓存。

27、DBCC TRACEON
功能:启用指定的跟踪标记。

格式:DBCC TRACEON ( trace# [ ,…n ][ , -1 ] ) [ WITH NO_INFOMSGS ]

28、DBCC TRACEOFF
功能:禁用指定的跟踪标记。
DBCC FREESYSTEMCACHE:从所有缓存中释放所有未使用的缓存条目。SQL Server 2005 数据库引擎会事先在后台清理未使用的缓存条目,以使内存可用于当前条目。但是,可以使用此命令从所有缓存中手动删除未使用的条目。

比如;DBCC FREESYSTEMCACHE(‘all’)

 

http://blog.itpub.net/29371470/viewspace-1082379