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. trigger instead of identity

trigger instead of identity

Scheduled Pinned Locked Moved Database
databasecareer
4 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.
  • M Offline
    M Offline
    Mr Kode
    wrote on last edited by
    #1

    i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go

    D L 3 Replies Last reply
    0
    • M Mr Kode

      i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go

      D Offline
      D Offline
      DerekFL
      wrote on last edited by
      #2

      Have you tried SET @MaxVal= (select Max(Num) from hitTheatre) SET @insVal= (@MaxVal+1) Just guessing..

      1 Reply Last reply
      0
      • M Mr Kode

        i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go

        D Offline
        D Offline
        DerekFL
        wrote on last edited by
        #3

        Another solution is to run DBCC CHECKIDENT('[table name]', RESEED, 0) when you flush the table and just use an identity.

        1 Reply Last reply
        0
        • M Mr Kode

          i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go

          L Offline
          L Offline
          leoinfo
          wrote on last edited by
          #4

          You should replace *for insert* with *INSTEAD OF INSERT*. When FOR is the only keyword specified, AFTER is used by default.

          CREATE TRIGGER dbo.insertNum
          ON testIcreTrigger
          INSTEAD OF INSERT
          AS
          BEGIN
          INSERT INTO testIcreTrigger (Num)
          SELECT MAX(Num)+1 FROM hitTheatre
          END

          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