我有一个 .sql 脚本,其中包含许多适用于某些临时表的操作查询。此脚本需要运行两次,中间还有一些其他命令,即:
do_stuff.sql
处理它蛮力方法是根据需要复制和粘贴 dostuff.sql。虽然这在技术上可行,但有更好的方法吗?
我希望有一个像
RunThisSQL 'C:\do_stuff.sql'
这样的命令我还没有发现。更新
嗯,已经大约 5 年了,我刚刚重新发现了这个老问题。我最近做了这个并制作了一个游标来循环通过主表。对于该主表中的每条记录,脚本使用主表设置的变量运行内部脚本。
https://www.mssqltips.com/sqlservertip/1599/sql-server-cursor-example/
最佳答案
如果您使用 Visual Studio,您可以创建“Sql Server 数据库”项目。使用该项目,您可以创建脚本,让您以某种方式执行 *.sql 文件
/*
Post-Deployment Script Template
--------------------------------------------------------------------------------------
This file contains SQL statements that will be appended to the build script.
Use SQLCMD syntax to include a file in the post-deployment script.
Example: :r .\myfile.sql
Use SQLCMD syntax to reference a variable in the post-deployment script.
Example: :setvar TableName MyTable
SELECT * FROM [$(TableName)]
--------------------------------------------------------------------------------------
*/
也可以看看。 http://candordeveloper.com/2013/01/08/creating-a-sql-server-database-project-in-visual-studio-2012/
关于来自另一个查询的 sql server : how to execute a . sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16676918/