Heim > Datenbank > MySQL-Tutorial > SqlServer2008根据现有表,获取该表的分区创建脚本

SqlServer2008根据现有表,获取该表的分区创建脚本

WBOY
Freigeben: 2016-06-07 15:45:51
Original
1258 Leute haben es durchsucht

1 *============================================================== 2 名称: [ GetMSSQLTableScript ] 3 功能: 获取customize单个表的mysql脚本 4 创建:2015年3月23日 5 参数: @DBName -- 数据库名称 6 @TBName -- 表名 7 @SchemeName -- 数据库表引用的

<span>  1</span> <span>*==============================================================</span>
<span>  2</span> 名称: <span>[</span><span>GetMSSQLTableScript</span><span>]</span>
<span>  3</span> <span>功能: 获取customize单个表的mysql脚本 
</span><span>  4</span> <span>创建:2015年3月23日
</span><span>  5</span> 参数:<span>@DBName</span>            <span>--</span><span>数据库名称</span>
<span>  6</span>       <span>@TBName</span>            <span>--</span><span>表名</span>
<span>  7</span>       <span>@SchemeName</span>        <span>--</span><span>数据库表引用的Scheme</span>
<span>  8</span>       <span>@PartitionScheme</span>    <span>--</span><span>分区Scheme</span>
<span>  9</span>       <span>@PartitionField</span>    <span>--</span><span>该表使用的分区字段      </span>
<span> 10</span>       <span>@SQL</span>                <span>--</span><span>输出脚本</span>
<span> 11</span> <span>==============================================================*/</span>
<span> 12</span> <span>ALTER</span> <span>PROCEDURE</span> <span>[</span><span>Tuning</span><span>]</span>.<span>[</span><span>GetMSSQLTableScript</span><span>]</span><span> (
</span><span> 13</span>     <span>@DBName</span> <span>nvarchar</span>(<span>64</span><span>),
</span><span> 14</span>     <span>@SchemeName</span> <span>nvarchar</span>(<span>32</span><span>),
</span><span> 15</span>     <span>@TBName</span> <span>nvarchar</span>(<span>128</span><span>),    
</span><span> 16</span>     <span>@PartitionScheme</span> <span>nvarchar</span>(<span>32</span><span>),
</span><span> 17</span>     <span>@PartitionField</span> <span>nvarchar</span>(<span>32</span><span>),    
</span><span> 18</span>     <span>@SQL</span> <span>nvarchar</span>(<span>max</span><span>) OUTPUT
</span><span> 19</span> <span>)
</span><span> 20</span> <span>AS</span>  
<span> 21</span> <span>Begin</span>
<span> 22</span>     <span>declare</span> <span>@table_script</span> <span>nvarchar</span>(<span>max</span>) <span>--</span><span>建表的脚本</span>
<span> 23</span>     <span>declare</span> <span>@index_script</span> <span>nvarchar</span>(<span>max</span>) <span>--</span><span>索引的脚本</span>
<span> 24</span>     <span>declare</span> <span>@default_script</span> <span>nvarchar</span>(<span>max</span>) <span>--</span><span>默认值的脚本</span>
<span> 25</span>     <span>declare</span> <span>@check_script</span> <span>nvarchar</span>(<span>max</span>) <span>--</span><span>check约束的脚本</span>
<span> 26</span>     <span>declare</span> <span>@sql_cmd</span> <span>nvarchar</span>(<span>max</span>)  <span>--</span><span>动态SQL命令</span>
<span> 27</span>     <span>declare</span> <span>@err_info</span> <span>varchar</span>(<span>200</span><span>)
</span><span> 28</span>     <span>set</span> <span>@TBName</span> <span>=</span> <span>UPPER</span>(<span>@TBName</span><span>);
</span><span> 29</span>     <span>if</span> <span>OBJECT_ID</span>(<span>@DBName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span>) <span>is</span> <span>null</span>
<span> 30</span>     <span>BEGIN</span>
<span> 31</span>         <span>set</span> <span>@err_info</span><span>=</span><span>'</span><span>对象:</span><span>'</span><span>+</span><span>@DBName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span><span>+</span><span>'</span><span>不存在!</span><span>'</span>
<span> 32</span>         <span>raiserror</span>(<span>@err_info</span>,<span>16</span>,<span>1</span><span>)
</span><span> 33</span>         <span>return</span>
<span> 34</span>     <span>END</span>
<span> 35</span> 
<span> 36</span>     <span>--</span><span>--------------------生成创建表脚本----------------------------</span>
<span> 37</span>     <span>--</span><span>1.添加算定义字段</span>
<span> 38</span>     <span>set</span> <span>@table_script</span> <span>=</span> <span>'</span><span>CREATE TABLE </span><span>'</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span><span>+</span><span>'</span>
<span> 39</span> <span>    (</span><span>'</span><span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>);
</span><span> 40</span>      
<span> 41</span>      
<span> 42</span>     <span>--</span><span>添加表中的其它字段</span>
<span> 43</span>     <span>set</span> <span>@sql_cmd</span><span>=</span>N<span>'</span>
<span> 44</span> <span>    use </span><span>'</span><span>+</span><span>@DBName</span><span>+</span><span>'</span>
<span> 45</span> <span>    set @table_script=</span><span>''''</span> 
<span> 46</span> <span>    select @table_script=@table_script+
</span><span> 47</span>             <span>''</span><span> [</span><span>''</span><span>+t.NAME+</span><span>''</span><span>] </span><span>''</span>
<span> 48</span> <span>            +(case when t.xusertype in (175,62,239,59,122,165,173) then </span><span>''</span><span>[</span><span>''</span><span>+p.name+</span><span>''</span><span>] (</span><span>''</span><span>+convert(varchar(30),isnull(t.prec,</span><span>''''</span><span>))+</span><span>''</span><span>)</span><span>''</span>
<span> 49</span> <span>                  when t.xusertype in (231) and t.length=-1 then </span><span>''</span><span>[ntext]</span><span>''</span>
<span> 50</span> <span>                  when t.xusertype in (231) and t.length-1 then </span><span>''</span><span>[</span><span>''</span><span>+p.name+</span><span>''</span><span>] (</span><span>''</span><span>+convert(varchar(30),isnull(t.prec,</span><span>''''</span><span>))+</span><span>''</span><span>)</span><span>''</span>
<span> 51</span> <span>                 when t.xusertype in (167) and t.length=-1 then </span><span>''</span><span>[text]</span><span>''</span>
<span> 52</span> <span>                  when t.xusertype in (167) and t.length-1 then </span><span>''</span><span>[</span><span>''</span><span>+p.name+</span><span>''</span><span>] (</span><span>''</span><span>+convert(varchar(30),isnull(t.prec,</span><span>''''</span><span>))+</span><span>''</span><span>)</span><span>''</span>
<span> 53</span> <span>                  when t.xusertype in (106,108) then </span><span>''</span><span>[</span><span>''</span><span>+p.name+</span><span>''</span><span>] (</span><span>''</span><span>+convert(varchar(30),isnull(t.prec,</span><span>''''</span><span>))+</span><span>''</span><span>,</span><span>''</span><span>+convert(varchar(30),isnull(t.scale,</span><span>''''</span><span>))+</span><span>''</span><span>)</span><span>''</span>
<span> 54</span> <span>                  else </span><span>''</span><span>[</span><span>''</span><span>+p.name+</span><span>''</span><span>]</span><span>''</span>
<span> 55</span> <span>             END)
</span><span> 56</span> <span>             +(case when t.isnullable=1 then </span><span>''</span><span> null</span><span>''</span><span> else </span><span>''</span><span> not null </span><span>''</span><span>end)
</span><span> 57</span> <span>             +(case when COLUMNPROPERTY(t.ID, t.NAME, </span><span>''</span><span>ISIDENTITY</span><span>''</span><span>)=1 then </span><span>''</span><span> identity</span><span>''</span><span> else </span><span>''''</span><span> end)
</span><span> 58</span> <span>             +</span><span>''</span><span>,</span><span>''</span><span>+char(13)+char(10)
</span><span> 59</span> <span>    from syscolumns t join systypes p  on t.xusertype = p.xusertype
</span><span> 60</span> <span>    where t.ID=OBJECT_ID(</span><span>'''</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span><span>+</span><span>'''</span><span>)
</span><span> 61</span> <span>    ORDER BY  t.COLID; 
</span><span> 62</span>     <span>'</span>
<span> 63</span>     <span>EXEc</span> sp_executesql <span>@sql_cmd</span>,N<span>'</span><span>@table_script varchar(max) output</span><span>'</span>,<span>@sql_cmd</span><span> output
</span><span> 64</span>     <span>set</span> <span>@table_script</span><span>=</span><span>@table_script</span><span>+</span><span>@sql_cmd</span>
<span> 65</span>     <span>IF</span> <span>len</span>(<span>@table_script</span>)<span>></span><span>0</span>
<span> 66</span>         <span>set</span> <span>@table_script</span><span>=</span><span>substring</span>(<span>@table_script</span>,<span>1</span>,<span>len</span>(<span>@table_script</span>)<span>-</span><span>3</span>)<span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span> 67</span>             <span>+</span><span>'</span><span>)On </span><span>'</span><span>+</span><span>@PartitionScheme</span><span>+</span><span>'</span><span>(</span><span>'</span><span>+</span><span>@PartitionField</span><span>+</span><span>'</span><span>)
</span><span> 68</span>             <span>'</span><span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span> 69</span>             <span>--</span><span>+'GO'</span>
<span> 70</span>             <span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span>)<span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span> 71</span>         
<span> 72</span>     <span>--</span><span>------------------生成索引脚本---------------------------------------</span>
<span> 73</span>     <span>set</span> <span>@index_script</span><span>=</span><span>''</span>
<span> 74</span>     <span>set</span> <span>@sql_cmd</span><span>=</span>N<span>'</span>
<span> 75</span> <span>    use </span><span>'</span><span>+</span><span>@DBName</span><span>+</span><span>'</span>
<span> 76</span> <span>    declare @ct int
</span><span> 77</span> <span>    declare @scheme nvarchar(32)
</span><span> 78</span> <span>    declare @indid int      --当前索引ID
</span><span> 79</span> <span>    declare @p_indid int    --前一个索引ID
</span><span> 80</span> <span>    declare @partitionField nvarchar(32)
</span><span> 81</span> <span>    set @partitionField=</span><span>'''</span><span>+</span><span>@PartitionField</span><span>+</span><span>'''</span>
<span> 82</span> <span>    select @indid=-1, @p_indid=0,@ct=0    --初始化,以后用@indid和@p_indid判断是否索引ID发生变化
</span><span> 83</span> <span>    set @index_script=</span><span>''''</span>
<span> 84</span> <span>    set @scheme=</span><span>'''</span><span>+</span><span>@SchemeName</span><span>+</span><span>'''</span>
<span> 85</span> <span>    select @indid=INDID
</span><span> 86</span> <span>        ,@index_script=@index_script
</span><span> 87</span> <span>        +(case when @indid@p_indid and @ct>0 
</span><span> 88</span> <span>            then </span><span>''</span><span>)</span><span>''</span><span>+char(13)+char(10)    +char(13)+char(10)
</span><span> 89</span> <span>            else </span><span>''''</span> 
<span> 90</span> <span>        end)
</span><span> 91</span> <span>        +(case when @indid@p_indid and UNIQ=</span><span>''</span><span>PRIMARY KEY</span><span>''</span> 
<span> 92</span> <span>              then </span><span>''</span><span>ALTER TABLE </span><span>''</span><span>+TABNAME+</span><span>''</span><span> ADD CONSTRAINT </span><span>''</span><span>+name+</span><span>''</span><span> PRIMARY KEY </span><span>''</span><span>+cluster+char(13)+char(10)+</span><span>''</span><span>(</span><span>''</span><span>+char(13)+char(10)+</span><span>''</span>    <span>''</span><span>+COLNAME+</span><span>''</span><span>,</span><span>''</span><span>+@partitionField+char(13)+char(10)
</span><span> 93</span> <span>              when @indid@p_indid and UNIQ=</span><span>''</span><span>UNIQUE</span><span>''</span> 
<span> 94</span> <span>              then </span><span>''</span><span>ALTER TABLE </span><span>''</span><span>+TABNAME+</span><span>''</span><span> ADD CONSTRAINT </span><span>''</span><span>+name+</span><span>''</span><span> UNIQUE </span><span>''</span><span>+cluster+char(13)+char(10)+</span><span>''</span><span>(</span><span>''</span><span>+char(13)+char(10)+</span><span>''</span>    <span>''</span><span>+COLNAME+</span><span>''</span><span>,</span><span>''</span><span>+@partitionField+char(13)+char(10)
</span><span> 95</span> <span>              when @indid@p_indid and UNIQ=</span><span>''</span><span>INDEX</span><span>''</span>     
<span> 96</span> <span>              then </span><span>''</span><span>CREATE </span><span>''</span><span>+cluster+</span><span>''</span><span> INDEX </span><span>''</span><span>+name+</span><span>''</span><span> ON </span><span>''</span><span>+TABNAME+char(13)+char(10)+</span><span>''</span><span>(</span><span>''</span><span>+char(13)+char(10)+</span><span>''</span>    <span>''</span><span>+COLNAME+char(13)+char(10)
</span><span> 97</span> <span>              when @indid=@p_indid
</span><span> 98</span> <span>              then  </span><span>''</span><span>    ,</span><span>''</span><span>+COLNAME+char(13)+char(10)
</span><span> 99</span> <span>         end)
</span><span>100</span> <span>        ,@ct=@ct+1
</span><span>101</span> <span>        ,@p_indid=@indid
</span><span>102</span> <span>    from 
</span><span>103</span> <span>    (
</span><span>104</span> <span>        SELECT A.INDID,B.KEYNO
</span><span>105</span> <span>            ,NAME,@scheme+</span><span>''</span><span>.</span><span>''</span><span>+(SELECT NAME FROM SYSOBJECTS WHERE ID=A.ID) AS TABNAME,
</span><span>106</span> <span>            (SELECT NAME FROM SYSCOLUMNS WHERE ID=B.ID AND COLID=B.COLID) AS COLNAME,
</span><span>107</span> <span>            (CASE WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=</span><span>''</span><span>UQ</span><span>''</span><span>) THEN </span><span>''</span><span>UNIQUE</span><span>''</span> 
<span>108</span> <span>                  WHEN EXISTS(SELECT 1 FROM SYSOBJECTS WHERE NAME=A.NAME AND XTYPE=</span><span>''</span><span>PK</span><span>''</span><span>) THEN </span><span>''</span><span>PRIMARY KEY</span><span>''</span>
<span>109</span> <span>                  ELSE </span><span>''</span><span>INDEX</span><span>''</span><span> END)  AS UNIQ,
</span><span>110</span> <span>            (CASE WHEN A.INDID=1 THEN </span><span>''</span><span>CLUSTERED</span><span>''</span><span> WHEN A.INDID>1 THEN </span><span>''</span><span>NONCLUSTERED</span><span>''</span><span> END) AS CLUSTER
</span><span>111</span> <span>        FROM SYSINDEXES A INNER JOIN SYSINDEXKEYS B ON A.INDID=B.INDID AND A.ID=B.ID
</span><span>112</span> <span>        WHERE A.ID=OBJECT_ID(</span><span>'''</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span><span>+</span><span>'''</span><span>) and a.indid0 /*如果该表是一个分区表,就必须添加条件:and b.keyno0*/
</span><span>113</span> <span>    ) t
</span><span>114</span> <span>    ORDER BY INDID,KEYNO</span><span>'</span>
<span>115</span>     <span>EXEc</span> sp_executesql <span>@sql_cmd</span>,N<span>'</span><span>@index_script varchar(max) output</span><span>'</span>,<span>@sql_cmd</span><span> output
</span><span>116</span>     <span>set</span> <span>@index_script</span><span>=</span><span>@sql_cmd</span>
<span>117</span>     <span>IF</span> <span>len</span>(<span>@index_script</span>)<span>></span><span>0</span>
<span>118</span>         <span>set</span> <span>@index_script</span><span>=</span><span>@index_script</span><span>+</span><span>'</span><span>)</span><span>'</span><span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span>119</span>         <span>--</span><span>+'go'</span>
<span>120</span>         <span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span>)<span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span>121</span>     <span>--</span><span>生成默认值约束</span>
<span>122</span>     <span>set</span> <span>@sql_cmd</span><span>=</span><span>'</span>
<span>123</span> <span>    use </span><span>'</span><span>+</span><span>@DBName</span><span>+</span><span>'</span>
<span>124</span> <span>    declare @scheme nvarchar(32)
</span><span>125</span> <span>    declare @partitionField nvarchar(32)
</span><span>126</span> <span>    set @partitionField=</span><span>'''</span><span>+</span><span>@PartitionField</span><span>+</span><span>'''</span>
<span>127</span> <span>    set @scheme=</span><span>'''</span><span>+</span><span>@SchemeName</span><span>+</span><span>'''</span>
<span>128</span> <span>    set @default_script=</span><span>''''</span>
<span>129</span> <span>    SELECT @default_script=@default_script
</span><span>130</span> <span>            +</span><span>''</span><span>ALTER TABLE </span><span>''</span><span>+@scheme+</span><span>''</span><span>.</span><span>''</span><span>+OBJECT_NAME(O.PARENT_OBJ)
</span><span>131</span> <span>            +</span><span>''</span><span> ADD CONSTRAINT </span><span>''</span><span>+O.NAME+</span><span>''</span><span> default </span><span>''</span><span>+t.text+</span><span>''</span><span> for </span><span>''</span><span>+C.NAME+char(13)+char(10)+char(13)+char(10)
</span><span>132</span> <span>    FROM SYSOBJECTS O INNER JOIN SYSCOMMENTS T ON O.ID=T.ID
</span><span>133</span> <span>        INNER JOIN SYSCOLUMNS C ON O.PARENT_OBJ=C.ID AND C.CDEFAULT=T.ID
</span><span>134</span> <span>    WHERE O.XTYPE=</span><span>''</span><span>D</span><span>''</span><span> AND O.PARENT_OBJ=OBJECT_ID(</span><span>'''</span><span>+</span><span>@SchemeName</span><span>+</span><span>'</span><span>.</span><span>'</span><span>+</span><span>@TBName</span><span>+</span><span>'''</span><span>)</span><span>'</span>
<span>135</span>     <span>EXEc</span> sp_executesql <span>@sql_cmd</span>,N<span>'</span><span>@default_script varchar(max) output</span><span>'</span>,<span>@sql_cmd</span><span> output
</span><span>136</span>     <span>set</span> <span>@default_script</span><span>=</span><span>@sql_cmd</span><span>+</span><span>char</span>(<span>13</span>)<span>+</span><span>char</span>(<span>10</span><span>)
</span><span>137</span> 
<span>138</span>     <span>set</span> <span>@SQL</span><span>=</span><span>@table_script</span><span>+</span><span>@index_script</span><span>+</span><span>@default_script</span>
<span>139</span>     <span>declare</span> <span>@len</span> <span>int</span>,<span>@n</span> <span>int</span>
<span>140</span>     <span>set</span> <span>@len</span><span>=</span><span>LEN</span>(<span>@SQL</span><span>)
</span><span>141</span>     <span>set</span> <span>@n</span><span>=</span><span>0</span>
<span>142</span>     <span>while</span>(<span>@len</span><span>></span><span>0</span><span>)
</span><span>143</span>     <span>BEGIN</span>
<span>144</span>       <span>PRINT</span>(<span>substring</span>(<span>@SQL</span>,<span>@n</span><span>*</span><span>4000</span><span>+</span><span>1</span>,<span>4000</span><span>));
</span><span>145</span>       <span>set</span> <span>@n</span><span>=</span><span>@n</span><span>+</span><span>1</span>
<span>146</span>       <span>set</span> <span>@len</span><span>=</span><span>@len</span><span>-</span><span>4000</span><span>;
</span><span>147</span>     <span>END</span>
<span>148</span> <span>End</span>
Nach dem Login kopieren

该函数的原创作者:http://www.cnblogs.com/champaign/p/3492510.html

本人及修改了一部分内容,让该存储过程更灵活点。

公司DBA支持给建议不要用sysindexkeys来查找对应的列,而是使用syscolumns来提到:

比如:select * from syscolumns where id=object_id('dx.Article');

 select * from sys.index_columns where object_id=object_id('dx.Article');

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage