创建系统日志表的存储过程(CreateSystemLogTable)

<bgsound cep="0"><span style="COLOR: #008080">--</span><span style="COLOR: #008080">==============================================================</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">类型:存储过程</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">名称:CreateSystemLogTable</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">参数:无</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">作者:Dicky</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">日期:2009-1-89:48</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">描述:检查当前年中1-12月的系统日志表是否存在,不存在则创建,</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">表名称如System_log_20091...System_log_200912。</span><span style="COLOR: #008080"><br>--</span><span style="COLOR: #008080">==============================================================</span><span style="COLOR: #008080"><br></span><span style="COLOR: #0000ff">CREATE</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">PROCEDURE</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">CreateSystemLogTable</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">AS</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">BEGIN</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">DECLARE</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">NVARCHAR</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">50</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #0000ff">DECLARE</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@createSql</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">NVARCHAR</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">500</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #0000ff">DECLARE</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">INT</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">SET</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">WHILE</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000"></span><span style="FONT-WEIGHT: bold; COLOR: #800000">12</span><span style="COLOR: #000000"></span><span style="COLOR: #008080">/*</span><span style="COLOR: #008080">循环1-12个月</span><span style="COLOR: #008080">*/</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">BEGIN</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">SET</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">System_log_</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff00ff">LTRIM</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">YEAR</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">GETDATE</span><span style="COLOR: #000000">()))</span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff00ff">LTRIM</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #0000ff">IF</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">NOT</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">EXISTS</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">SELECT</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"></span><span style="COLOR: #0000ff">FROM</span><span style="COLOR: #000000">sysobjects</span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000">id</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="COLOR: #ff00ff">OBJECT_ID</span><span style="COLOR: #000000">(N</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)</span><span style="COLOR: #808080">AND</span><span style="COLOR: #000000"></span><span style="COLOR: #ff00ff">OBJECTPROPERTY</span><span style="COLOR: #000000">(id,N</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">IsUserTable</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #0000ff">BEGIN</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">SET</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@createSql</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000"><br>CREATETABLE[</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">](<br>[id][int]IDENTITY(1,1)NOTNULL,<br>[userid][int]NULL,<br>[type][nvarchar](50)NULL,<br>[context][nvarchar](50)NULL,<br>[ip][nvarchar](50)NULL,<br>[createDate][nvarchar](50)NULL<br>)ON[PRIMARY]<br><br>ALTERTABLE[</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">]ADD<br>CONSTRAINT[DF_</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@tableName</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">_createDate]DEFAULT(GETDATE())FOR[createDate]</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">EXEC</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@createSql</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #0000ff">END</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">SET</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"></span><span style="COLOR: #008000">@i</span><span style="COLOR: #000000"></span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"></span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">END</span></bgsound>

你可能感兴趣的:(create)