Using KILL with a declared variable(将 KILL 与声明的变量一起使用)
问题描述
我正在尝试使用带有声明变量的 KILL 语句,但它给了我一个语法错误.无论如何不使用常量并以编程方式更改 SPID?
I'm trying to use a KILL statement with a declared variable but it's giving me a syntax error. Is there anyway to not use a constant and programatically change the SPID?
例如:
DECLARE @SPID smallint
SET @SPID = 100
Kill @SPID
顺便说一句,这只是一个例子.我需要使用游标在循环中运行 kill 以摆脱旧的持久用户连接.(别问了)
BTW this is just an example. I need to run the kill in a loop with a cursor to get rid of old persistant user connections. (Don't ask)
推荐答案
我认为您需要为此使用动态 SQL.在使用动态 SQL 执行任何操作之前,请阅读这个重要页面.
I think you're going to need dynamic SQL for this. Read this essential page before doing anything with dynamic SQL, please.
DECLARE @SPID smallint
DECLARE @SQL nvarchar(1000)
SET @SPID = 100
SET @SQL = 'KILL ' + CAST(@SPID as varchar(4))
EXEC (@SQL)
这篇关于将 KILL 与声明的变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 KILL 与声明的变量一起使用


基础教程推荐
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01