Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
/**********************************************************************/
/* Install.SQL                                                        */
/* Creates a login and makes the user a member of db_owner            */
/*                                                                    */
/**********************************************************************/

-- Declare variables for database name, username and password
DECLARE @dbName sysname,
      @dbUser sysname,
      @dbPwd nvarchar(max);

-- Set variables for database name, username and password
SET @dbName = 'PlaceholderForDbName';
SET @dbUser = 'PlaceholderForDbUsername';
SET @dbPwd = 'PlaceholderForDbUserPassword';

DECLARE @cmd nvarchar(max)

-- Create login
IF( SUSER_SID(@dbUser) is null )
BEGIN
    print '-- Creating login '
    SET @cmd = N'CREATE LOGIN ' + quotename(@dbUser) + N' WITH PASSWORD ='''+ replace(@dbPwd, '''', '''''') + N''''
    EXEC(@cmd)
END

-- Create database user and map to login
-- and add user to the datareader, datawriter, ddladmin and securityadmin roles
--
SET @cmd = N'USE ' + quotename(@DBName) + N'; 
IF( NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = ''' + replace(@dbUser, '''', '''''') + N'''))
BEGIN
    print ''-- Creating user'';
    CREATE USER ' + quotename(@dbUser) + N' FOR LOGIN ' + quotename(@dbUser) + N';
    print ''-- Adding user'';
    EXEC sp_addrolemember ''db_owner'', ''' + replace(@dbUser, '''', '''''') + N''';
END'
EXEC(@cmd)
GO

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

help me with this error what caused this error PlaceholderForDbName is assignrd to a varibale then why database error
Posted
Updated 3-Oct-13 22:29pm
v2

Hi Varun,

You are trying to connect to the non existing database that is why the error is thrown.
In @cmd variable you are trying to use non existing database.

You can recreate the error if you want by typing the below command in sql server query window
SQL
USE databasenotexist;

now you can see the error in your sql server studio

Try hard coding the database name in this line like this
SQL
SET @dbName = 'your database name here';

and check it.

Regards,
RK
 
Share this answer
 
Probably, because what it actually wants there is the name of your database

This is just a guess, based on the actual string the variable contains: "PlaceholderForDbName" - which is (I think) meant to be a clue to you as to what the heck you are supposed to put it in...

At another guess: when you have fixed that, you should probably look for any other instances of the word "Placeholder" in that code...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900