SQL Server:如何在没有DMO/SMO的情况下生成对象脚本?
我想为数据库对象生成脚本,例如
- 表
- 视图
- 存储过程
- 函数
自:
未安装在全新安装的
- Windows XP
- Windows Vista
- Windows 7
上,也不可重新分发,它们不是一个选项(它将在客户的计算机上运行)。
(编辑:看起来 SMO 实际上是 可重新分发。)
是否有任何源代码可以将SELECTs从系统表转换为关联的脚本?
我将从编写存储过程、视图、触发器或用户定义函数的伪代码开始:
String GetStoredProcedureScript(String storedProcedureName)
{
return GetHelpText(storedProcedureName);
}
String GetViewScript(String viewName)
{
return GetHelpText(viewName);
}
String GetTriggerScript(String triggerName)
{
return GetHelpText(storedProcedureName);
}
String GetUserDefinedFunctionScript(String userDefinedFunctionName)
{
return GetHelpText(userDefinedFunctionName);
}
所有这些都可以在内部使用单个辅助函数:
String GetHelpText(String objectName)
{
String szQuery = 'EXECUTE sp_helptext '+QuotedStr(objectName);
String sql := '';
using (Recordset rs = connection.Execute(szQuery))
{
while not rs.EOF do
{
sql = sql+rs['text'];
rs.Next;
}
}
return sql;
}
编辑:< /strong> 谢谢 servicesharvest316 指出 sp_helptext
。这就是为什么我有一个类来抽象这些东西。
i want to generate scripts for database objects, e.g.
- tables
- views
- stored procedures
- functions
Since:
are not installed on a fresh install of:
- Windows XP
- Windows Vista
- Windows 7
nor are they redistributable, they are not an option (it will run on a customer's machine).
(EDIT: It looks as if SMO is actually redistributable as of today.)
Is there any source code that converts SELECTs from system tables into associated scripts?
i'll start us off with the pseudo-code that scripts a stored procedures, views, triggers, or user-defined functions:
String GetStoredProcedureScript(String storedProcedureName)
{
return GetHelpText(storedProcedureName);
}
String GetViewScript(String viewName)
{
return GetHelpText(viewName);
}
String GetTriggerScript(String triggerName)
{
return GetHelpText(storedProcedureName);
}
String GetUserDefinedFunctionScript(String userDefinedFunctionName)
{
return GetHelpText(userDefinedFunctionName);
}
All which can internally use a single helper function:
String GetHelpText(String objectName)
{
String szQuery = 'EXECUTE sp_helptext '+QuotedStr(objectName);
String sql := '';
using (Recordset rs = connection.Execute(szQuery))
{
while not rs.EOF do
{
sql = sql+rs['text'];
rs.Next;
}
}
return sql;
}
Edit: Thanks servicesharvest316 for pointing out sp_helptext
. That's why i have a class that abstracts these things away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这本书适合你。它解释了如何制作一个可以满足您要求的代码生成器。
我使用 MySql 的修改版本,它非常有用。
Microsoft .NET 中的代码生成
This is the book for you. It explains how to make a code generator that will do what you asked.
I use a modified version for MySql and it worked like a charm.
Code Generation in Microsoft .NET
您尝试过 sp_helptext 吗?
Have you tried sp_helptext?
打开 DBDiff (引用此处) 实现用于比较数据库和生成更新脚本的 GUI。它还包括一个命令行工具。您可以使用项目的内部结构来生成 CREATE TABLE 语句。
Open DBDiff (referenced here) implements a GUI for comparing databases and generating an update script. It also includes a command line tool. You could use the guts of the project to generate CREATE TABLE statements.