-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlserver.sh
105 lines (94 loc) · 3.67 KB
/
sqlserver.sh
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
sqlserver_cfg() {
local HOST=140.143.97.87
local USER=sa
local PWD=Innov6-2-1
local DB=WIMSFX11
echo "-S ${HOST} -U ${USER} -P ${PWD} -d ${DB}"
}
sqlserverconnectsqlcmd() {
local ret=$(sqlserver_cfg)
eval "sqlcmd ${ret}"
}
sqlserverconnectmssql() {
local ret=$(sqlserver_cfg)
eval "mssql-cli ${ret}"
}
sqlserversql() {
local ret=$(sqlserver_cfg)
eval "sqlcmd ${ret} -y 25 -Y 25 -Q \"${1}\""
}
sqlserverdbs() {
sqlserversql "SELECT name FROM sys.databases ORDER BY name"
}
sqlservertables() {
sqlserversql "SELECT name FROM sys.objects WHERE type = 'U' ORDER BY name"
}
sqlserverdesc() {
sqlserversql "SELECT
t1.column_name Field,
(t1.data_type + '(' + CAST(t2.length AS VARCHAR(10)) + ')') Type,
t1.is_nullable [Null],
t1.column_default [Default],
t3.value [Comment],
t4.value [Table Comment],
t5.type [Table Type]
FROM information_schema.columns t1
LEFT JOIN syscolumns t2 ON t1.column_name = t2.name
LEFT JOIN sys.extended_properties t3 ON t2.colid = t3.minor_id AND t3.major_id = object_id('${1}')
LEFT JOIN sys.extended_properties t4 ON t4.major_id = object_id('${1}') AND t4.minor_id = 0 AND t4.name = 'MS_Description'
LEFT JOIN sysobjects t5 ON t5.id = object_id('${1}')
WHERE t1.table_name = '${1}'
AND t2.id = object_id('${1}')"
}
sqlserverprimarykey() {
sqlserversql "SELECT
table_name [Table],
column_name [Pri]
FROM information_schema.key_column_usage
WHERE table_name = '${1}'"
}
sqlservertopn() {
sqlserversql "SELECT TOP ${1} * FROM ${2}"
}
sqlserveraddcmt() {
if [ -z "${3}" ]; then
sqlserversql "EXEC sp_addextendedproperty
@name = N'MS_Description', @value = '${2}',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}'"
else
sqlserversql "EXEC sp_addextendedproperty
@name = N'MS_Description', @value = '${3}',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}',
@level2type = N'Column', @level2name = '${2}'"
fi
}
sqlserverupdatecmt() {
if [ -z "${3}" ]; then
sqlserversql "EXEC sp_updateextendedproperty
@name = N'MS_Description', @value = '${2}',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}'"
else
sqlserversql "EXEC sp_updateextendedproperty
@name = N'MS_Description', @value = '${3}',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}',
@level2type = N'Column', @level2name = '${2}'"
fi
}
sqlserverrmcmt() {
if [ -z "${2}" ]; then
sqlserversql "EXEC sp_dropextendedproperty
@name = N'MS_Description',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}'"
else
sqlserversql "EXEC sp_dropextendedproperty
@name = N'MS_Description',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = '${1}',
@level2type = N'Column', @level2name = '${2}'"
fi
}