Posts

Check progress of SQL Server restore

From MSSQLTips : SELECT session_id as SPID , command , a . text AS Query , start_time , percent_complete , dateadd ( second , estimated_completion_time / 1000 , getdate ( ) ) as estimated_completion_time FROM sys . dm_exec_requests r CROSS APPLY sys . dm_exec_sql_text ( r . sql_handle ) a WHERE r . command in ( 'BACKUP DATABASE' , 'RESTORE DATABASE' )

Simple SQL Server backup/restore

-- Back Up --------------------------------------------- declare @filename nvarchar ( 255 ) = 'C:\backups\' + format ( sysutcdatetime ( ) , 'yyyyMMddHHmmss' ) + '.bak' ; backup database MyDB to disk = @filename ; go -- Restore --------------------------------------------- -- Find the logical names - result will be the MDF and LDF -- logical names: restore filelistonly from disk = 'C:\backups\20201103130743.bak' ; go restore database DifferentDB from disk = 'C:\backups\20201103130743.bak' with replace , move '**MDF Logical Name**' to 'c:\data\DifferentDB.mdf' , move '**LDF Logical Name**' to 'c:\data\DifferentDB_log.ldf' ; go

NodeJS Read/Write Files

const fs = require ( " fs " ) ; const readline = require ( " readline " ) ; // Read full file fs . readFile ( " myfile.txt " , { encoding : " utf-8 " } , ( err , data ) = > { console . log ( " Full file contents: " ) ; console . log ( data ) ; console . log ( " =================== " ) ; } ) ; // Read file line-by-line var lineReader = readline . createInterface ( { input : fs . createReadStream ( " myfile.txt " ) } ) ; lineReader . on ( " line " , line = > { console . log ( ` Line: ${ line } ` ) ; } ) ; lineReader . on ( " close " , ( ) = > console . log ( " DONE " ) ) ; // Write full file fs . writeFile ( " myfile.txt " , " some file contents " , err = > { if ( err ) console . error ( err ) ; } ) ; // Write file line-by-line var outputStream = fs . createWriteStream ( " myfil

NodeJS read/write text files

" use strict " ; ( function ( ) { var fs = require ( " fs " ) ; var readline = require ( " readline " ) ; fs . readFile ( " input.json " , function ( error , data ) { var obj = JSON . parse ( data ) ; fs . writeFile ( " output.json " , JSON . stringify ( obj ) , function ( err ) { if ( err ) { console . log ( " Error: " + err ) ; } else { console . log ( " Saved " ) ; } } ) ; } ) ; ( function ( ) { var outputStream = fs . createWriteStream ( " output.txt " ) ; var lineReader = readline . createInterface ( { input : fs . createReadStream ( " input.txt " ) } ) ; lineReader . on ( " line " , function ( line ) { console . log ( " Line: " + l

Modify web.config appsettings value

var config = WebConfigurationManager . OpenWebConfiguration ( " ~ " ) ; var section = ( AppSettingsSection ) config . GetSection ( " appSettings " ) ; section . Settings [ " something " ] . Value = " newValue " ; config . Save ( ) ;

SQL Server Identity Info

WITH cte AS ( SELECT s . name + '.' + t . name AS TableName , c . name AS ColumnName FROM sys . tables t INNER JOIN sys . schemas s ON t . schema_id = s . schema_id INNER JOIN sys . columns c ON c . object_id = t . object_id AND c . is_identity = 1 ) SELECT TableName , ColumnName , IDENT_SEED ( TableName ) AS Seed , IDENT_INCR ( TableName ) AS Increment , IDENT_CURRENT ( TableName ) AS LastIdentity FROM cte order by TableName

SQL Server basic maintenance

Refresh Views: create procedure util . RefreshViews as begin declare @msg nvarchar ( 1000 ) ; set @msg = '------------- Executing RefreshViews -------------' ; raiserror ( @msg , 0 , 1 ) with nowait ; set nocount on ; declare @viewNames table ( FullName nvarchar ( 261 ) -- '[' + 128 + '].[' + 128 + ']' ) ; insert @viewNames ( FullName ) select '[' + object_schema_name ( [ object_id ] ) + '].[' + [ name ] + ']' from sys . views ; declare @fullName nvarchar ( 261 ) ; select top 1 @fullName = FullName from @viewNames order by FullName ; while exists ( select 1 from @viewNames ) begin set @msg = convert ( varchar , getutcdate ( ) , 127 ) + ' Working on ' + @fullName ; raiserror ( @msg , 0 , 1 ) with nowait ; begin try exec ( 'sp_refres