代码之家  ›  专栏  ›  技术社区  ›  John Rudy

SQL SMO不枚举表

  •  6
  • John Rudy  · 技术社区  · 17 年前

    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 */
    

    1 回复  |  直到 17 年前
        1
  •  13
  •   Oleg Sych Oleg Sych    17 年前

    推荐文章