Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Database, user, login creation

Author  Topic 

SinanP
Starting Member

1 Post

Posted - 2009-09-25 : 16:16:45
I have a script that creates the database, user, login and the tables.


I need databse name to be a variable so i dont need to write it a few times.

What I am trying is below, and obviuosly it is wrong.

--DATABASE NAME
declare @domain varchar(30)
set @domain = 'test'

declare @db varchar(50)
set @db = 'isharedocs_' + @domain

PRINT 'Domain name declared'


--CREATE USER
CREATE LOGIN @db
PASSWORD = 'AndyhAsgoAts'
DEFAULT_DATABASE = @db;

PRINT 'New Login Created'

USE @db

CREATE USER @db
FOR LOGIN @db
WITH DEFAULT_SCHEMA = dbo;

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-25 : 16:38:13
You need to use dynamic SQL for this. Search for "dynamic SQL" here or on Google and you'll get plenty of hits with examples.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -