Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQLServer2005中执行测试_Sql_Sql Server_Sql Server 2005_Exec - Fatal编程技术网

在SQLServer2005中执行测试

在SQLServer2005中执行测试,sql,sql-server,sql-server-2005,exec,Sql,Sql Server,Sql Server 2005,Exec,当我执行以下命令时 EXEC 'DROP TABLE bkp_anish_test' (“DROP TABLE bkp\u anish\u test”是一个动态构建的sql查询) 我得到以下错误 找不到存储过程“DROP TABLE bkp_anish_test” 改为这样做: exec sp_executesql N'DROP TABLE bkp_anish_test' 或者对于动态构建的字符串: declare @MyTable nvarchar(100) set @MyTable =

当我执行以下命令时

EXEC 'DROP TABLE bkp_anish_test'
“DROP TABLE bkp\u anish\u test”
是一个动态构建的sql查询)

我得到以下错误

找不到存储过程“DROP TABLE bkp_anish_test”

改为这样做:

exec sp_executesql N'DROP TABLE bkp_anish_test'
或者对于动态构建的字符串:

declare @MyTable nvarchar(100)
set @MyTable = N'bkp_anish_test'

declare @sql nvarchar(100)
set @sql = N'DROP TABLE ' + @MyTable
exec sp_executesql @sql

您不需要使用EXEC来运行sql语句。在查询编辑器中,只需运行

DROP TABLE bkp_anish_test
如果该表位于xyz数据库中,请尝试以下操作

 EXEC ('USE xyz ; DROP TABLE bkp_anish_test;');

尝试在命令中添加括号。如果要使用EXEC命令,则在运行SQL语句时必须包含它们

EXEC ('DROP TABLE bkp_anish_test')
如果您发布代码或XML,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码”按钮(101 010),以很好地格式化和语法突出显示它!