December 17, 2003 at 10:02 am
Hi,
I'd like to use a udf or a sproc to reduce repetition in a trigger.
<code>
IF EXISTS(SELECT 1 FROM INSERTED
WHERE tblClientId = (SUBSELECT))
<\code>
...no problems
HOWEVER, If i substitute the SUBSELECT for a UDF/sproc that contains exactly the same syntax and return values, I get an error.
I have prefixed the udf/sproc with the name of the database (as I understand that triggers reside in the "master" database)
still no joy.
do I have to keep repeating sub selects in my triggers?
ta,
yogi
December 17, 2003 at 10:29 am
Are you prefixing your UDF with dbo. ?
December 17, 2003 at 10:36 am
hi,
yes, it errors:
"the column prefix 'dbo' does not match with a table name or alias used in the query"
ta, yogi
December 17, 2003 at 10:53 am
I've never used a UDF inside a trigger, but I used SPs inside triggers all the time. Never had a problem.
What's your subselect, and what's the error?
December 17, 2003 at 12:13 pm
Hi,
SUBSELECT
<code>
IF EXISTS(SELECT 1 FROM INSERTED
WHERE jobTypeId =
(SELECT jobTypeId
FROM tblJob
WHERE jobType = 'cleanUp'))
<\code>
sproc
<code>
IF EXISTS(SELECT 1 FROM INSERTED
WHERE jobTypeId =
(dbo.spJobTypeIdPerJobName('cleanUp'))
<\code>
cheers,
yogi
December 19, 2003 at 3:08 am
Store proc cannot reference to INSERTED table at Trigger. INSERTED table will not be identified outside of the scope of the trigger itself.
One of the methods is using Temporary Table. Store procedure can access Temporary Table created in the same session. Here is the example:
/* TRIGGER */
CREATE TRIGGER T_B4_INSERT
INSTEAD OF INSERT
AS
SELECT * INTO #TMPTBL FROM INSERTED
EXEC DBO.EXECATSTOREPROC
/* STORE PROC */
CREATE PROCEDURE DBO.EXECATSTOREPROC
BEGIN
IF EXISTS(SELECT 1 FROM #TMPTBL
WHERE jobTypeId =
(SELECT jobTypeId
FROM tblJob
WHERE jobType = 'cleanUp'))
PRINT 'HELLO WORLD'
END
*The syntacs of above codes will be incorrect as the author do not test it out in the real environment. User(s) may need to do some modification to the codes before it work!
Regards,
kokyan
Regards,
kokyan
December 19, 2003 at 2:30 pm
Thanks kokyan,
I'll try that..
yogi
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply