USE master
go
IF object_id('sp_Mylogininfo') IS NOT NULL
DROP PROCEDURE sp_Mylogininfo
go
CREATE PROCEDURE sp_Mylogininfo
(
@loginmode varchar(30) = NULL OUTPUT,
@integrated varchar(30) = NULL OUTPUT,
@showdetails bit = 0
)
as
SET nocount ON
SET ansi_defaults OFF
DECLARE @account varchar(30), @nt_domain varchar(30), @nt_username varchar(30),
@privilege varchar(30), @showresults bit
IF @loginmode IS NULL or @integrated IS NULL
SELECT @showresults = 1
CREATE TABLE #loginconfig (name varchar(30), config_value varchar(30) null)
INSERT #loginconfig EXEC master..xp_loginconfig
SELECT @loginmode = config_value FROM #loginconfig WHERE name = 'login mode'
IF @loginmode = 'standard'
begin
SELECT @integrated = 'No'
GOTO SHOW_RESULTS_LABEL
end
CREATE TABLE #logininfo (account varchar(60), type varchar(30),
privilege varchar(30) null, mapped_login varchar(60) null,
permission_path varchar(255) null)
INSERT #logininfo EXEC master..xp_logininfo
DECLARE accounts insensitive CURSOR FOR
SELECT account FROM #logininfo WHERE charindex('SYSTEM', account) = 0
OPEN accounts
while('FETCH IS OK' = 'FETCH IS OK')
begin
FETCH next FROM accounts INTO @account
IF @@fetch_status < 0 BREAK
INSERT #logininfo EXEC master..xp_logininfo @account, 'members'
end
DEALLOCATE accounts
SELECT @nt_domain = nt_domain, @nt_username = nt_username FROM sysprocesses
WHERE spid = @@spid
-- First check for sa
IF suser_id() = 1
begin
-- Next CHECK FOR NT USER privilege
IF exists( SELECT privilege FROM #logininfo
WHERE account = @nt_domain + '\' + @nt_username
AND privilege = 'admin')
SELECT @integrated = 'Yes'
ELSE
SELECT @integrated = 'No'
GOTO SHOW_RESULTS_LABEL
end
-- Second check for users
SELECT @integrated = CASE WHEN @nt_username = suser_name() THEN 'Yes' ELSE 'No' END
SHOW_RESULTS_LABEL:
IF @showresults = 1
begin
SELECT @loginmode AS "Server Login Mode",
@integrated AS "Integrated"
end
IF @showdetails = 1
begin
PRINT ''
PRINT 'Server Security Configuration'
SELECT * FROM #loginconfig
PRINT ''
PRINT 'Server Security Details'
SELECT * FROM #logininfo
end
go
GRANT EXECUTE on sp_Mylogininfo TO PUBLIC
go
-- Usage Examples:
PRINT 'With no parameters...'
EXEC sp_Mylogininfo
PRINT ''
go
PRINT 'With show details option...'
EXEC sp_Mylogininfo @showdetails = 1
PRINT ''
go
PRINT 'With output parameters...'
DECLARE @loginmode varchar(30), @integrated varchar(30)
SELECT @loginmode = '', @integrated = ''
EXEC sp_Mylogininfo @loginmode out, @integrated out
SELECT @loginmode AS LoginMode, @integrated as Integrated
donderdag 27 november 2008
Discover the name of the Windows group that allowed you to connect
If you need name of the Windows group that allowed you to connect:
==================================================================
If you need name of the Windows group that allowed the user to connect, take
a look at xp_logininfo and the 'mapped user name' column in the result.
You'll need to insert the results into a table like the example below if you
need a scalar result. See xp_logininfo in the Books Online for permission
requirements.
-------------------------------------------:
http://www.megasolutions.net/Sqlserver/Using-suser_id()-and-sys_server_principals-when-connecting-through-a-windows-group-7608.aspx
SET NOCOUNT ON
DECLARE
@me sysname,
@permission_path sysname
SET @me = SUSER_SNAME()
CREATE TABLE #LoginInfo
(
account_name sysname,
logintype char(8),
privilege char(9),
mapped_login_name sysname,
permission_path sysname
)
INSERT INTO #LoginInfo
EXEC xp_logininfo @me
SELECT @permission_path = permission_path
FROM #LoginInfo
DROP TABLE #LoginInfo
SELECT @permission_path
GO
==================================================================
If you need name of the Windows group that allowed the user to connect, take
a look at xp_logininfo and the 'mapped user name' column in the result.
You'll need to insert the results into a table like the example below if you
need a scalar result. See xp_logininfo in the Books Online for permission
requirements.
-------------------------------------------:
http://www.megasolutions.net/Sqlserver/Using-suser_id()-and-sys_server_principals-when-connecting-through-a-windows-group-7608.aspx
SET NOCOUNT ON
DECLARE
@me sysname,
@permission_path sysname
SET @me = SUSER_SNAME()
CREATE TABLE #LoginInfo
(
account_name sysname,
logintype char(8),
privilege char(9),
mapped_login_name sysname,
permission_path sysname
)
INSERT INTO #LoginInfo
EXEC xp_logininfo @me
SELECT @permission_path = permission_path
FROM #LoginInfo
DROP TABLE #LoginInfo
SELECT @permission_path
GO
Shrinking the Transaction Log in SQL Server 2000 with DBCC SHRINKFILE
Shrinking the Transaction Log in SQL Server 2000 with DBCC SHRINKFILE
--------------------------------------------------------------------
http://support.microsoft.com/kb/272318
When DBCC SHRINKFILE is run, SQL Server 2000 shrinks the log file by removing as many virtual log files as it can to attempt to reach the target size. If the target file size is not reached, SQL Server places dummy log entries in the last virtual log file until the virtual log is filled and moves the head of the log to the beginning of the file. The following actions are then required to complete the shrinking of the transaction log:
You must run a BACKUP LOG statement to free up space by removing the inactive portion of the log.
You must run DBCC SHRINKFILE again with the desired target size until the log file shrinks to the target size.
The following example demonstrates this with the pubs database and attempts to shrink the pubs_log file to 2 MB:
Run this code:
DBCC SHRINKFILE(pubs_log, 2)
NOTE: If the target size is not reached, proceed to the next step.
Run this code if you want to truncate the transaction log and not keep a backup of the transaction log. Truncate_only invalidates your transaction log backup sequence. Take a full backup of your database after you perform backup log with truncate_only:
BACKUP LOG pubs WITH TRUNCATE_ONLY
-or-
Run this code if you want to keep a backup of your transaction log and keep your transaction log backup sequence intact. See SQL Server Books Online topic "BACKUP" for more information:
BACKUP LOG pubs TO pubslogbackup
Run this code:
DBCC SHRINKFILE(pubs_log,2)
--------------------------------------------------------------------
http://support.microsoft.com/kb/272318
When DBCC SHRINKFILE is run, SQL Server 2000 shrinks the log file by removing as many virtual log files as it can to attempt to reach the target size. If the target file size is not reached, SQL Server places dummy log entries in the last virtual log file until the virtual log is filled and moves the head of the log to the beginning of the file. The following actions are then required to complete the shrinking of the transaction log:
You must run a BACKUP LOG statement to free up space by removing the inactive portion of the log.
You must run DBCC SHRINKFILE again with the desired target size until the log file shrinks to the target size.
The following example demonstrates this with the pubs database and attempts to shrink the pubs_log file to 2 MB:
Run this code:
DBCC SHRINKFILE(pubs_log, 2)
NOTE: If the target size is not reached, proceed to the next step.
Run this code if you want to truncate the transaction log and not keep a backup of the transaction log. Truncate_only invalidates your transaction log backup sequence. Take a full backup of your database after you perform backup log with truncate_only:
BACKUP LOG pubs WITH TRUNCATE_ONLY
-or-
Run this code if you want to keep a backup of your transaction log and keep your transaction log backup sequence intact. See SQL Server Books Online topic "BACKUP" for more information:
BACKUP LOG pubs TO pubslogbackup
Run this code:
DBCC SHRINKFILE(pubs_log,2)
woensdag 26 november 2008
Script permissions on all databases
use master
go
begin
declare @databasename varchar(30)
declare cur cursor for
select name from sysdatabases
create table #result
(dbname varchar(30),result varchar(300))
open cur
fetch next from cur into @databasename
while(@@fetch_status=0)
begin
create table #t
(a1 varchar(50)
,a2 varchar(50)
,a3 varchar(50)
,a4 varchar(50)
,a5 varchar(50)
,a6 varchar(50)
,a7 varchar(50))
insert into #t exec sp_helprotect @username = null
insert into #result select @databasename,a5+' '+a6+' on ['+a1+'].['+a2+']'+
CASE
WHEN (PATINDEX('%All%', a7)=0) and (a7 <> '.')
THEN ' ('+a7+')'
ELSE ''
END+' to ['+a3+']' from #t
drop table #t
fetch next from cur into @databasename
end
select * from #result
close cur
deallocate cur
drop table #result
end
go
------------------------------------
http://www.sqlservercentral.com/scripts/Security/62477/
go
begin
declare @databasename varchar(30)
declare cur cursor for
select name from sysdatabases
create table #result
(dbname varchar(30),result varchar(300))
open cur
fetch next from cur into @databasename
while(@@fetch_status=0)
begin
create table #t
(a1 varchar(50)
,a2 varchar(50)
,a3 varchar(50)
,a4 varchar(50)
,a5 varchar(50)
,a6 varchar(50)
,a7 varchar(50))
insert into #t exec sp_helprotect @username = null
insert into #result select @databasename,a5+' '+a6+' on ['+a1+'].['+a2+']'+
CASE
WHEN (PATINDEX('%All%', a7)=0) and (a7 <> '.')
THEN ' ('+a7+')'
ELSE ''
END+' to ['+a3+']' from #t
drop table #t
fetch next from cur into @databasename
end
select * from #result
close cur
deallocate cur
drop table #result
end
go
------------------------------------
http://www.sqlservercentral.com/scripts/Security/62477/
ACCESSING REMOTE DATA SOURCE (Linked Servers and Ad Hoc Queries)
http://dbdigger.blogspot.com/search/label/Linked%20Servers%20for%20SQL%20Server:
===============================================================================
In some situations a SQL Server DBA is required to fetch data from another data source. Other data source may be another instance of SQL Server or any other RDBMS. Also it may be a file. SQL Server 2005 provides two major ways to access data from other data sources. These are
Linked Servers (may use distributed queries)
Ad hoc queries with openrowset
In following elaboration of using these two ways I will discuss SQL Server as remote data source.
CONFIGURATION AND USAGE OF LINKED SERVERS:
------------------------------------------
Linked servers provide SQL Server with access to remote data sources that may be SQL Server also or any other relational DBMS or text file. Remote data sources are connected through OLE DB provider. OLE DB selection determines that what kind of distributed query operations may be implemented.
Create Linked Server
--------------------:
To create the linked server, use the system stored procedure sp_addlinkedserver. To create a linked server for a SQL Server instance named test on host DBA, we will have following format
EXEC sp_addlinkedserver @server= 'dba\test',
@srvproduct= 'SQL Server'
GO
Or if it is default instance then you may just enter host name instead of host\instance format.
Change Any Property Of Linked Server
------------------------------------:
After a linked server has been created then you may change its several properties like collation compatibility, collation name, connection timeout, data access and query time out etc. For example to change query time out property to 60 seconds for the created linked server, I will use following system stored procedure
EXEC sp_serveroption
@server = 'dba\test' ,
@optname = 'query timeout',
@optvalue = 60
View Meta Data Of Linked Server:
---------------------------------
Now we have a linked server with query time out property changed to 60 seconds.
In order to view the meta data of this linked servers use following script.
SELECT [name], data_source,query_timeout, modify_date
FROM sys.servers
WHERE is_linked = 1
GO
Configure Logins Of Linked Server:
----------------------------------
Now we have a linked server attached. But to access the data on it we have to map proper logins as required. In following script I have mapped the user dba (my local user) to user sa of linked server (remote data source). If I set the values of parameter @locallogin = null then all my local users will be mapped against the user sa of remote data source. If yoy set the parameter @useself = true then windows authentication will be used for connection to linked server. Also we have to provide password for remote user that is sa in this case.
EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'dba\test',
@useself = false ,
@locallogin = 'dba', -- if null Applies to all local logins
@rmtuser = 'sa',
@rmtpassword = 'test'
GO
View Login Information Of Linked Server:
-----------------------------------------
To view the login mapping for remote server use the following script.
SELECT s.name LinkedServerName, ll.remote_name, p.name LocalLoginName
FROM sys.linked_logins ll
INNER JOIN sys.servers s
ON s.server_id = ll.server_id
LEFT OUTER JOIN sys.server_principals p
ON p.principal_id = ll.local_principal_id
WHERE s.is_linked = 1
Go
Executing the Queries
----------------------:
Now linked server is configured and logins are also mapped properly. It is now time to execute remote queries as it is actual purpose of all these configurations. Distributed queries reference one or more linked servers. And perform read or update operations against remote tables, views, or stored procedures. The types of query operations that are supported against linked servers depend on the level of support for transactions present in the OLE DB providers used. The basic syntax for referencing a linked server is using a four-part name. To fetch data from pubs.authors of dba\test server
SELECT * FROM [dba\test].pubs.dbo.authors
GO
To execute a system-stored procedure on the linked server
--------------------------------------------------------:
EXEC [dba\test].master.dbo.sp_monitor
GO
Tired Of Using Four Part Name:
------------------------------
If it is not convenient to use four parts lengthy name then you may create a synonym for whole name.
To create a synonym mySyn for whole four parts name
CREATE SYNONYM testSynonym FOR [dba\test].pubs.dbo.authors
GO
Using OPENQUERY :
-----------------
It is relevant to mention here that SQL Server provides another way to execute distributed queries other than using the four parts naming method. OPENQUERY
is a function that issues a pass-through query against an existing linked server and is referenced in the FROM clause of a query just like a table. The syntax is as follows:
OPENQUERY ( linked_server ,'query' )
GO
WIPE OUT :
---------
Now its time to see that how to drop the craeted objects like linked servers, login mappings and synonyms.
--To drop the mapped login that is in our case dba sp_droplinkedsrvlogin 'dba\test' , 'dba'
GO --To drop the linked server dba\test sp_dropserver 'dba\test'
GO --To directly drop linked server along with all mapped logins
sp_dropserver 'dba\test', 'droplogins'
GO --To drop the synonym mySyn for linked server four parts name
drop synonym testSynonym
GO
Ad Hoc QUERIES USING OPENROWSET :
---------------------------------
In some cases it is not required to retain the connection from linked server for a long time. So to execute such Ad hoc queries OPENROWSET command is used. It is referenced in the FROM clause and acts like a table in a SELECT statement. OPENROWSET creates an ad hoc connection to the data and does not use an existing linked server connection to query the remote data source.
This property to use OPENROWSET to query a remote data source is off by default in SQL Server 2005. You may turn it on through surface area configuration.
USAGE
SELECT *
FROM OPENROWSET('SQLNCLI','dba\test';'sa';'test','SELECT * from pubs..authors')
Go
===============================================================================
In some situations a SQL Server DBA is required to fetch data from another data source. Other data source may be another instance of SQL Server or any other RDBMS. Also it may be a file. SQL Server 2005 provides two major ways to access data from other data sources. These are
Linked Servers (may use distributed queries)
Ad hoc queries with openrowset
In following elaboration of using these two ways I will discuss SQL Server as remote data source.
CONFIGURATION AND USAGE OF LINKED SERVERS:
------------------------------------------
Linked servers provide SQL Server with access to remote data sources that may be SQL Server also or any other relational DBMS or text file. Remote data sources are connected through OLE DB provider. OLE DB selection determines that what kind of distributed query operations may be implemented.
Create Linked Server
--------------------:
To create the linked server, use the system stored procedure sp_addlinkedserver. To create a linked server for a SQL Server instance named test on host DBA, we will have following format
EXEC sp_addlinkedserver @server= 'dba\test',
@srvproduct= 'SQL Server'
GO
Or if it is default instance then you may just enter host name instead of host\instance format.
Change Any Property Of Linked Server
------------------------------------:
After a linked server has been created then you may change its several properties like collation compatibility, collation name, connection timeout, data access and query time out etc. For example to change query time out property to 60 seconds for the created linked server, I will use following system stored procedure
EXEC sp_serveroption
@server = 'dba\test' ,
@optname = 'query timeout',
@optvalue = 60
View Meta Data Of Linked Server:
---------------------------------
Now we have a linked server with query time out property changed to 60 seconds.
In order to view the meta data of this linked servers use following script.
SELECT [name], data_source,query_timeout, modify_date
FROM sys.servers
WHERE is_linked = 1
GO
Configure Logins Of Linked Server:
----------------------------------
Now we have a linked server attached. But to access the data on it we have to map proper logins as required. In following script I have mapped the user dba (my local user) to user sa of linked server (remote data source). If I set the values of parameter @locallogin = null then all my local users will be mapped against the user sa of remote data source. If yoy set the parameter @useself = true then windows authentication will be used for connection to linked server. Also we have to provide password for remote user that is sa in this case.
EXEC sp_addlinkedsrvlogin
@rmtsrvname = 'dba\test',
@useself = false ,
@locallogin = 'dba', -- if null Applies to all local logins
@rmtuser = 'sa',
@rmtpassword = 'test'
GO
View Login Information Of Linked Server:
-----------------------------------------
To view the login mapping for remote server use the following script.
SELECT s.name LinkedServerName, ll.remote_name, p.name LocalLoginName
FROM sys.linked_logins ll
INNER JOIN sys.servers s
ON s.server_id = ll.server_id
LEFT OUTER JOIN sys.server_principals p
ON p.principal_id = ll.local_principal_id
WHERE s.is_linked = 1
Go
Executing the Queries
----------------------:
Now linked server is configured and logins are also mapped properly. It is now time to execute remote queries as it is actual purpose of all these configurations. Distributed queries reference one or more linked servers. And perform read or update operations against remote tables, views, or stored procedures. The types of query operations that are supported against linked servers depend on the level of support for transactions present in the OLE DB providers used. The basic syntax for referencing a linked server is using a four-part name. To fetch data from pubs.authors of dba\test server
SELECT * FROM [dba\test].pubs.dbo.authors
GO
To execute a system-stored procedure on the linked server
--------------------------------------------------------:
EXEC [dba\test].master.dbo.sp_monitor
GO
Tired Of Using Four Part Name:
------------------------------
If it is not convenient to use four parts lengthy name then you may create a synonym for whole name.
To create a synonym mySyn for whole four parts name
CREATE SYNONYM testSynonym FOR [dba\test].pubs.dbo.authors
GO
Using OPENQUERY :
-----------------
It is relevant to mention here that SQL Server provides another way to execute distributed queries other than using the four parts naming method. OPENQUERY
is a function that issues a pass-through query against an existing linked server and is referenced in the FROM clause of a query just like a table. The syntax is as follows:
OPENQUERY ( linked_server ,'query' )
GO
WIPE OUT :
---------
Now its time to see that how to drop the craeted objects like linked servers, login mappings and synonyms.
--To drop the mapped login that is in our case dba sp_droplinkedsrvlogin 'dba\test' , 'dba'
GO --To drop the linked server dba\test sp_dropserver 'dba\test'
GO --To directly drop linked server along with all mapped logins
sp_dropserver 'dba\test', 'droplogins'
GO --To drop the synonym mySyn for linked server four parts name
drop synonym testSynonym
GO
Ad Hoc QUERIES USING OPENROWSET :
---------------------------------
In some cases it is not required to retain the connection from linked server for a long time. So to execute such Ad hoc queries OPENROWSET command is used. It is referenced in the FROM clause and acts like a table in a SELECT statement. OPENROWSET creates an ad hoc connection to the data and does not use an existing linked server connection to query the remote data source.
This property to use OPENROWSET to query a remote data source is off by default in SQL Server 2005. You may turn it on through surface area configuration.
USAGE
SELECT *
FROM OPENROWSET('SQLNCLI','dba\test';'sa';'test','SELECT * from pubs..authors')
Go
Abonneren op:
Posts (Atom)