| SELECT |
Adding fuel to my passion on SQL Server
| SELECT |
| SELECT |
select |
| DECLARE @SearchPhrase nvarchar(1000) |
1: SELECT destination_database_name,max(restore_date) as restore_date
2: FROM msdb.dbo.restorehistory
3: WHERE destination_database_name = 'testDB’ ---- Place your database name here
4: GROUP BY destination_database_name
The result will look like;
destination_database_name restore_date
testDB 2013-04-29 11:49:19.940
It would be useful to cross check, when the Linked Server remote login password has changed.
In this case, linked server will wail due to wrong password. To fix this we have reset the correct password.
| SELECT s.server_id,S.name,S.product,S.provider,l.remote_name,'Remote_Login_Modified_Dt'=l.modify_date,s.modify_date |
Before you run the following query, replace <database_name> and <schema_name.object_name> with valid names.
| USE <database_name>; |
Reference: http://msdn.microsoft.com/en-us/library/ms190324.aspx
Before you run the following query, replace <database_name> and <n_days> with valid values.
| USE <database_name>; |
Source: http://msdn.microsoft.com/en-us/library/ms190324.aspx
Users and schemas are completely separate. The behavior of schemas changed in SQL Server 2005. Schemas are no longer equivalent to database users;A schema is simply a container of objects. Schemas own objects and principles own schemas.
A schema can be owned by any user, and its ownership is transferable.
Why Schema introduced on SQL Server?
The Users and Schemas separation means objects and schemas can be created before users are added to the database. It also means a user can be dropped without specifically dropping the objects owned by that user. A schema can only be owned by one user at a time, but a single user can simultaneously own many schemas.
How to transfer Schema ownership?
The following example transfers ownership of the schema LabProduction to user Sivaprasad.
ALTER AUTHORIZATION ON SCHEMA::LabProduction TO Sivaprasad;
GO
Changes the ownership of a securable – using ALTER AUTHORIZATION
ALTER AUTHORIZATION can be used to change the ownership of any entity that has an owner. Ownership of database-contained entities can be transferred to any database-level principal. Ownership of server-level entities can be transferred only to server-level principals
How to move objects between Schema?
ALTER SCHEMA can only be used to move securables between schemas in the same database. To change or drop a securable within a schema, use the ALTER or DROP statement specific to that securable.
For example, below modifies the schema HumanResources by transferring the table Address from schema Person into the schema
ALTER SCHEMA HumanResources TRANSFER Person.Address;
GO
| -- Create TestDB Database -- Change the DB Context to TestDB -- Created Schema Student --– Created table in Student schema – --– Assign Permission to Schema |
Are you curious to find the Database Backup / Restore status?
Do you have to find out the status the DBCC CHECKDB ?
Do you have to report when the ROLLBACK would finish ?
Do you want judge how long the Database Shrink will take?
Here is the answer!
The query listed below can be used to find the Percentage of work completed for the following commands:
We have to replace the where condition filter based on the requirement.
| SELECT command, |
The following steps show the processing order for a SELECT statement.
FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
This is the reason where we can not use column alias on Where clause, however we can use it on Select / Order by.
If interested on the logical order of the query processing poster, download it from here.
Reference: http://msdn.microsoft.com/en-us/library/ms189499.aspx
Below is the TSQL Query to find when the database last accessed for SQL Server 2005 / 2008
-- Query to find when the database last accessed on SQL Server 2005 / 2008 --- select d.name, x1 = (select X1= max(bb.xx) from ( select xx = max(last_user_seek) where max(last_user_seek) is not null union all select xx = max(last_user_scan) where max(last_user_scan) is not null union all select xx = max(last_user_lookup) where max(last_user_lookup) is not null union all select xx = max(last_user_update) where max(last_user_update) is not null) bb) FROM master.dbo.sysdatabases d left outer join sys.dm_db_index_usage_stats s on d.dbid= s.database_id group by d.name |
Format the date or time without dividing characters, as well as concatenate the date and time string:
Sample statement | Output |
select replace(convert(varchar, getdate(),111),'/','') | 20100413 |
select replace(convert(varchar, getdate(),101),'/','') + replace(convert(varchar, getdate(),108),':','') | 20100413 004426 |
Select cast(int, convert(char,getdate(),111)) | 20100413 |