SQL Server:如何在没有DMO/SMO的情况下生成对象脚本?

发布于 2024-08-12 18:13:37 字数 1856 浏览 4 评论 0原文

我想为数据库对象生成脚本,例如

  • 视图
  • 存储过程
  • 函数

自:

未安装在全新安装的

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扎心 2024-08-19 18:13:37

这本书适合你。它解释了如何制作一个可以满足您要求的代码生成器。

我使用 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

忱杏 2024-08-19 18:13:37

您尝试过 sp_helptext 吗?

String szQuery = 'EXEC sp_helptext '+QuotedStr(storedProcedureName)

Have you tried sp_helptext?

String szQuery = 'EXEC sp_helptext '+QuotedStr(storedProcedureName)
梦过后 2024-08-19 18:13:37

打开 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文