Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to create a ddl trigger in sql server 2005. which create some tables in database as user create a database in sql server 2005. For this i write a code, which create the tables in master table not in which i created.

Thanks

Here is my code which i use to create trigger.

SQL
Create TRIGGER [ddltrig_CreateDefaultTables]
ON ALL SERVER
FOR CREATE_DATABASE
AS
CREATE TABLE [dbo].[CategoryMaster](
[CategoryId] [int] IDENTITY(1,1) NOT NULL,
[CategoryName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [PK_CategoryMaster] PRIMARY KEY CLUSTERED 
(
[CategoryId] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
Posted
Comments
OriginalGriff 7-Aug-11 4:47am    
And your problem is?
Use the "Improve question" widget to edit your question and provide better information.

I am assuming here the problem you are running into is that the trigger gets fired whe you create a new database but instead of running under the context of the new database it runs under the master database (thus trying to create your tables in Master)?

I had to do a similar thing, except it was setup permissions on each database. In order to acomplish it, I had to build dynamic queries with the database names. Loop through each check and see if the database had the obects already created if not then create them. Of coarse my task was to check and apply the perissions where needed against all databases. So I didnt target just the new database, but all databases. It works well and insures the persmissions are always correct.

Make sense?
 
Share this answer
 
Comments
rohit_189 19-Aug-11 2:07am    
Thanks Douglas
It help me a lot as i added use [+ @dbname +] and it works for me
Hi Rohit, that's great news. Glad to have been able to help.
 
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