7

I am using SQL Server 2008 R2. I need one simple stored procedure to be created by default, as and when a new database is created in SQL Server. How can I achieve this? Assume the stored procedure to be created is

CREATE PROCEDURE [dbo].[Default_SP]
AS
BEGIN
    SELECT GETDATE()
END

Triggers can be applied on the table level. But this is on database create event. I searched over the net and I could not find answers.

21

Create the procedure in the system database model.

From model Database

The model database is used as the template for all databases created on an instance of SQL Server.

  • That's what I would do too. Other solutions would be to have a scheduled job looking for/creating the SP into each DB at regular intervals. Or you could try for a server-level trigger (forgot the name. DLL or DDL trigger I think) but I never did it myself. – Philippe Mar 10 '15 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.