Skip to content

Commit d5b0b40

Browse files
authored
Add files via upload
1 parent a197af5 commit d5b0b40

24 files changed

+2200
-0
lines changed

Scripts/DBCC_CHECKDB_Latest_date.sql

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Author: Max Vernon
3+
Original link: https://www.sqlserverscience.com/maintenance/check-dbcc-checkdb/
4+
Source link: https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/DBCC_CHECKDB_Latest_date.sql
5+
Modified: 2018-01-26 18:20 by Konstantin Taranov
6+
*/
7+
8+
9+
SET NOCOUNT ON;
10+
11+
DECLARE @MaxDaysWithoutCheckDB int = 1;
12+
13+
IF OBJECT_ID(N'tempdb..#Results') IS NOT NULL
14+
BEGIN
15+
DROP TABLE #Results;
16+
END;
17+
18+
CREATE TABLE #Results(
19+
DatabaseName SYSNAME NULL
20+
, IsOnline BIT NULL
21+
, ParentObject varchar(100) NULL
22+
, [Object] varchar(100) NULL
23+
, [Field] varchar(100) NULL
24+
, [Value] varchar(100) NULL
25+
);
26+
27+
DECLARE @cmd NVARCHAR(4000);
28+
DECLARE @dbName SYSNAME;
29+
DECLARE @IsOnline BIT;
30+
31+
DECLARE cur CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY
32+
FOR
33+
SELECT DBCCCommand = CAST(N'DBCC DBINFO(N''' + d.name + N''') WITH TABLERESULTS;' AS NVARCHAR(500))
34+
, DatabaseName = d.name
35+
, IsOnline = CONVERT(BIT, CASE WHEN d.state_desc = 'ONLINE' THEN 1 ELSE 0 END)
36+
FROM sys.databases d
37+
ORDER BY d.name;
38+
39+
OPEN cur;
40+
FETCH NEXT FROM cur INTO @cmd, @dbName, @IsOnline;
41+
42+
WHILE @@FETCH_STATUS = 0
43+
BEGIN
44+
RAISERROR (@dbName, 0, 1) WITH NOWAIT;
45+
IF @IsOnline = 1
46+
BEGIN
47+
INSERT INTO #Results
48+
(ParentObject, [Object], [Field], [Value])
49+
EXEC sp_executesql @cmd;
50+
51+
UPDATE #Results
52+
SET DatabaseName = @dbName
53+
, IsOnline = @IsOnline
54+
WHERE DatabaseName IS NULL;
55+
END
56+
ELSE
57+
BEGIN
58+
INSERT INTO #Results
59+
(DatabaseName, IsOnline)
60+
VALUES
61+
(@dbName, @IsOnline)
62+
END
63+
64+
FETCH NEXT FROM cur INTO @cmd, @dbName, @IsOnline;
65+
END
66+
67+
CLOSE cur;
68+
DEALLOCATE cur;
69+
70+
SELECT ServerName = @@SERVERNAME
71+
, DatabaseName = r.DatabaseName
72+
, LastKnownGoodDate = CONVERT(DATETIME, r.Value, 120)
73+
, AtRisk = CASE WHEN DATEDIFF(day, CONVERT(DATETIME, r.Value, 120), GETDATE()) > @MaxDaysWithoutCheckDB THEN 'X' ELSE '' END
74+
, IsDBOnline = r.IsOnline
75+
FROM #Results r
76+
WHERE r.Field = 'dbi_dbccLastKnownGood'
77+
OR r.Field IS NULL;
+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*--------------------------------------------------------------------------------------------------
2+
Name: DataDictionarySelect.sql
3+
Purpose: Display information for every column in every table of a database.
4+
Author: Patrick Slesicki
5+
Created: 3/16/2008
6+
Notes: Select the database to run against and execute.
7+
http://www.sqlservercentral.com/scripts/Data+Dictionary/167354/
8+
----------------------------------------------------------------------------------------------------
9+
PRELIMINAIRES
10+
*/--------------------------------------------------------------------------------------------------
11+
DECLARE
12+
@InstanceName AS nvarchar(128) = @@SERVERNAME
13+
,@DatabaseName AS nvarchar(128) = DB_NAME()
14+
15+
SELECT
16+
InstanceName = @InstanceName
17+
,DatabaseName = @DatabaseName
18+
,ObjectType = o.type_desc
19+
,SchemaName = s.name
20+
,ObjectName = o.name
21+
,ColumnID = c.column_id
22+
,ColumnName = c.name
23+
,SQLServerDataType = CASE
24+
--Variable length fields
25+
WHEN st.name IN('binary', 'char', 'text', 'varbinary', 'varchar') AND c.max_length > -1 THEN st.name + '(' + CONVERT(varchar(6), c.max_length) + ')'
26+
--Unicode fields require twice the storage as standard variable length fields
27+
WHEN st.name IN('nchar', 'ntext', 'nvarchar') AND c.max_length > -1 THEN st.name + '(' + CONVERT(varchar(6), c.max_length / 2) + ')'
28+
--Scale based lengths for date and time fields
29+
WHEN st.name IN('datetime2', 'datetimeoffset', 'time') THEN st.name + '(' + CONVERT(varchar(3), c.scale) + ')'
30+
--Numeric fields with precision and scale
31+
WHEN st.name IN('decimal', 'numeric') THEN st.name + '(' + CONVERT(varchar(3), c.precision) + ', ' + CONVERT(varchar(3), c.scale) + ')'
32+
--Large Object types (LOB)
33+
WHEN st.name NOT IN('geography', 'geometry', 'xml') AND c.max_length = -1 THEN st.name + '(MAX)'
34+
--Everything else
35+
ELSE st.name
36+
END
37+
,CustomDataTypeName = CASE ut.name
38+
WHEN st.name THEN ''
39+
ELSE ut.name
40+
END
41+
,Length = c.max_length
42+
,Precision = c.precision
43+
,Scale = c.scale
44+
,IsNullable = c.is_nullable
45+
,IsIdentity = c.is_identity
46+
,PrimaryKeyType = ISNULL
47+
(
48+
(
49+
SELECT TOP (1) i.type_desc
50+
FROM sys.indexes AS i
51+
JOIN sys.index_columns AS ic
52+
ON i.object_id = ic.object_id
53+
AND i.index_id = ic.index_id
54+
WHERE
55+
ic.object_id = c.object_id
56+
AND ic.column_id = c.column_id
57+
AND i.is_primary_key = 1
58+
)
59+
,''
60+
)
61+
,ForeignKeySource = ISNULL
62+
(
63+
(
64+
SELECT TOP (1) QUOTENAME(ss.name) + '.' + QUOTENAME(so.name) + '.' + QUOTENAME(c.name)
65+
FROM sys.foreign_keys AS fk
66+
JOIN sys.foreign_key_columns AS fkc
67+
ON fk.object_id = fkc.constraint_object_id
68+
JOIN sys.objects AS so
69+
ON fkc.referenced_object_id = so.object_id
70+
JOIN sys.schemas AS ss
71+
ON so.schema_id = ss.schema_id
72+
WHERE
73+
fkc.parent_object_id = c.object_id
74+
AND fkc.parent_column_id = c.column_id
75+
)
76+
,''
77+
)
78+
,DependentForeignKeyCount =
79+
(
80+
SELECT COUNT(*)
81+
FROM sys.foreign_key_columns AS fkc
82+
WHERE
83+
c.object_id = fkc.referenced_object_id
84+
AND c.column_id = fkc.referenced_column_id
85+
)
86+
,DefaultValue = ISNULL
87+
(
88+
(
89+
SELECT TOP (1) dc.definition
90+
FROM sys.default_constraints AS dc
91+
WHERE
92+
c.object_id = dc.parent_object_id
93+
AND c.column_id = dc.parent_column_id
94+
)
95+
,''
96+
)
97+
,UniqueConstraintName = ISNULL
98+
(
99+
(
100+
SELECT TOP (1) i.name
101+
FROM sys.indexes AS i
102+
JOIN sys.index_columns AS ic
103+
ON i.object_id = ic.object_id
104+
AND i.index_id = ic.index_id
105+
WHERE
106+
ic.object_id = c.object_id
107+
AND ic.column_id = c.column_id
108+
AND i.is_unique_constraint = 1
109+
)
110+
,''
111+
)
112+
,CheckConstraintDefinition = ISNULL
113+
(
114+
(
115+
SELECT TOP (1) dc.definition
116+
FROM sys.check_constraints AS dc
117+
WHERE
118+
c.object_id = dc.parent_object_id
119+
AND c.column_id = dc.parent_column_id
120+
)
121+
,''
122+
)
123+
FROM sys.schemas AS s
124+
JOIN sys.objects AS o
125+
ON s.schema_id = o.schema_id
126+
JOIN sys.columns AS c
127+
ON o.object_id = c.object_id
128+
JOIN sys.types AS ut
129+
ON c.user_type_id = ut.user_type_id
130+
JOIN sys.types AS st
131+
ON ut.system_type_id = st.user_type_id
132+
WHERE
133+
o.type_desc NOT IN('INTERNAL_TABLE', 'SYSTEM_TABLE')
134+
AND s.name NOT IN('cdc', 'sys')
135+
AND o.name NOT LIKE 'MSp%'
136+
AND o.name NOT LIKE 'sys%'
137+
AND o.name NOT LIKE 'SYNC_%'
138+
139+
ORDER BY
140+
CASE o.type_desc
141+
WHEN 'USER_TABLE' THEN 1
142+
WHEN 'VIEW' THEN 2
143+
WHEN 'SQL_INLINE_TABLE_VALUED_FUNCTION' THEN 3
144+
WHEN 'SQL_TABLE_VALUED_FUNCTION' THEN 4
145+
ELSE 5
146+
END
147+
,s.name
148+
,o.name
149+
,c.column_id
150+
151+
/*--------------------------------------------------------------------------------------------------
152+
END
153+
*/--------------------------------------------------------------------------------------------------
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Author: Tim Ford
3+
Original link: http://sqlmag.com/database-administration/how-set-sql-server-database-mail-one-easy-script
4+
*/
5+
6+
====================================
7+
-- DATABASE MAIL CONFIGURATION
8+
--================================================================
9+
--==========================================================
10+
-- Create a Database Mail account
11+
--==========================================================
12+
EXECUTE msdb.dbo.sysmail_add_account_sp
13+
@account_name = '<account_name, DBM account name, Database Mail Primary Account>',
14+
@description = '<description, , SQL Server Notification Service>',
15+
@email_address = '<email_address, email address for DBM. Does not need a valid mail account ,>',
16+
@replyto_address = '<replyto_address, reply email address for DBM. Does not need a valid mail account ,>',
17+
@display_name = '<display_name, friendly name for emails sent via DBM, Database Mail Account>',
18+
@mailserver_name = '<mailserver_name, smtp mail server name,>',
19+
@port = <port_number, port number of the mailserver, 25>;
20+
21+
22+
23+
--==========================================================
24+
-- Create a Database Mail Profile
25+
--==========================================================
26+
DECLARE @profile_id INT, @profile_description sysname;
27+
SELECT @profile_id = COALESCE(MAX(profile_id),1) FROM msdb.dbo.sysmail_profile
28+
SELECT @profile_description = 'Database Mail Profile for ' + @@servername
29+
30+
31+
EXECUTE msdb.dbo.sysmail_add_profile_sp
32+
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
33+
@description = @profile_description;
34+
35+
-- Add the account to the profile
36+
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
37+
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
38+
@account_name = '<account_name, DBM account name, Database Mail Primary Account>',
39+
@sequence_number = @profile_id;
40+
41+
-- Grant access to the profile to the DBMailUsers role
42+
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
43+
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>',
44+
@principal_id = 0,
45+
@is_default = 1 ;
46+
47+
48+
--==========================================================
49+
-- Enable Database Mail
50+
--==========================================================
51+
USE master;
52+
GO
53+
54+
sp_CONFIGURE 'show advanced', 1
55+
GO
56+
RECONFIGURE
57+
GO
58+
sp_CONFIGURE 'Database Mail XPs', 1
59+
GO
60+
RECONFIGURE
61+
GO
62+
63+
64+
--EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'DatabaseMailProfile', N'REG_SZ', N''
65+
--EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail', N'REG_DWORD', 1
66+
--GO
67+
68+
EXEC msdb.dbo.sp_set_sqlagent_properties @email_save_in_sent_folder = 0
69+
GO
70+
71+
72+
--==========================================================
73+
-- Review Outcomes
74+
--==========================================================
75+
SELECT * FROM msdb.dbo.sysmail_profile;
76+
SELECT * FROM msdb.dbo.sysmail_account;
77+
GO
78+
79+
80+
--==========================================================
81+
-- Test Database Mail
82+
--==========================================================
83+
DECLARE @sub VARCHAR(100)
84+
DECLARE @body_text NVARCHAR(MAX)
85+
SELECT @sub = 'Test from New SQL install on ' + @@servername
86+
SELECT @body_text = N'This is a test of Database Mail.' + CHAR(13) + CHAR(13) + 'SQL Server Version Info: ' + CAST(@@version AS VARCHAR(500))
87+
88+
EXEC msdb.dbo.[sp_send_dbmail]
89+
@profile_name = '<profile_name, DBM profile name, Database Mail Primary Profile>'
90+
, @recipients = '<test_email_address, email address to send test email,>'
91+
, @subject = @sub
92+
, @body = @body_text
93+
94+
95+
96+
97+
98+
--================================================================
99+
-- SQL Agent Properties Configuration
100+
--================================================================
101+
EXEC msdb.dbo.sp_set_sqlagent_properties
102+
@databasemail_profile = '<profile_name, DBM profile name, Database Mail Primary Profile>'
103+
, @use_databasemail=1
104+
GO

0 commit comments

Comments
 (0)