USE master
GO
ALTER DATABASE @dbName
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE @dbName
SET ONLINE
OR simply
Right Click database -> Tasks -> Take database offline
USE master
GO
ALTER DATABASE @dbName
SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE @dbName
SET ONLINE
IF NOT EXISTS (SELECT name FROM sysusers WHERE issqlrole=1 AND name = 'RestrictedRole')
exec sp_addrole 'RestrictedRole', 'dbo';
IF NOT EXISTS (SELECT name FROM sysusers WHERE issqlrole=1 AND name = 'LessRestrictedRole')
exec sp_addrole 'LessRestrictedRole', 'dbo';
GRANT SELECT, INSERT, UPDATE on Table1 to RestrictedRole;
GRANT SELECT on Table2 to RestrictedRole;
EXEC sp_addrolemember 'RestrictedRole', 'LessRestrictedRole';
GRANT INSERT, UPDATE on Table2 to LessRestrictedRole;
To create a new Database Schema:
CREATE SCHEMA NewSchema
GO
To create a new Database Schema only if it does not exist:
IF NOT EXISTS (SELECT * FROM sys.schemas
WHERE name ='NewSchema')
EXEC dbo.sp_executesql @statement=N'
CREATE SCHEMA NewSchema';
GO
To change a table's Schema:
ALTER SCHEMA NewSchema TRANSFER OldSchema.TableName;
--default schema is dbo then:
ALTER SCHEMA NewSchema TRANSFER dbo.TableName;
DECLARE @Role varchar(20) = 'UserRole'
IF NOT EXISTS (SELECT name FROM sysusers
WHERE issqlrole=1 AND name = @Role)
EXEC sp_addrole @Role; --EXEC sp_addrole @Role, 'dbo'
SELECT * FROM Table WHERE Column LIKE '_a_'The above code will generate results that has any one character before and after 'a' such as:
SELECT * FROM Table WHERE Column LIKE '%[_]a[_]%'The above code will generate results such as: