Skip to content

Instantly share code, notes, and snippets.

View multithread2.ps1
begin {
$SqlInstance = Connect-DbaInstance -SqlInstance "MyServer";
}
process {
$SqlInstance = "MyServer";
$SourceDb = "MySourceDb";
$TargetDb = "MyTargetTb";
$TableList = @("Table1","Table2","Table3","Table4");
foreach ($Table in $TableList) {
Start-ThreadJob -ThrottleLimit 2 -ArgumentList $SqlInstance, $SourceDb, $TargetDb, $Table -ScriptBlock {
View multithread1.ps1
process {
$SqlInstance = "MyServer";
$SourceDb = "MySourceDb";
$TargetDb = "MyTargetTb";
$TableList = @("Table1","Table2","Table3","Table4");
foreach ($Table in $TableList) {
Start-ThreadJob -ThrottleLimit 4 -ArgumentList $SqlInstance, $SourceDb, $TargetDb, $Table -ScriptBlock {
param($SqlInstance,
$Source
$Destination
View Restore with users2.ps1
Invoke-DbaQuery -SqlInstance $DstInstance -Database $DBName -file $DstUserScriptPath;
Write-Output "Users in $DstInstance.$DBName after correcting"
Get-DbaDbUser -sqlinstance $DstInstance -Database $DBName | format-table -autosize -Property SqlInstance, Database, Name, Login;
Write-Output "Orphan users in $DstInstance.$DBName after correcting";
Get-DbaDbOrphanUser -sqlinstance $DstInstance -Database $DBName | format-table -autosize -property SqlInstance, DatabaseName, User;
View Restore with users1.ps1
$RestoreResult = Restore-DbaDatabase -SqlInstance $DstInstance -Database $DBName -Path $Sql17Backup.FullName -WithReplace;
Write-Output "Users in $DstInstance.$DBName before correcting";
Get-DbaDbUser -sqlinstance $DstInstance -Database $DBName | format-table -autosize -Property SqlInstance, Database, Name, Login;
Write-Output "Orphan users in $DstInstance.$DBName before correcting";
Get-DbaDbOrphanUser -sqlinstance $DstInstance -Database $DBName | format-table -autosize -property SqlInstance, DatabaseName, User;
View Backup DBs and Users.ps1
import-module dbatools;
$SrcInstance = "flexo\sql17";
$DstInstance = "flexo\sql19";
$UserPassword = 'P@$$w0rd' | ConvertTo-SecureString -AsPlainText -Force;
$DBName = "UserPermTest";
$DstUserScriptPath = "C:\sql\Backup\Sql19Users.sql";
# Backup the databases
$Sql17Backup = Backup-DbaDatabase -SqlInstance $SrcInstance -Database $DBName -Path 'C:\sql\Backup\FLEXO$SQL17';
$Sql19Backup = Backup-DbaDatabase -SqlInstance $DstInstance -Database $DBName -Path 'C:\sql\Backup\FLEXO$SQL19';
View Copy-DBWithPermissionsSaved1.ps1
import-module dbatools;
$SrcInstance = "flexo\sql17";
$DstInstance = "flexo\sql19";
$UserPassword = 'P@$$w0rd' | ConvertTo-SecureString -AsPlainText -Force;
$DBName = "UserPermTest";
$DstUserScriptPath = "C:\sql\Backup\Sql19Users.sql";
# Create database & users on each
Remove-DbaDatabase -SqlInstance $SrcInstance, $DstInstance -Database $DBName -Confirm:$false | Out-Null;
New-DbaDatabase -SqlInstance $SrcInstance, $DstInstance -Name $DBName -Confirm:$false | Out-Null;
View UDT 5.sql
create table #StringTemp3
(
TempMyString testing.dbo.MyStringType not null
);
View UDT 4.sql
use testing;
go
create table #StringTemp2
(
TempMyString MyStringType not null
);
View UDT 3.sql
use tempdb;
go
-- Get the full name of the table
select *
from tempdb.sys.tables
GO
-- Now get the definition
sp_help #StringTemp_________________________________________________________________________________________________________00000000081A
go
drop table #StringTemp;
View UDT2.sql
insert into TestMyString
(ThisIsMyString)
values
('Hello there');
go
select *
into #StringTemp
from TestMyString;
go
select *