Author |
Topic |
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-12-08 : 05:37:11
|
Hi,I have the code below, my CREATE LOGIN statement seems to be generating an error, what is the correct syntax?Incorrect syntax near 'test1234'.Thanksif not exists(select dbid from master..sysdatabases where name = @dbName)-- CREATE SERVER LOGINEXEC ('CREATE LOGIN ' + @dbUsername + ' WITH PASSWORD = ' + @dbPassword)-- CREATE DATABASEEXEC ('CREATE DATABASE ' + @dbName)-- CREATE DB USEREXEC ('USE ' + @dbName)EXEC ('CREATE USER ' + @dbUsername + ' FOR LOGIN ' + @dbUserName + ' WITH DEFAULT_SCHEMA = db_owner') |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-08 : 05:39:19
|
try printing the sql strings using PRINT () and see for any syntax errors before excuting them with EXEC() |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-12-08 : 05:48:23
|
This is what is being outputCREATE LOGIN testUser WITH PASSWORD = Password1234CREATE DATABASE dbTestUSE dbTestCREATE USER testUser FOR LOGIN testUser WITH DEFAULT_SCHEMA = db_ownerThanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-08 : 06:04:20
|
[code]if not exists(select dbid from master..sysdatabases where name = @dbName)-- CREATE SERVER LOGINEXEC ('CREATE LOGIN ' + @dbUsername + ' WITH PASSWORD = ''' + @dbPassword+'''')-- CREATE DATABASEEXEC ('CREATE DATABASE ' + @dbName)-- CREATE DB USEREXEC ('USE ' + @dbName)EXEC ('CREATE USER ' + @dbUsername + ' FOR LOGIN ' + @dbUserName + ' WITH DEFAULT_SCHEMA = db_owner')[/code] |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-12-08 : 06:16:12
|
Thanks!Its working fine, apart from the last line - it creates the user in the current database, I want to create it in the new database I just created - it seems the USE @dbName doesn't work?Thanks |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-08 : 06:26:07
|
The use works, but the USE is only effective within that EXEC.TryEXEC ('USE ' + @dbName + '; CREATE USER ' + @dbUsername + ' FOR LOGIN ' + @dbUserName + ' WITH DEFAULT_SCHEMA = db_owner')--Gail ShawSQL Server MVP |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-12-08 : 06:37:26
|
That works great, thanks.One last question, i've added this -- ADD TO ROLESEXEC sp_addrolemember 'db_owner', @dbUsernameHow can I get that to work with the newly created DB?Thanks |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-08 : 08:16:29
|
Same as the create userEXEC ('USE ' + @dbName + '; CREATE USER ' + @dbUsername + ' FOR LOGIN ' + @dbUserName + ' WITH DEFAULT_SCHEMA = db_owner; exec sp_addrolemember 'db_owner', ''' + @dbUsername + '''')--Gail ShawSQL Server MVP |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2008-12-08 : 09:35:16
|
But this time i'm calling another procedure - would it be like this? (i.e. Nested EXEC's?)EXEC('USE ' + @dbName +'; EXEC sp_addrolemember ''db_owner''', ' + @dbUsername) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-08 : 09:42:31
|
quote: Originally posted by Mondeo But this time i'm calling another procedure - would it be like this? (i.e. Nested EXEC's?)EXEC('USE ' + @dbName +'; EXEC sp_addrolemember ''db_owner''', ''' + @dbUsername+'''')
should be like above |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-08 : 09:53:33
|
I forgot to escape the quotes around the dbowner, otherwise, how I had it will work. No need for a second exec.EXEC ('USE ' + @dbName + '; CREATE USER ' + @dbUsername + ' FOR LOGIN ' + @dbUserName + ' WITH DEFAULT_SCHEMA = db_owner; EXEC sp_addrolemember ''db_owner'', ''' + @dbUsername + '''')--Gail ShawSQL Server MVP |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-08 : 09:55:35
|
quote: Originally posted by visakh16
quote: Originally posted by Mondeo But this time i'm calling another procedure - would it be like this? (i.e. Nested EXEC's?)EXEC('USE ' + @dbName +'; EXEC sp_addrolemember ''db_owner''', ''' + @dbUsername+'''')
should be like above
One too many quotes after the db_owner. Highlighted quote shouldn't be there.--Gail ShawSQL Server MVP |
|
|
|