Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. can't crate trigger in stored procedure

can't crate trigger in stored procedure

Scheduled Pinned Locked Moved Database
databasehelptoolsquestionannouncement
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    papy boom
    wrote on last edited by
    #1

    hi all, i want to create a trigger on table through a stored procedure but i got error in excuting the script it say syntax problem near word TRIGGER CREATE PROCEDURE CreateMyTrigger -- Add the parameters for the stored procedure here AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; CREATE TRIGGER [dbo].[InsertQuestionTrigger] ON [dbo].[Question] FOR INSERT AS UPDATE child -- set the depth of this "child" to be the -- depth of the parent, plus one. SET depth = ISNULL(parent.depth + 1,0), -- the lineage is simply the lineage of the parent, -- plus the child's ID (and appropriate '/' characters lineage = ISNULL(parent.lineage,'/') + LTrim(Str(child.QuestionId)) + '/' -- we can't update the "inserted" table directly, -- so we find the corresponding child in the -- "real" table FROM Question child INNER JOIN inserted i ON i.QuestionId=child.QuestionId -- now, we attempt to find the parent of this -- "child" - but it might not exist, so these -- values may well be NULL LEFT OUTER JOIN Question parent ON child.IdParent=parent.QuestionId END GO Any Idea and how can create trigger using function or stored procedure. Thank you in advance!

    W 1 Reply Last reply
    0
    • P papy boom

      hi all, i want to create a trigger on table through a stored procedure but i got error in excuting the script it say syntax problem near word TRIGGER CREATE PROCEDURE CreateMyTrigger -- Add the parameters for the stored procedure here AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; CREATE TRIGGER [dbo].[InsertQuestionTrigger] ON [dbo].[Question] FOR INSERT AS UPDATE child -- set the depth of this "child" to be the -- depth of the parent, plus one. SET depth = ISNULL(parent.depth + 1,0), -- the lineage is simply the lineage of the parent, -- plus the child's ID (and appropriate '/' characters lineage = ISNULL(parent.lineage,'/') + LTrim(Str(child.QuestionId)) + '/' -- we can't update the "inserted" table directly, -- so we find the corresponding child in the -- "real" table FROM Question child INNER JOIN inserted i ON i.QuestionId=child.QuestionId -- now, we attempt to find the parent of this -- "child" - but it might not exist, so these -- values may well be NULL LEFT OUTER JOIN Question parent ON child.IdParent=parent.QuestionId END GO Any Idea and how can create trigger using function or stored procedure. Thank you in advance!

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Instead of writing the trigger creation directly to the stored procedure, put it in a varchar variable. Then you can use EXECUTE[^] to execute the string.

      The need to optimize rises from a bad design.My articles[^]

      R 1 Reply Last reply
      0
      • W Wendelius

        Instead of writing the trigger creation directly to the stored procedure, put it in a varchar variable. Then you can use EXECUTE[^] to execute the string.

        The need to optimize rises from a bad design.My articles[^]

        R Offline
        R Offline
        Riaan Booyzen
        wrote on last edited by
        #3

        for example; declare @query varchar(500) set @query = "your trigger create here, I would suggest using not exists clause to prevent you from trying to create it when it is already created :)" EXECUTE @query Good luck

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups