Thursday, June 20, 2013

Conditional trigger in SQL Server

Create TRIGGER trg_Student_Monitoring ON Categories AFTER UPDATE AS

IF (select COUNT(name) from students s, deleted d where s.studentId = d.studentID and s.categoryId = 4 )>=1
Begin
      INSERT INTO Students_Temp(StudentID, StudentNAME,  StudentCategory)
      SELECT  I.StudentID, I.StudentNAME,  I.StudentCategory FROM INSERTED I
End

Unable to see triggers in SSMS


Unable to find triggers in SSMS ?

Below query helps to identify whether triggers are there or not 

SELECT so.name, st.name, st.type FROM sys.triggers st LEFT OUTER join sys.objects so
ON st.parent_id = so.object_id AND st.is_ms_shipped =0 

Later we need to expand Databases - Tables - Triggers (you will be able to see triggers in this path)

 

Friday, June 7, 2013

Msg 952, Level 16, State 1, Line 1 Database is in transition offline database to online


I got the below error message when i am trying to bring the offline database to online.

Msg 952, Level 16, State 1, Line 1 Database 'xxxxxxx' is in transition.

Below solution worked for me, It may varies from environment to environment

Try to access the database from SSMS, If it gives same error message then restart the SSMS,

Sometimes restart SSMS will fix the issue, If not issue the below comand

select spid, db_name(dbid) from master..sysprocesses where dbid = dbid('db_name')

Verify are there any spids which is trying to access the database,

Issue kill on that SPID, then database will become offline

bring the database online, it works for me