Recently a customer asked me if I could write a script that could document which logins on the sql SQL Server that was member of a server role.
Select
CASE
WHEN isntgroup=1 THEN 'Windows Group'
WHEN isntuser=1 THEN 'Windows User'
WHEN isntuser=0 THEN 'SQL Login '
END LoginType,
loginname as [Login Name],
CASE sysadmin WHEN 1 THEN 'Yes' ELSE 'No' END isSysAdmin,
CASE securityadmin WHEN 1 THEN 'Yes' ELSE 'No' END isSecurityAdmin,
CASE serveradmin WHEN 1 THEN 'Yes' ELSE 'No' END isServerAdmin,
CASE processadmin WHEN 1 THEN 'Yes' ELSE 'No' END isProcessAdmin,
CASE diskadmin WHEN 1 THEN 'Yes' ELSE 'No' END isDiskAdmin,
CASE dbcreator WHEN 1 THEN 'Yes' ELSE 'No' END isDBCreator,
CASE bulkadmin WHEN 1 THEN 'Yes' ELSE 'No' END isBulkAdmin,
createdate LoginCreatedDate
from sys.syslogins
where hasaccess=1
Monday, October 10, 2011
How to document logins with server roles
Indsendt af
Rasmus Glibstrup
kl.
10:05
0
kommentarer
Etiketter: database maintenance
Wednesday, September 28, 2011
How to make your SQL Server create database files instantly
The trick is "Instant File Initialization" - an NTFS feature that, when enabled for the SQL Server service account, creates the database files almost instantly - even with TB of size.
Note that this trick will also reduce your restore time !
How to: http://www.mytechmantra.com/LearnSQLServer/Database_Instant_File_Initialization_Feature.html
Remember that this feature does not work with TDE enabled.
Indsendt af
Rasmus Glibstrup
kl.
13:15
0
kommentarer
Etiketter: database maintenance, Performance tuning
Wednesday, August 25, 2010
Script Server Roles when migrating to new SQL Server
A handy script for scripting out Server Roles that logins belongs to when migrating to another server.
/**************************************************
Script Server Roles
Author: Rasmus Glibstrup
Company: it-Craft Denmark
Date: 25-08-2010
Version: 1.0
Script to generate all Server Roles for SQL Server
in the case of migrating logins to other SQL Server
May very well be used together with sp_help_revlogin
Output is in text and can be cut'n'pasted to the
new server.
***************************************************/
SET NOCOUNT ON
DECLARE @SQLCmd nvarchar(1000)
DECLARE @RoleName sysname
DECLARE @Login sysname
DECLARE @Count int
Create table #ServerRoles (
ServerRole sysname,
MemberName sysname,
MemberSID varbinary(85))
INSERT INTO #ServerRoles
exec sp_helpsrvrolemember
DECLARE ServerRoleCursor Cursor
FOR SELECT ServerRole,MemberName
FROM #ServerRoles
WHERE MemberName not like 'NT SERVICE%' AND
MemberName <> 'sa' AND
MemberName not like 'NT AUTHORITY%'
OPEN ServerRoleCursor
FETCH NEXT FROM ServerRoleCursor
INTO @RoleName, @Login
SET @Count = 0
WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLCmd = 'exec sp_addsrvrolemember ''' + @Login + ''' , ''' + @RoleName + ''''
PRINT @SQLCmd
SET @Count = @Count + 1
FETCH NEXT FROM ServerRoleCursor
INTO @RoleName, @Login
END
IF @Count=0
Print 'No logins with serverroles, besides SA'
ELSE
Print CAST(@Count as varchar(5)) + ' Roles scriptet'
CLOSE ServerRoleCursor
DEALLOCATE ServerRoleCursor
DROP TABLE #ServerRoles
Indsendt af
Rasmus Glibstrup
kl.
10:57
0
kommentarer
Etiketter: database maintenance
Wednesday, December 16, 2009
A couple of nice DATE functions
First day of month:
SELECT
DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
Last day of month:
SELECT
DATEADD(day,-1,DATEADD(month, DATEDIFF(month, 0, GETDATE())+1, 0))
Just the date of any datetime field:
SELECT
CAST(FLOOR(CAST(GETDATE()
AS
FLOAT))AS
DATETIME)
I Can't take credit for these, but they are just SOO handy - and fast.
Indsendt af
Rasmus Glibstrup
kl.
12:15
0
kommentarer
Etiketter: dates, programming
Monday, May 19, 2008
Performance tuning counters for SQL Server
As an instructor I always tell my students to make a performance baseline on their SQL Server so that they have something to compare against, when (not if) the users complain about performance. But my students and customers often ask – which counters should I monitor then? Well here is, to my experience, a good list to start with. Arguably you could narrow or widen the list – but for a start this list has in my experience proven to be sufficient. Performance object Counter Warning Alert Notes Memory Pages/sec 100 200 I know that many sites say 20 – but that was from the NT4 days with 128 MB RAM Memory Available MBytes 200 100 SQL Server: Buffer Manager Buffer Cache Hit Ratio 90 85 SQL Server: Buffer Manager Page Life Expectancy 350 300 These should be per physical disk Physical Disk Disk Reads/sec No levels – just monitor these Physical Disk Disk Writes/sec No levels – just monitor these Physical Disk % Disk time No levels – just monitor these Physical Disk % Disk time No levels – just monitor these Physical Disk Avg. Disk Queue Length 1 2 Processor % Processor Time 70 80 System Processor Queue Length Network Interface Bytes Received/sec No levels – just monitor these Network Interface Bytes Sent/sec No levels – just monitor these Network Interface Output Queue Length No levels – just monitor these SQL Server: General User Connections No levels – just monitor these SQL Server: Memory Manager Connection Memory (KB) No levels – just monitor these SQL Server: Memory Manager Total Server Memory (KB) No levels – just monitor these You don't have to monitor the counters all the time to create a good baseline, the warning and alert levels are included for those of you who want to implement the counters in your normal performance monitoring system like MOM.
Indsendt af
Rasmus Glibstrup
kl.
14:19
0
kommentarer
Etiketter: Performance Counters, Performance tuning
Friday, November 16, 2007
Import and Export SQL 2005 Maintenance plans
The way SQL 2005 produces Maintenance plans is very different from SQL 2000. Instead of using the extended stored procedure xp_sqlmaint to execute and the system tables of the MSDB database to store the package – SQL 2005 uses SSIS (SQL Server Integration Services) to create and store the Maintenance plans. To Export an existing maintenance plan from an SQL Server 2005 To Import or copy an existing maintenance plan from one server to another
Indsendt af
Rasmus Glibstrup
kl.
21:20
2
kommentarer
Etiketter: database maintenance, Integration Services, SSIS
Friday, November 9, 2007
Moving system databases
When installing SQL Server 2000/2005/2008 many often forget where the system databases go on the filesystem. They are by default placed into C:\Program Files\Microsoft SQL Server \.... – Which we properly all can agree is not the optimal placement. Note that to move the system databases require SQL server downtime, it doesn't take long though (depending on the size of course) DISCLAIMER: Please test this recipe on a test server before trying it in production – failing at any step will invalidate or totally stop you SQL server installation. To move the system databases do as follows: Adapt the new file location to match YOUR filesystem ALTER DATABASE MODEL MODIFY FILE (NAME = modellog , FILENAME = 'G:\SQLLogFiles\modellog.ldf') ALTER DATABASE TEMPDB MODIFY FILE (NAME = templog , FILENAME = 'G:\SQLLogFiles\templog.ldf') ALTER DATABASE MSDB MODIFY FILE (NAME = MSDBlog , FILENAME = 'G:\SQLLogFiles\MSDBlog.ldf') ALTER DATABASE master MODIFY FILE (NAME = master , FILENAME = 'H:\SQLDataFiles\master.mdf') As Per SQL Server 2008 the MSSQLSYSTEMRESOURCE database cannot be moved ! But in SQL Server 2005 the database location must be the same as the Master database. The MSSQLSYSTEMRESOURCE database file and log file must remain on the same drive ! Please test this recipe on a test server before trying it in production – failing at any step will invalidate or totally stop you SQL server installation.
DO NOT MOVE THE FILES PHYSICALLY YET
GO
ALTER DATABASE MODEL MODIFY FILE (NAME = modeldev , FILENAME = 'H:\SQLDataFiles\model.mdf')
GO
GO
ALTER DATABASE TEMPDB MODIFY FILE (NAME = tempdev , FILENAME = 'H:\SQLDataFiles\tempdb.mdf')
GO
GO
ALTER DATABASE MSDB MODIFY FILE (NAME = MSDBData, FILENAME = 'H:\SQLDataFiles\MSDBData.mdf')
GO
GO
ALTER DATABASE master MODIFY FILE (NAME = mastlog, FILENAME = 'G:\SQLLogFiles\Mastlog.ldf')
GO
For SQL 2005 use:
ALTER DATABASE mssqlsystemresource MODIFY FILE (NAME=data, FILENAME= 'H:\SQLDataFiles\mssqlsystemresource.mdf')
GO
ALTER DATABASE mssqlsystemresource MODIFY FILE (NAME=log, FILENAME= 'H:\SQLDataFiles\mssqlsystemresource.ldf')
GO
-dH:\SQLDataFiles\master.mdf
-eH:\SQLDataFiles\log\ErrorLog
-lG:\SQLLogFiles\mastlog.ldf
Indsendt af
Rasmus Glibstrup
kl.
10:35
0
kommentarer
Etiketter: Installation