Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop tables with dashes in the name

I need to delete all tables which name has the pattern '%-%-%-%-%'. The best way to do it I found so far is to do it in 2 steps:

  1. Generate the deletion request as by calling the request below

    SELECT ' DROP TABLE ' + REPLACE(TABLE_NAME, '-', NCHAR(8211)) FROM information_schema.tables where TABLE_NAME like '%-%-%-%-%' ORDER BY TABLE_NAME

  2. Run the deletion request.

Unfortunately step 2. does not work. Even if I REPLACE(TABLE_NAME, '-', NCHAR(8211)), I still get Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '0'. etc.; 0 appreas in the message because the first line of deletion request is DROP TABLE 0df78d48–c718–49be–ab55–5f158ebdd64c–results.

Could you help? Thanks!

like image 769
Yulia V Avatar asked Dec 01 '25 04:12

Yulia V


1 Answers

Placing square brackets around the table names should give you the result you want.

Example:

DROP TABLE [0df78d48–c718–49be–ab55–5f158ebdd64c–results]
like image 149
SQLGuru Avatar answered Dec 02 '25 18:12

SQLGuru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!