简体   繁体   English

导入/导出SQL Server存储过程的方法

[英]Approach to import/export SQL Server stored procedures

I know most people will give responses like, right click database name, select task, generate scripts, etc. Most people give detail response on the internet include stackoverflow 我知道大多数人都会给出响应,例如右键单击数据库名称,选择任务,生成脚本等。大多数人会在Internet上给出详细的响应,包括stackoverflow

I already know that long time ago. 我早就知道了

My question is any suggestions to write scripts to export/import stored procedures. 我的问题是编写脚本以导出/导入存储过程的任何建议。 If I only have to do 5 or 10, it is no big issue. 如果我只需要做5或10,就没什么大问题了。 I just have to pick right ones. 我只需要选择合适的。 If there are lots like 50 or more, it is pain and bound to be errors. 如果有很多像50或更多,那是痛苦的,注定是错误的。 Sometimes, to make life easier, we want to export/import with suffix such as *test.sql . 有时,为了使生活更轻松,我们希望使用*test.sql等后缀进行导出/导入。

Any suggestion using SQL scripts, C# or dos commands to do so? 有建议使用SQL脚本,C#或dos命令这样做吗?

Thanks 谢谢

Why have you tagged this question with C# when you ask for answers in SQL or DOS script? 在SQL或DOS脚本中要求答案时,为什么用C#标记此问题?

This CMD script drops all stored procedures whose names begin with "usp": 此CMD脚本删除名称以“ usp”开头的所有存储过程:

FOR /F "tokens=* USEBACKQ" %%F IN (`sqlcmd -S %SERVER% -d %DATABASE% -Q "select name from sys.objects where type = 'P'" `) DO (echo %%F | findstr "^usp" >nul 2>&1 && sqlcmd -S %SERVER% -d %DATABASE% -Q "drop procedure %%F" )

It's all on one line and %SERVER% and %DATABASE% identify the server and database. 它全部在一行上,%SERVER%和%DATABASE%标识服务器和数据库。

This script loads all stored procedures from the current directory whose names match "usp*.sql". 该脚本从名称匹配“ usp * .sql”的当前目录中加载所有存储过程。 You will need the setting QUOTED_IDENTIFIER to be ON if you invoke XML data type methods. 如果调用XML数据类型方法,则需要将QUOTED_IDENTIFIER设置为ON。

for %%f in (usp*.sql) do sqlcmd  -S %SERVER% -d %DATABASE% -I -i %%f

Since you tagged also C#. 由于您还标记了C#。 With .NET you can do it using Microsoft.SqlServer.Smo . 使用.NET,您可以使用Microsoft.SqlServer.Smo来实现 There is a StoredProcedure class . 有一个StoredProcedure类 This approach gives you really lots of control over the task you want to achieve. 这种方法使您可以对要完成的任务进行大量控制。 Example usage you can find here , also using PowerShell. 您可以在此处找到示例用法,也可以使用PowerShell。

Here's a snippet I've put together that might help you(uses Microsoft.SqlServer.Smo , Microsoft.SqlServer.ConnectionInfo , Microsoft.SqlServer.Management.Sdk.Sfc DLLs): 这是我整理的一个片段,可能会对您有所帮助(使用Microsoft.SqlServer.SmoMicrosoft.SqlServer.ConnectionInfoMicrosoft.SqlServer.Management.Sdk.Sfc DLL):

var serverConnection = new ServerConnection(new SqlConnection(ConnectionString));
var server = new Server(serverConnection);
foreach (StoredProcedure sp in server.Databases["YourDB"].StoredProcedures)
{
    if (sp.Name.Contains("yourSubstring"))
    {
        System.IO.File.WriteAllText(sp.TextHeader + Environment.NewLine + sp.TextBody);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM