/ Published in: SQL
This will create a table, create an update trigger to add current datetime to modify_date field, do 2 inserts, 1 update and display the results.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/***CREATE TABLE SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Users]( [uid] [int] IDENTITY(1,1) NOT NULL, [fname] [varchar](50) NOT NULL, [lname] [varchar](50) NOT NULL, [title] [varchar](50) NULL, [manager] [varchar](2) NULL, [create_date] [datetime] NOT NULL, [modify_date] [datetime] NOT NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'User a manager or not ' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Users', @level2type=N'COLUMN',@level2name=N'manager' GO ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_manager] DEFAULT ((0)) FOR [manager] GO ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_create_date] DEFAULT (getdate()) FOR [create_date] GO ALTER TABLE [dbo].[Users] ADD CONSTRAINT [DF_Users_modify_date] DEFAULT (getdate()) FOR [modify_date] GO ***/ /**** CREATE TRIGGER usermod on dbo.Users After UPDATE AS BEGIN Update dbo.Users set modify_date = getdate() from INSERTED i Where i.uid = dbo.Users.uid END ***/ /*** TEST INSERT insert into dbo.Users (fname,lname,title,manager) values ('bob','smith','Director','1') insert into dbo.Users (fname,lname,title,manager) values ('joe','jones','Manager','1') ***/ /*** TEST UPDATE update dbo.Users set fname='joan' where uid = 2 ***/ /****** Script for SelectTopNRows command from SSMS SELECT TOP 10[uid] ,[fname] ,[lname] ,[title] ,[manager] ,[create_date] ,[modify_date] FROM [dbo].[Users] ******/