-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlterCollation.ps1
157 lines (141 loc) · 4.91 KB
/
AlterCollation.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# load .NET SMO assembly
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$Server = 'REDALERT'
$Database = 'AdventureWorks2017'
$FileDropFK = 'C:\Temp\01.DropFK.sql'
$FileAlter = 'C:\Temp\02.Alter.sql'
$FileCreateFK = 'C:\Temp\03.CreateFK.sql'
$Collate = 'Cyrillic_General_CI_AS'
function WriteScript()
{
Param([ref]$srv,[ref]$table,[ref]$row)
$scripter = New-Object('Microsoft.SqlServer.Management.Smo.Scripter') $srv.value
$scripter.Options.Indexes = $true
$scripter.Options.ClusteredIndexes = $true
$scripter.Options.ScriptBatchTerminator = $true
$scripter.Options.NoCommandTerminator = $false
$scripter.Options.ToFileOnly = $false
foreach ($t in $db.Tables)
{
if ($t.Name -eq $row.Value['table'] -and $t.Schema -eq $row.Value['schema'])
{
'/********************************************************************/' | Out-File $FileAlter -Append
# set drop option
$scripter.Options.ScriptDrops = $true
# drop indexes
foreach ($v in $t.Indexes)
{
if ($v.IndexedColumns.Contains($row.Value['column']))
{
$scripter.Script($v) | Out-File $FileAlter -Append
'go' | Out-File $FileAlter -Append
}
}
# drop foreign keys
foreach ($v in $t.ForeignKeys )
{
if ($v.Columns.Contains($row.Value['column']))
{
$scripter.Script($v) | Out-File $FileDropFK -Append
'go' | Out-File $FileDropFK -Append
}
}
$row.Value['alter'] | Out-File $FileAlter -Append
'go' | Out-File $FileAlter -Append
# set create option
$scripter.Options.ScriptDrops = $false
# create indexes
foreach ($v in $t.Indexes)
{
if ($v.IndexedColumns.Contains($row.Value['column']))
{
$q = 'schema = ''' + $table.Value.Schema + ''' and table = ''' + $table.Value.Name + ''' and RowNum > ' + $row.Value['RowNum']
if ($table.Value.select($q).Length -eq 0)
{
$scripter.Script($v) | Out-File $FileAlter -Append
'go' | Out-File $FileAlter -Append
}
}
}
# create foreign keys
foreach ($v in $t.ForeignKeys)
{
if ($v.Columns.Contains($row.Value['column']))
{
$q = 'schema = ''' + $table.Value.Schema + ''' and table = ''' + $table.Value.Name + ''' and RowNum > ' + $row.Value['RowNum']
if ($table.Value.select($q).Length -eq 0)
{
$scripter.Script($v) | Out-File $FileCreateFK -Append
'go' | Out-File $FileCreateFK -Append
}
}
}
}
}
}
function GetAlter()
{
Param([ref]$db)
$alter = $db.value.ExecuteWithResults('
select row_number() over (order by schema_name(o.[schema_id]), o.name, c.name) RowNum,
schema_name(o.[schema_id]) [schema],
o.name [table],
c.name [column],
''alter table ['' + schema_name(o.[schema_id]) + ''].['' + o.name + ''] alter column ['' + c.name + ''] '' + t.name +
case when t.name not in (''ntext'', ''text'')
then ''('' +
case
when t.name in (''nchar'', ''nvarchar'') and c.max_length != -1
then cast(c.max_length / 2 as varchar(10))
when t.name in (''char'', ''varchar'') and c.max_length != -1
then cast(c.max_length as varchar(10))
when t.name in (''nchar'', ''nvarchar'', ''char'', ''varchar'') and c.max_length = -1
then ''max''
else cast(c.max_length as varchar(10))
end + '')''
else ''''
end + '' collate '' + ''' + $Collate + ''' +
case when c.is_nullable = 1
then '' null''
else '' not null''
end [alter]
from sys.columns c
join sys.objects o
on c.[object_id] = o.[object_id]
join sys.types t
on c.system_type_id = t.system_type_id and c.user_type_id = t.user_type_id
where t.name in (''char'', ''varchar'', ''text'', ''nvarchar'', ''ntext'', ''nchar'')
and c.collation_name != ''' + $Collate + '''
and o.[type] = ''U''
and o.name not in(''sysarticles'', ''sysschemaarticles'')
order by RowNum')
return $alter
}
try
{
$srv = New-Object('Microsoft.SqlServer.Management.Smo.Server') $Server
$db = $srv.Databases[$Database]
if ($db -eq $null)
{
throw 'Incorrect server or database name'
}
$alter = GetAlter ([ref]$db)
'use [' + $Database + ']' | Out-File $FileDropFK
'go' | Out-File $FileDropFK -Append
'use [' + $Database + ']' | Out-File $FileAlter
'go' | Out-File $FileAlter -Append
'use [' + $Database + ']' | Out-File $FileCreateFK
'go' | Out-File $FileCreateFK -Append
foreach ($t in $alter.Tables)
{
foreach($row in $t.Rows)
{
WriteScript ([ref]$srv) ([ref]$t) ([ref]$row)
}
}
}
catch
{
$message = $_.Exception.ToString();
Write-Host "Failed $message"
}