Relativity
Relativity

Reputation: 6868

executing a test in sql server 2005

When I am executing following ...

EXEC 'DROP TABLE bkp_anish_test'

('DROP TABLE bkp_anish_test' is a dynamically build sql query)

I am getting following error

Could not find stored procedure 'DROP TABLE bkp_anish_test'.

Upvotes: 6

Views: 111

Answers (3)

Abhijit
Abhijit

Reputation: 1173

You dont need to use EXEC to run the sql statement. In the query editor, just run

DROP TABLE bkp_anish_test

if the table is in xyz database, try this

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

Upvotes: 0

bobs
bobs

Reputation: 22224

Try adding parentheses to your command. You must include them when running a SQL statement, if you're going to use the EXEC command.

EXEC ('DROP TABLE bkp_anish_test')

Upvotes: 1

Joe Stefanelli
Joe Stefanelli

Reputation: 135928

Do this instead:

exec sp_executesql N'DROP TABLE bkp_anish_test'

or for the case of a dynamically built string:

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

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

Upvotes: 2

Related Questions