SQL Server:从外部停止查询
我通过 PHP 使用 SQLCMD 实用程序运行查询,我想知道是否有办法通过传递进程 ID 或其他内容来停止我通过另一个 SQL 命令执行的查询或脚本。
我还使用 SQL Server 的 PHP 驱动程序,这样做会更容易吗?
或者这一切都不可能 - 一旦查询运行,您就无法停止它?
谢谢大家
I run queries using SQLCMD utility via PHP and I wanted to know if there was a way to stop queries or scripts that I have executed via another SQL command by passing the process ID or something.
I also make use of the PHP Driver for SQL Server would it be easier to do this way?
Or is none of this possible - once a query runs you can not stop it?
Thanks all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL Server 跟踪所有正在执行的进程。您可以使用存储过程
SP_WHO
和SP_WHO2
查看该列表 - 每个进程都有一个唯一的SPID
。过去,我出于自己的目的复制并定制了这些过程背后的代码。
您可以使用
DBCC INPUTBUFFER (@SPID)
检查上次在SPID
上执行的查询您可以使用
KILL @SPID
终止大多数进程请注意通常认为终止进程不是一个好主意,因为您可能不确定它们到底在做什么。
SQL Server keeps track of all executing processes. You can review the list by using stored procedures
SP_WHO
andSP_WHO2
- each process has a uniqueSPID
.In the past, I have copied and customized the code behind these procedures for my own purposes.
You can check the query last executed on a
SPID
usingDBCC INPUTBUFFER (@SPID)
You can kill most processes using
KILL @SPID
Do note that it is not generally considered a good idea to kill processes because you may not be sure about exactly what they are doing.
取决于你如何调用 SQLCMD。您是否将其作为对 exec() 的调用来运行? PHP 命令没有给您太多的控制权。
如果您正在做一些耗时的事情,那么使用生产者-消费者架构可能会更好。让您的主程序发布(到数据库或文本文件)要运行的命令。让一个单独的进程(也许是另一个 PHP 脚本)运行该命令 - 但在启动 SQLCMD 之前,让它向主程序提供其进程 ID(或其他一些句柄)。
然后,在主程序中,如果生成的进程花费太长时间,您可以终止它。
关于 PHP exec 函数的一些注释可能会有所启发: https://www .php.net/manual/en/function.exec.php
Depends how you're calling that SQLCMD. Are you running it as a call to exec()? The PHP commands don't give you a lot of control.
If you're doing something time-consuming, it might be better to use a Producer-Consumer architecture. Have your main program publish (to a database, or a text file) the command to be run. Have a separate process (another PHP script, perhaps) run that command - but before it starts the SQLCMD, have it provide its process ID (or some other handle) to the main program.
Then, in your main program, you can kill the spawned process if it is taking too long.
Some of the comments on the PHP exec function might be enlightening: https://www.php.net/manual/en/function.exec.php