Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax for creating trigger inside a proc with condition 1

Status
Not open for further replies.

manmaria

Technical User
Aug 8, 2003
286
0
0
US
I want the syntax for something like this

create proc
if condition
then create trigger
else
do soemething.

TIA
 
Seems like a dangerous way to create TRIGGERs and a fast way to trash your databse, but here you go:
Code:
 CREATE PROCEDURE trigger_gen
AS

DECLARE @Sql VARCHAR(255)
SET @Sql = ''

IF [i]Condition[/i] = 1
    BEGIN
    SET @Sql = 'CREATE TRIGGER trigger_name
                AS
                [i]trigger stuff[/i]'
    EXECUTE (@Sql);
END

That's the basics. To my knowledge you cannot use CREATE in a StProc. You have to EXECUTE it separately as shown above. I'm sure someone else has a better way, but that will get there.

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
I know it looks like dangerous but it is not. Thanks for your response. Yes, we need to execute this temporary proc to create the trigger. We drop the temporary proc after creating the trigger. Thanks any way.

If I am doing this from the scratch, I do not go with this approach but we are migrating from some old systems to sql08. So I am just following their old methods to convert into the sql. Lets see how it goes.

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top