CREATE TABLE [dbo].[Orders]( [ID] [int] IDENTITY(1,1) NOT NULL, [OrderDate] [date] NOT NULL, [CustomID] [int] NOT NULL, [Title] [nchar](50) NOT NULL ) ON [PRIMARY]
BEGIN TRAN declare @currentMaxID int declare @newID int select @currentMaxID = max(CustomID) from [dbo].[Orders] WITH (UPDLOCK) where year(OrderDate) = year(getdate()) and month(OrderDate) = month(getdate()) if @currentMaxID is null begin set @currentMaxID = 0 end set @newID = @currentMaxID + 1 insert [dbo].[Orders] (OrderDate, CustomID, Title) values (getdate(), @newID, 'my title') COMMIT
declare @dbname sysname declare @sql nvarchar(1000) declare db_cursor cursor for select name from master.dbo.sysdatabases where name not in ('tempdb', 'model', 'msdb', 'master') open db_cursor fetch next from db_cursor into @dbname while @@fetch_status = 0 begin print @dbname select @sql = ' ALTER DATABASE [' + @dbname + '] SET RECOVERY SIMPLE DBCC SHRINKDATABASE ([' + @dbname + ']) ALTER DATABASE [' + @dbname + '] SET RECOVERY FULL' exec sp_executesql @sql fetch next from db_cursor into @dbname end close db_cursor deallocate db_cursor
sqlcmd -Q "CREATE LOGIN [domain\user] from windows" sqlcmd -Q "EXEC sys.sp_addsrvrolemember @loginame = N'domain\user', @rolename = N'sysadmin'"
select * from sys.sysprocesses where dbid = DB_ID('WSS_Content')
declare @dbname sysname declare @basepath sysname declare @filename sysname declare @filedate varchar(8) set @basepath = 'c:\Backup\' select @filedate = convert(varchar(8), getdate(), 112) declare db_cursor cursor for select name from master.dbo.sysdatabases where name not in ('tempdb', 'model', 'msdb', 'master') open db_cursor fetch next from db_cursor into @dbname while @@fetch_status = 0 begin set @filename = @basepath + @filedate + '-' + @dbname + '.bak' print @filename backup database @dbname to disk = @filename with compression fetch next from db_cursor into @dbname end close db_cursor deallocate db_cursor