Showing posts with label Sql Server. Show all posts
Showing posts with label Sql Server. Show all posts

Wednesday, September 29, 2010

What exactly is a trusted connection?

Trusted connections are only opened by the SQL Server client software (such
as the ADO.NET, OLE DB, or ODBC provider/driver) if you request a Windows
Authentication (used to be called Integrated Security) connection. The SQL
Server client software then opens a specific kind of network connection that
can only be opened by a process that has already successfully logged on to
Windows using a valid Windows login and password. Since those types of
connections are only possible after Windows has validated the login and
password, the Database Engine software does not have to re-validate the
login and password, it can "trust" that authentication was already performed
by Windows. The security ID of the Windows account is passed as part of the
information concerning the connection.


The above just controls whether the you can open a connection to the
database engine (authentication). After you have connected, you can only
perform actions for which the proper permissions have been assigned to
either your login or any user your login has been mapped to. Part of what a
DBA has to do in Windows Authentication environments is define to the
Databae Engine which Windows accounts and groups used as logins map to
different users in each database, and which permissions are granted to each
user or login.


--
Alan Brewer [MSFT]
SQL Server Documentation Team

Source:
http://dbaspot.com/forums/ms-sqlserver/142379-what-exactly-trusted-connection.html

Saturday, May 2, 2009

Find Tables With Foreign Key Constraint in Database

SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id

/*** Script all Foreign Key Constraints ***/
/*** The Result Set can be used to copy constraints to your testing DB or to keep on hand in case of errors. ***/
SELECT
‘ALTER TABLE ‘+FK.TABLE_NAME+
‘ ADD CONSTRAINT ‘+C.CONSTRAINT_NAME+’ FOREIGN KEY’+
‘(’+CU.COLUMN_NAME+’) ‘+
‘REFERENCES ‘+PK.TABLE_NAME+
‘(’+PT.COLUMN_NAME+’)’ ForeignKeyScripts
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = ‘PRIMARY KEY’
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
–WHERE PK.TABLE_NAME IN (’Table1′, ‘Table2′)
–WHERE FK.TABLE_NAME IN (’Table1′, ‘Table2′)


Thursday, April 30, 2009

Some Useful T-SQL DateTime Functions

Example:

----First Day of Last Month
SELECT DATEADD(mm,-1,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0))

----Last Day of Last Month
SELECT DATEADD(ms,-3,DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)))