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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need A MSSQL Trigger

Need A MSSQL Trigger

Scheduled Pinned Locked Moved Database
databasesql-serverhelpquestionannouncement
8 Posts 3 Posters 0 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.
  • I Offline
    I Offline
    idumlupinar
    wrote on last edited by
    #1

    Hello guys, i am using a module. I want to run a query whenever user updates an article. Reason is: During article update Fileid becomes FileID under ImageURL column, and this causes broken image paths for my case. I want to keep this Fileid thing same whenever an update occurs at database table. Following trigger works for new entries: CREATE TRIGGER FileID_replace ON Articles AFTER INSERT AS BEGIN update Articles set ImageUrl=REPLACE(ImageUrl,'FileID','Fileid') where ArticleID = (select max(ArticleID) as ArticleID from Articles) END However i need a one to keep Fileid whenever an update occurs. Would you please help? Thank you.

    N M 2 Replies Last reply
    0
    • I idumlupinar

      Hello guys, i am using a module. I want to run a query whenever user updates an article. Reason is: During article update Fileid becomes FileID under ImageURL column, and this causes broken image paths for my case. I want to keep this Fileid thing same whenever an update occurs at database table. Following trigger works for new entries: CREATE TRIGGER FileID_replace ON Articles AFTER INSERT AS BEGIN update Articles set ImageUrl=REPLACE(ImageUrl,'FileID','Fileid') where ArticleID = (select max(ArticleID) as ArticleID from Articles) END However i need a one to keep Fileid whenever an update occurs. Would you please help? Thank you.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      So create an UPDATE trigger. What difficulties are you having?


      I know the language. I've read a book. - _Madmatt

      I 1 Reply Last reply
      0
      • N Not Active

        So create an UPDATE trigger. What difficulties are you having?


        I know the language. I've read a book. - _Madmatt

        I Offline
        I Offline
        idumlupinar
        wrote on last edited by
        #3

        Can you tell me which query to use? I tried one, but it works for only latest entry. If editing a previous entry, it does not work. Because my query was relying on increase on articleId.

        N 1 Reply Last reply
        0
        • I idumlupinar

          Can you tell me which query to use? I tried one, but it works for only latest entry. If editing a previous entry, it does not work. Because my query was relying on increase on articleId.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          Maybe you'd like to show what you have tried.


          I know the language. I've read a book. - _Madmatt

          I 1 Reply Last reply
          0
          • N Not Active

            Maybe you'd like to show what you have tried.


            I know the language. I've read a book. - _Madmatt

            I Offline
            I Offline
            idumlupinar
            wrote on last edited by
            #5

            Sure: (I simply changed AFTER CREATE with AFTER UPDATE) But i guess i am missing something. I tried following: CREATE TRIGGER FileID_replace ON Articles AFTER UPDATE AS BEGIN update Articles set ImageUrl=REPLACE(ImageUrl,'FileID','Fileid') where ArticleID = (select max(ArticleID) as ArticleID from Articles) END GO

            N 1 Reply Last reply
            0
            • I idumlupinar

              Sure: (I simply changed AFTER CREATE with AFTER UPDATE) But i guess i am missing something. I tried following: CREATE TRIGGER FileID_replace ON Articles AFTER UPDATE AS BEGIN update Articles set ImageUrl=REPLACE(ImageUrl,'FileID','Fileid') where ArticleID = (select max(ArticleID) as ArticleID from Articles) END GO

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              Of course these queries are only going to work for the last record, that's what you're asking for.

              idumlupinar wrote:

              select max(ArticleID)

              If you read the documentation for triggers you will understand. Query the inserted or updated to get the record currently being affected.


              I know the language. I've read a book. - _Madmatt

              I 1 Reply Last reply
              0
              • N Not Active

                Of course these queries are only going to work for the last record, that's what you're asking for.

                idumlupinar wrote:

                select max(ArticleID)

                If you read the documentation for triggers you will understand. Query the inserted or updated to get the record currently being affected.


                I know the language. I've read a book. - _Madmatt

                I Offline
                I Offline
                idumlupinar
                wrote on last edited by
                #7

                Well, i am not a SQL Developer yet, i am trying to learn anyway :) Can you share an url that would explain my case clearly? I reviewed a few resources but i couldn't understand most of them :(

                1 Reply Last reply
                0
                • I idumlupinar

                  Hello guys, i am using a module. I want to run a query whenever user updates an article. Reason is: During article update Fileid becomes FileID under ImageURL column, and this causes broken image paths for my case. I want to keep this Fileid thing same whenever an update occurs at database table. Following trigger works for new entries: CREATE TRIGGER FileID_replace ON Articles AFTER INSERT AS BEGIN update Articles set ImageUrl=REPLACE(ImageUrl,'FileID','Fileid') where ArticleID = (select max(ArticleID) as ArticleID from Articles) END However i need a one to keep Fileid whenever an update occurs. Would you please help? Thank you.

                  M Offline
                  M Offline
                  Md Marufuzzaman
                  wrote on last edited by
                  #8

                  Try like this:

                  DECLARE @MyId VARCHAR(50)
                  SET @MyId = SELECT ID FROM INSERTED
                  UPDATE ........ WHERE ID = @MyId

                  Thanks Md. Marufuzzaman


                  I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

                  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