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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Restriction in INSERT

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. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/
Go to Top of Page

daipayan
Posting Yak Master

181 Posts

Posted - 2009-03-11 : 20:17:04
[code]CREATE TRIGGER dbo.Check_Marks
ON MyTable
AFTER UPDATE
AS
IF EXISTS(SELECT * FROM MyTable WHERE Student, Course, Marks IS NOT NULL)
BEGIN
RAISERROR('Please contact Admin for Updates', 16, 1)
ROLLBACK TRAN
END[/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. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/



Daipayan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-12 : 12:37:27
[code]
CREATE TRIGGER dbo.Check_Marks
ON MyTable
AFTER UPDATE
AS
IF 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 TRAN
END
[/code]
PK is your primary key
Go to Top of Page

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 )
GO
CREATE TRIGGER dbo.Check_Marks ON MyTables
FOR UPDATE
AS
BEGIN
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.ID
END
GO

INSERT 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 MyTables

UPDATE MyTables
SET MARKS = '90'
WHERE Course = 'ASM' and Student = 'Andy'
--Now You get the ERROR Message

DROP TABLE MyTables

THANKS A LOT AGAIN!
quote:
Originally posted by visakh16


CREATE TRIGGER dbo.Check_Marks
ON MyTable
AFTER UPDATE
AS
IF 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 TRAN
END

PK is your primary key



Daipayan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-13 : 13:06:12
welcome
Go to Top of Page
   

- Advertisement -