Oleg Sych's initial tutorial
为所有表提供枚举,以创建(在我看来相当愚蠢的)删除过程。这只是一个实验,所以它的完全无用不会打扰我。:)
<#@ template language="C#" hostspecific="true" #>
<#@ output extension="SQL" #>
<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ include file="T4Toolbox.tt" #>
<#
// Config variables
string serverName = "dbserver\\dbinstance";
string dbName = "dbname";
#>
USE <#= dbName #>
<#
// Iterate over tables and generate procs
Server server = new Server(serverName);
Database database = new Database(server, dbName);
WriteLine("/* Number of tables: " + database.Tables.Count.ToString() + " */");
foreach (Table table in database.Tables)
{
table.Refresh();
#>
CREATE PROCEDURE <#= table.Name #>_Delete
<#
PushIndent(" ");
foreach (Column column in table.Columns)
{
if (column.InPrimaryKey)
WriteLine("@" + column.Name + " " + column.DataType.Name);
}
PopIndent();
#>
AS
DELETE FROM
<#= table.Name #>
WHERE
<#
PushIndent(" ");
foreach (Column column in table.Columns)
{
if (column.InPrimaryKey)
WriteLine(column.Name + " = @" + column.Name);
}
PopIndent();
WriteLine("GO");
}
#>
Tables
0
.
USE dbname
/* Number of tables: 0 */