CodeSmith Procedure 生成模板:
Template:
<%@ CodeTemplate Language="C#" TargetLanguage="T-SQL" Description="Generates a update stored procedure." %>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the stored procedures should be based on." %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>
<script runat="template">
public string GetSqlParameterStatement(ColumnSchema column)
{
string param="@"+column.Name+" "+column.NativeType;
switch (column.DataType)
{
case DbType.Decimal:
{
param+="("+column.Precision+", "+column.Scale+")";
break;
}
default:
{
if(column.Size>0)
{
param+="("+column.Size+")";
}
break;
}
}
return param;
}
</script>
------------------------------------------------------------
-- Date Create:<%=DateTime.Now.ToLongDateString()%>
-- Create By: Generated By CodeSmith
------------------------------------------------------------
///
///Update<%=SourceTable.Name%>????
///
CREATE PROCEDURE dbo.Update<%=SourceTable.Name%>
<%for(int i=0;i<SourceTable.Columns.Count;i++){%>
<%=GetSqlParameterStatement(SourceTable.Columns[i])%><%if(i<SourceTable.Columns.Count-1){%>,
<%}%>
<%}%>
AS
UPDATE [<%= SourceTable.Name %>] SET
<% for (int i = 0; i < SourceTable.NonPrimaryKeyColumns.Count; i++) { %>
[<%= SourceTable.NonPrimaryKeyColumns[i].Name %>] = @<%= SourceTable.NonPrimaryKeyColumns[i].Name %><% if (i < SourceTable.NonPrimaryKeyColumns.Count - 1) { %>,<% } %>
<% } %>
WHERE
<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %>
<% if (i > 0) { %>AND <% } %>
[<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>] = @<%= SourceTable.PrimaryKey.MemberColumns[i].Name %>
<% } %>
Output:
------------------------------------------------------------
-- Date Create:2009年9月3日 星期四
-- Create By: Generated By CodeSmith
------------------------------------------------------------
///
///UpdateCompany????
///
CREATE PROCEDURE dbo.UpdateCompany
@CompanyId int(4),
@CompanyAccount varchar(50),
@CompanyName varchar(50),
@CompanyAddress varchar(50),
@ShortName varchar(50),
@Phone varchar(50),
@Logo varchar(50),
@Email varchar(50),
@PostCode int(4),
@URL varchar(50),
@CreateTime datetime(8),
@Belong varchar(50),
@AdminId int(4),
@ChangeTime datetime(8),
@AdminId2 int(4),
@State int(4),
@Remark varchar(100)AS
UPDATE [Company] SET
[CompanyAccount] = @CompanyAccount,
[CompanyName] = @CompanyName,
[CompanyAddress] = @CompanyAddress,
[ShortName] = @ShortName,
[Phone] = @Phone,
[Logo] = @Logo,
[Email] = @Email,
[PostCode] = @PostCode,
[URL] = @URL,
[CreateTime] = @CreateTime,
[Belong] = @Belong,
[AdminId] = @AdminId,
[ChangeTime] = @ChangeTime,
[AdminId2] = @AdminId2,
[State] = @State,
[Remark] = @Remark
WHERE
[CompanyId] = @CompanyId