Table of Contents

Mssql

MSSQL. Info

MSSQL. Docker

Docker mssql Mssql docker

 # пробросить mnt/volume для WSL и жестких дисков + нестандартный порт
sudo docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=passw0rd_123" \
 -p 1434:1433 --name sql1 -h sql1 -v /mnt:/mnt/volume \
 -v /opt/docker/mssql/isoj:/var/opt/mssql/data
 -d mcr.microsoft.com/mssql/server:2019-latest

Пример работы с подключением из командной строки и выполнения восстановления

sudo docker exec -it sql3 "bash"
/opt/mssql-tools/bin/sqlcmd -S 172.31.28.155,1434 -U SA -P "passw0rd_123"
 
-- включить mssql agent
sp_configure 'show advanced options', 1;  
GO  
RECONFIGURE;  
GO  
sp_configure 'Agent XPs', 1;  
GO  
RECONFIGURE  
GO  

MSSQL. SQL administration

--- MSSQL. Sql to Get info data of base , space used, files

--- MSSQL. Backup Restore

--- MSSQL. turn on agent

--- SCRIPT - for archive all files except ZIP

--- SCRIPT - for archive ZIP all files except last one for sort in time

--- SCRIPT - clear age files in directory

MSSQL.bulk copy to file

bcp [ISOJ].[dbo].[lNLogData] OUT i:\lNLogData.txt  -T -c -b 10000

MSSQL. Shrink Files, Reclaim Space

https://stackoverflow.com/questions/56628/how-do-you-clear-the-sql-server-transaction-log

USE ISOJ
GO
 
DECLARE @FileName sysname = N'ISOJ';
DECLARE @TargetSize INT = (SELECT 1 + SIZE*8./1024 FROM sys.database_files WHERE name = @FileName);
DECLARE @Factor FLOAT = .995;
DECLARE @Inc INT = 0
 
WHILE @Inc < 10
BEGIN
    SET @Inc += 1
    SET @TargetSize *= @Factor;
    DBCC SHRINKFILE(@FileName, @TargetSize);
    DECLARE @msg VARCHAR(200) = 'Shrink file completed. Target Size: '+ 
         CAST(@TargetSize AS VARCHAR )+ ' MB. Timestamp: ';
    RAISERROR(@msg, 1, 1) WITH NOWAIT;
    WAITFOR DELAY '00:00:01';
END;
 
 
-- reclail logs
USE yourdb;
GO
CHECKPOINT;
GO
CHECKPOINT; -- run twice to ensure file wrap-around
GO
DBCC SHRINKFILE(yourdb_log, 200); -- unit is set in MBs
GO

MSSQL rebuild all indexes in database

-- Short way 
EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD'

Rebuild indexes

MSSQL. trace

If trace skip rows , need to do trace on server side
https://www.sqlservercentral.com/blogs/setting-up-a-server-side-trace

MSSQL. syncronize tables

Check syncrhonize

Syncronize tables

TSQL example

TSQL example