TransactSQL 运行另一个 TransactSQL 脚本 [英] TransactSQL to run another TransactSQL script

查看:29
本文介绍了TransactSQL 运行另一个 TransactSQL 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 10 个事务 SQL 脚本,每个脚本创建一个表并用数据填充它.

I have 10 transact SQL scripts that each create a table and fill it with data.

我正在尝试创建 1 个主 sql 脚本,该脚本将运行其他 10 个脚本中的每一个.

I am attempting to create 1 master sql script that will run each of the 10 other scripts.

Microsoft SQL Server 2008 的 TSQL/TRANSACTSQL 有没有办法从当前 tsql 脚本中执行另一个 tsql 脚本?

Is there a way with TSQL / TRANSACTSQL for Microsoft SQL Server 2008 to execute another tsql script from within the current tsql script?

这旨在通过 SQL Server Management Studio (SSMS) 运行.

This is intended to be run through the SQL Server Management Studio (SSMS).

谢谢!

推荐答案

如果您尝试在 SSMS 中执行 .sql 文件,请尝试此操作:

Try this if you are trying to execute a .sql file in SSMS:

:r C:\Scripts\Script1.sql
:r C:\Scripts\Script2.sql
:r C:\Scripts\Script3.sql
...

注意:为了这个运行打开sql命令模式(查询> SQLCMD模式)

note: for this to run turn on sql command mode (Query > SQLCMD Mode)

如果这些是您经常运行的脚本,您可能会考虑将它们放入存储过程中并以这种方式运行...

If these are scripts you run fairly often you might consider dropping them in a stored proc and running them that way...

您也可以通过 sqlcmd 来完成(我认为它更常见):

You can also do it through sqlcmd (which I believe is more common):

sqlcmd -S serverName\instanceName -i C:\Scripts\Script1.sql

这篇关于TransactSQL 运行另一个 TransactSQL 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆