Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
db:mssql [2021/09/16 20:59] – [MSSQL. Docker] admin | db:mssql [2021/11/03 09:53] (current) – admin | ||
---|---|---|---|
Line 2: | Line 2: | ||
===== MSSQL. Info ===== | ===== MSSQL. Info ===== | ||
+ | * https:// | ||
+ | * mssql video traning http:// | ||
+ | * https:// | ||
* https:// | * https:// | ||
- | * https://habr.com/ru/post/316064/ - информация по mssql | + | * https://docs.microsoft.com/ru-ru/sql/ssms/download-sql-server-management-studio-ssms? |
* https:// | * https:// | ||
* [[https:// | * [[https:// | ||
* mssql update pack https:// | * mssql update pack https:// | ||
- | * mssql video traning http:// | ||
* sp_command help https:// | * sp_command help https:// | ||
Line 16: | Line 17: | ||
sudo docker run -e " | sudo docker run -e " | ||
-p 1434:1433 --name sql1 -h sql1 -v / | -p 1434:1433 --name sql1 -h sql1 -v / | ||
+ | -v / | ||
-d mcr.microsoft.com/ | -d mcr.microsoft.com/ | ||
</ | </ | ||
Line 59: | Line 61: | ||
<code SQL> | <code SQL> | ||
-- # Backup Restore/ | -- # Backup Restore/ | ||
+ | |||
+ | -- get backup set | ||
+ | SELECT TOP 100 | ||
+ | name, | ||
+ | type, * | ||
+ | FROM | ||
+ | msdb..backupset | ||
+ | WHERE 1=1 | ||
+ | -- and database_name like ' | ||
+ | ORDER BY | ||
+ | backup_set_id DESC | ||
-- delete database | -- delete database | ||
Line 73: | Line 86: | ||
# backup full | # backup full | ||
- | /* | + | /* */ |
USE WAdmIsk; | USE WAdmIsk; | ||
BACKUP DATABASE WAdmIsk TO DISK = ' | BACKUP DATABASE WAdmIsk TO DISK = ' | ||
Line 109: | Line 122: | ||
</ | </ | ||
++++ | ++++ | ||
+ | |||
+ | |||
+ | ++++ --- MSSQL. turn on agent| | ||
+ | <code SQL> | ||
+ | -- Get data for mssql | ||
+ | sp_configure 'show advanced options', | ||
+ | GO | ||
+ | RECONFIGURE; | ||
+ | GO | ||
+ | sp_configure 'Agent XPs', 1; | ||
+ | GO | ||
+ | RECONFIGURE | ||
+ | GO | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ++++ --- SCRIPT - for archive all files except ZIP| | ||
+ | <code BASH> | ||
+ | Function Zip | ||
+ | { | ||
+ | Param | ||
+ | ( | ||
+ | [string[]]$toBeZipped | ||
+ | ) | ||
+ | if ($null -eq $toBeZipped) {return} | ||
+ | $CurDir = Get-Location | ||
+ | Set-Location " | ||
+ | foreach ( $f in $toBeZipped) | ||
+ | { | ||
+ | echo " | ||
+ | | ||
+ | } | ||
+ | Set-Location $CurDir | ||
+ | } | ||
+ | |||
+ | |||
+ | Function ZipFiles | ||
+ | { | ||
+ | Param | ||
+ | ( | ||
+ | [string]$zipFile | ||
+ | , | ||
+ | [string[]]$toBeZipped | ||
+ | ) | ||
+ | $CurDir = Get-Location | ||
+ | Set-Location " | ||
+ | .\7z.exe A -tzip $zipFile $toBeZipped | Out-Null | ||
+ | Set-Location $CurDir | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | # Code | ||
+ | # get-childitem $path -recurse -exclude *.cs, | ||
+ | $bckpdir=$args[0] | ||
+ | if ($null -eq $bckpdir){ | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | $CheckFile = Test-Path -Path $bckpdir | ||
+ | |||
+ | |||
+ | if (!($CheckFile)) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | echo " | ||
+ | |||
+ | |||
+ | |||
+ | $files = Get-ChildItem $bckpdir\*.* -exclude *.zip | Where-Object {$_.CreationTime -lt ((Get-date).adddays(+1))} | ||
+ | Zip $files | ||
+ | foreach ( $f in $files) | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | echo " | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ++++ --- SCRIPT - for archive ZIP all files except last one for sort in time| | ||
+ | <code BASH> | ||
+ | Function Zip | ||
+ | { | ||
+ | Param | ||
+ | ( | ||
+ | [string[]]$toBeZipped | ||
+ | ) | ||
+ | if ($null -eq $toBeZipped) {return} | ||
+ | $CurDir = Get-Location | ||
+ | Set-Location " | ||
+ | foreach ( $f in $toBeZipped) | ||
+ | { | ||
+ | echo " | ||
+ | | ||
+ | } | ||
+ | Set-Location $CurDir | ||
+ | } | ||
+ | |||
+ | |||
+ | Function ZipFiles | ||
+ | { | ||
+ | Param | ||
+ | ( | ||
+ | [string]$zipFile | ||
+ | , | ||
+ | [string[]]$toBeZipped | ||
+ | ) | ||
+ | $CurDir = Get-Location | ||
+ | Set-Location " | ||
+ | .\7z.exe A -tzip $zipFile $toBeZipped | Out-Null | ||
+ | Set-Location $CurDir | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | # Code | ||
+ | # get-childitem $path -recurse -exclude *.cs, | ||
+ | $bckpdir=$args[0] | ||
+ | if ($null -eq $bckpdir){ | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | $CheckFile = Test-Path -Path $bckpdir | ||
+ | |||
+ | |||
+ | if (!($CheckFile)) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | echo " | ||
+ | |||
+ | |||
+ | |||
+ | $files = Get-ChildItem $bckpdir\*.* -exclude *.zip | sort -Property CreationTime | ||
+ | |||
+ | if ($files -is [array]) | ||
+ | { | ||
+ | $files = $files[0..($files.Count - 2)] | ||
+ | Zip $files | ||
+ | foreach ( $f in $files) | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | | ||
+ | echo " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | echo " | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
+ | ++++ --- SCRIPT - clear age files in directory | | ||
+ | <code BASH> | ||
+ | $bckpdir=$args[0] | ||
+ | if ($null -eq $bckpdir){ | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | $numdays=$args[1] | ||
+ | if ($null -eq $numdays -or -not $numdays -match ' | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | |||
+ | |||
+ | $CheckFile = Test-Path -Path $bckpdir | ||
+ | |||
+ | |||
+ | if (!($CheckFile)) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | } | ||
+ | echo " | ||
+ | |||
+ | $files = Get-ChildItem –Path $bckpdir | Where-Object {($_.CreationTime -lt (Get-Date).AddDays(-$numdays))} | ||
+ | if ($null -eq $files){ | ||
+ | echo " | ||
+ | exit 0 | ||
+ | } | ||
+ | echo $($files| Format-Table | ||
+ | $files | Remove-Item | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | |||
===== MSSQL.bulk copy to file ===== | ===== MSSQL.bulk copy to file ===== | ||
Line 203: | Line 418: | ||
https:// | https:// | ||
+ | ===== MSSQL. syncronize tables ===== | ||
+ | |||
+ | ++++Check syncrhonize| | ||
+ | <code SQL> | ||
+ | |||
+ | -- Sector | ||
+ | select ' | ||
+ | year(case when c.date is null then co.date else c.date end) as year, | ||
+ | count(*) as cnt, | ||
+ | sum(case when co.id is null then 1 else 0 end) as cntForLoad | ||
+ | from ISOJ2018.dbo.cAppeal c | ||
+ | full outer join isoj.dbo.cAppeal co on co.id=c.id | ||
+ | -- where co.id is null | ||
+ | group by rollup(year(case when c.date is null then co.date else c.date end) | ||
+ | ); | ||
+ | </ | ||
+ | ++++ | ||
+ | |||
+ | ++++ Syncronize tables| | ||
+ | <code SQL> | ||
+ | USE ISOJ; | ||
+ | --- synchronize tables | ||
+ | |||
+ | DECLARE @DATABASE NVARCHAR(255) | ||
+ | DECLARE @TABLE NVARCHAR(255) | ||
+ | DECLARE @cmd NVARCHAR(1000) | ||
+ | |||
+ | |||
+ | -- ' | ||
+ | | ||
+ | | ||
+ | FROM [ISOJ].INFORMATION_SCHEMA.TABLES | ||
+ | WHERE table_type = 'BASE TABLE' | ||
+ | |||
+ | OPEN DatabaseCursor | ||
+ | |||
+ | FETCH NEXT FROM DatabaseCursor INTO @TABLE | ||
+ | WHILE @@FETCH_STATUS = 0 | ||
+ | BEGIN | ||
+ | FETCH NEXT FROM DatabaseCursor INTO @TABLE | ||
+ | |||
+ | --set @TABLE = ' | ||
+ | |||
+ | SET @cmd = ' | ||
+ | INSERT INTO [isoj].[dbo].[' | ||
+ | select top 1000000 * | ||
+ | from ISOJ2018.[dbo].[' | ||
+ | where c.id in ( | ||
+ | select c.id | ||
+ | from ISOJ2018.[dbo].[' | ||
+ | left join isoj.[dbo].[' | ||
+ | where co.id is null | ||
+ | ) | ||
+ | order by c.DateModified asc; | ||
+ | ' | ||
+ | |||
+ | BEGIN TRY | ||
+ | |||
+ | PRINT @TABLE -- uncomment if you want to see commands | ||
+ | EXEC (@cmd) | ||
+ | END TRY | ||
+ | BEGIN CATCH | ||
+ | PRINT ' | ||
+ | PRINT @cmd | ||
+ | PRINT ERROR_MESSAGE() | ||
+ | PRINT ' | ||
+ | END CATCH | ||
+ | |||
+ | | ||
+ | CLOSE DatabaseCursor | ||
+ | DEALLOCATE DatabaseCursor | ||
+ | |||
+ | GO | ||
+ | </ | ||
+ | ++++ | ||
===== TSQL example ===== | ===== TSQL example ===== | ||
++++ TSQL example| | ++++ TSQL example| |