1

As the title might have explained, i have a php project here. Inside i use PDO to connect to my database. At first PHP was connected to a MySQL-Database. But now things have changed and I use a MsSQL-Database.

In MySQL you can easily do the following:

DROP DATABASE IF EXISTS mydatabase;

How can this be achieved with a MsSQL Database?

I have tried to do something like this:

IF EXISTS(select * from sys.databases where name='mydatabase')
DROP DATABASE mydatabase

When i do so i get the following error

Doctrine\DBAL\Driver\PDOException: SQLSTATE[42S22]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Ungültiger Spaltenname 'mydatabase'.

But it doesnt work. It think this is related to the fact, that a PDO Connection is tied to a specific database and in my case i create a connection to database called mydatabase.

Im thankful for every hint you can give me.

Max

Max Schindler
  • 380
  • 2
  • 17
  • Any errors? Your query looks valid to me – simon May 03 '17 at 09:21
  • you are doing these in localhost, right? – Nirav Madariya May 03 '17 at 09:29
  • No, on our company sql server. – Max Schindler May 03 '17 at 11:19
  • Meanwhile i found out, that it is possible to connect via PDO without defining the database. new PDOConnection('sqlsrv:Server=myhost', $dbUser, $dbPass); But when i call self::$pdoConnection->exec(''DROP DATABASE mydatabase'); I still get errors: Doctrine\DBAL\Driver\PDOException: SQLSTATE[42000]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Die mydatabase-Datenbank kann nicht gelöscht werden, da sie zurzeit verwendet wird. – Max Schindler May 03 '17 at 13:00

0 Answers0