Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-03-11 : 18:25:24
|
I have table, where if data is inserted in 2 attempts, after that, no one can neither update the data in the following rows of the table,e.g.First am just inserting the following value in MyTable:-------------------------ID Student Course Marks-------------------------1 Andy ASM <NULL>2 Bob ASM <NULL>3 Andy OB <NULL>4 Bob OB <NULL>------------------------- Here, you can see, Marks column left blank, and other column being filled up, on next attempt, marks will be inserted only once and if someone wants to update any of the columns on third attempt, the TRIGGER should restrict it.How can this be done through TRIGGER?Daipayan |
|
|
guptam
Posting Yak Master
161 Posts |
Posted - 2009-03-11 : 19:21:52
|
| Hi, So you want to make it after the marks are inserted you can't make any more changes correct? Then you can create a trigger on update and check if marks were entered before change if they were roll back the change.Thanks.-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/ |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-03-11 : 20:17:04
|
[code]CREATE TRIGGER dbo.Check_MarksON MyTableAFTER UPDATEASIF EXISTS(SELECT * FROM MyTable WHERE Student, Course, Marks IS NOT NULL)BEGIN RAISERROR('Please contact Admin for Updates', 16, 1) ROLLBACK TRANEND[/code]Will this be the query??quote: Originally posted by guptam Hi, So you want to make it after the marks are inserted you can't make any more changes correct? Then you can create a trigger on update and check if marks were entered before change if they were roll back the change.Thanks.-- Mohit K. GuptaB.Sc. CS, Minor JapaneseMCITP: Database AdministratorMCTS: SQL Server 2005http://sqllearnings.blogspot.com/
Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-12 : 12:37:27
|
| [code]CREATE TRIGGER dbo.Check_MarksON MyTableAFTER UPDATEASIF EXISTS(SELECT 1 FROM INSERTED i JOIN DELETED d ON d.PK=i.PK WHERE d.Marks IS NOT NULL AND i.Marks IS NOT NULL)BEGIN RAISERROR('Please contact Admin for Updates', 16, 1) ROLLBACK TRANEND[/code]PK is your primary key |
 |
|
|
daipayan
Posting Yak Master
181 Posts |
Posted - 2009-03-12 : 15:44:02
|
THANKS VISAKH...YOU PEOPLE JUST ROCK!YOU INSPIRED ME A LOT....THE TRUE SAVIOR!I ALSO PLAYED WITH THIS and CHECK THIS RESULT:CREATE TABLE MyTables(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,Student VARCHAR(30),Course VARCHAR(4),MARKS DECIMAL(10,2),EditAttempts INT DEFAULT 1 )GOCREATE TRIGGER dbo.Check_Marks ON MyTablesFOR UPDATEASBEGIN IF EXISTS(SELECT * FROM INSERTED WHERE INSERTED.EditAttempts > 1) BEGIN RAISERROR('Please contact Admin for Updates', 16, 1) ROLLBACK TRAN END UPDATE MyTables SET MyTables.EditAttempts = MyTables.EditAttempts + 1 FROM INSERTED WHERE MyTables.ID = INSERTED.IDENDGOINSERT INTO MyTables(Student,Course)SELECT 'Andy ','ASM' UNION ALL SELECT 'Bob','ASM' UNION ALL SELECT 'Andy','OB' UNION ALL SELECT 'Bob','OB' UPDATE MyTables SET MARKS = '92' WHERE Course = 'ASM' and Student = 'Andy'SELECT * FROM MyTablesUPDATE MyTables SET MARKS = '90' WHERE Course = 'ASM' and Student = 'Andy'--Now You get the ERROR MessageDROP TABLE MyTablesTHANKS A LOT AGAIN!quote: Originally posted by visakh16
CREATE TRIGGER dbo.Check_MarksON MyTableAFTER UPDATEASIF EXISTS(SELECT 1 FROM INSERTED i JOIN DELETED d ON d.PK=i.PK WHERE d.Marks IS NOT NULL AND i.Marks IS NOT NULL)BEGIN RAISERROR('Please contact Admin for Updates', 16, 1) ROLLBACK TRANENDPK is your primary key
Daipayan |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-03-13 : 13:06:12
|
| welcome |
 |
|
|
|
|
|
|
|