Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
below is my code..
USE [PRSAppTest_TST]
GO
/****** Object:  Trigger [dbo].[trg_URPatients]    Script Date: 04/26/2013 03:54:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_URPatients] 
  ON  [dbo].[Patients]
  FOR INSERT
AS 
 
BEGIN
 
DECLARE @PCPID varchar(50)
DECLARE @Cnt int
DECLARE @PatientID varchar(50)
 
SET @PatientID =  (SELECT top(1) PatientID from INSERTED)
SET @PCPID = (select top(1) UserId from users where PCPGroupID=(SELECT top(1) PCPID from INSERTED))
if @PatientID =NULL
begin
return
end
else
begin
insert into UserRef(PatientID,AssignedBy,AssignedTo,ReasonOfAccs,EndDate) 
values(@PatientID,@PCPID,@PCPID,'PCP',Dateadd(d,60,GETDATE()))
end
 
END
Here above is my code...is that write.
Posted 26-Apr-13 2:30am
ntitish1.5K
Edited 26-Apr-13 2:32am
RyanDev125K
v2
Comments
ryanb31 at 26-Apr-13 7:33am
   
What's your question?
ntitish at 26-Apr-13 7:39am
   
@PatientID = (SELECT top(1) PatientID from INSERTED)
 
if i got above @patientID=null means it should return else it should go into else part. but my query is showing wrong
ntitish at 26-Apr-13 7:46am
   
sir i had one more dought...can we alter the trigger code
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
You are Compaing NULL in wrong way. Use "IS NULL" for NULL Comparision not "=".
Check the links...
Comparison Operators (Transact-SQL)[^]
SQL Operators[^]
 
-- I changed your Trigger Code. Check the following Script....
 
USE [PRSAppTest_TST]
GO
/****** Object:  Trigger [dbo].[trg_URPatients]    Script Date: 04/26/2013 03:54:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_URPatients] 
  ON  [dbo].[Patients]
  FOR INSERT
AS 
 
BEGIN
 
DECLARE @PCPID varchar(50)
DECLARE @Cnt int
DECLARE @PatientID varchar(50)
 
SET @PatientID =  (SELECT top(1) PatientID from INSERTED)
SET @PCPID = (select top(1) UserId from users where PCPGroupID=(SELECT top(1) PCPID from INSERTED))
if @PatientID IS NULL
begin
return
end
else
begin
insert into UserRef(PatientID,AssignedBy,AssignedTo,ReasonOfAccs,EndDate) 
values(@PatientID,@PCPID,@PCPID,'PCP',Dateadd(d,60,GETDATE()))
end
 
END
GVPrabu
  Permalink  
v3
Comments
ntitish at 26-Apr-13 8:37am
   
thanks sir....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i changed you code:
 
USE [PRSAppTest_TST]
GO
/****** Object:  Trigger [dbo].[trg_URPatients]    Script Date: 04/26/2013 03:54:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trg_URPatients] 
  ON  [dbo].[Patients]
  FOR INSERT
AS 
 
BEGIN
 
DECLARE @PCPID varchar(50)
DECLARE @Cnt int
DECLARE @PatientID varchar(50)
 
Select @PatientID =  top(1) PatientID from INSERTED
SELECT @PCPID = top(1) UserId from users where PCPGroupID=(SELECT top(1) PCPID from INSERTED)
if @PatientID is NULL
begin
return 
end
else
begin
insert into UserRef(PatientID,AssignedBy,AssignedTo,ReasonOfAccs,EndDate) 
values(@PatientID,@PCPID,@PCPID,'PCP',Dateadd(d,60,GETDATE()))
end
 
END
  Permalink  
v2
Comments
ntitish at 26-Apr-13 8:38am
   
thanks sir...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 26 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100