0

This script fails to execute from PowerShell (sqlcmd) and directly from SQL Management Studio:

IF db_id('BANANATEST') is not null
 BEGIN   
  print 'DB BANANATEST already exists. NO ACTION TAKEN.'
 END
ELSE
 BEGIN
  print 'DB BANANATEST does NOT exist.'
  print 'Restoring a backup of BANANATEST'
  RESTORE DATABASE BANANATEST
   FROM DISK = 'BANANATEST.original.bak' ;

  print 'Creating User BANANATEST'
  USE master
  If Exists (select loginname from master.dbo.syslogins where name ='BANANATEST')
  Begin
   DROP LOGIN [BANANATEST]
  END
  CREATE LOGIN [BANANATEST] WITH PASSWORD=N'BANANATEST', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
  USE BANANATEST
  ALTER USER [BANANATEST] WITH LOGIN = [BANANATEST]
END

The error is Syntax.

Msg 911, Level 16, State 1, Line 20 Database 'BANANATEST' does not exist. Make sure that the name is entered correctly.

While it is accurate, BANANATEST DB doesn't exist in the moment of query "compilation"... SQL Server doesn't realise that database WILL exist when it reaches this line.

How can I make it work? Either by ignoring syntax checks or whatever it make it work...

NOTE: The backup comes with a BANANATEST usr and I want to cover two scenarios:

  1. LOGIN already exists in SQL Server
  2. LOGIN does not exist
4
  • 1
    Use dynamic SQL so you don't get a compilation error when running this code. Commented Apr 25, 2016 at 16:25
  • Take this one step at a time. Commented Apr 25, 2016 at 16:29
  • Separate the RESTORE and USE into separate batches using a GO Commented Apr 25, 2016 at 16:34
  • I cannot use GO inside a BEGIN END :-( Commented Apr 25, 2016 at 16:34

1 Answer 1

1

You can get around the go with #temp tables

CREATE table #temp12 (restore1  bit)
IF db_id('BANANATEST') is not null
 BEGIN   
  print 'DB BANANATEST already exists. NO ACTION TAKEN.'
 END
ELSE
 BEGIN
  print 'DB BANANATEST does NOT exist.'
  print 'Restoring a backup of BANANATEST'
  RESTORE DATABASE BANANATEST
   FROM DISK = 'BANANATEST.original.bak' ;
  INSERT INTO #temp12 VALUES ('1')
  print 'Creating User BANANATEST'
 END 
 GO
  USE master
  IF EXISTS (SELECT 1 from #TEMP12)
      BEGIN
      If Exists (select loginname from master.dbo.syslogins where name ='BANANATEST')
      Begin
       DROP LOGIN [BANANATEST]
      END
      CREATE LOGIN [BANANATEST] WITH PASSWORD=N'BANANATEST', DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
      USE BANANATEST
      ALTER USER [BANANATEST] WITH LOGIN = [BANANATEST]
  END
DROP TABLE #temp12
Sign up to request clarification or add additional context in comments.

1 Comment

It works ok. I edited your answer as there usually a #TEMP db in sql server already and restore is a reserverd word...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.