Solved

ways to debug a check constraint which is unsing a UDF

Posted on 2014-01-17
3
648 Views
Last Modified: 2014-01-17
Hi, I know that you cannot use print, raise error or execute any table data changes from a UDF, but does anyone have a good approach to debug a constraint. It's rejecting all data and I can't see why.

The constraint uses a UDF. If I run the UDF on it's own, it gives me the expected outcome which would allow the check constraint to be satisfied, however in the check constraint the exact same data seems to get a different result from the UDF. The UDF returns 1 or 0 depending on if it's checks on the data are acceptable or not.

I have even hard coded the data that is tested within the UDF, so it's not that the constraint is passing different data.

it's confused me no end! I could really do with seeing what the UDF is doing using some kind of output. I know that the raiseerror, print, logging to table, etc are all out. also profiler doesn't pick up the UDF being called from the constraint.

is there anything I can do to debug this?

thanks
0
Comment
Question by:PatrickK_W
  • 3
3 Comments
 
LVL 1

Author Comment

by:PatrickK_W
Comment Utility
sorry - I should have mentioned what the function does.

I have a table of bookings. I cannot have any overlapping bookings, so the UDF lokos at the booking table and ensures that the new booking (it's just a start and end date) don't overlap with any existing records int eh booking table.

so if I run the UDF froma query it works, but inserting the same 2 dates I tested with fails.

I assume it's because it's finding the record that I have just inserted and so fails, but i'm trying to pass the PK of the new record to the UDF via the constraint and ignore that new record.

this is why I could do with seeing some debug info from the UDF - see if it is picking up the inserting row as a duplicate
0
 
LVL 1

Author Comment

by:PatrickK_W
Comment Utility
my understanding is that the order of execution is
instead of trigger
constraints
after insert trigger


so I thought this would work:
1) add a bit field as a flag in my booking table. default to 0
2) in the UDF called by constraint, exclude any records with this flag set to 0
3) in the after insert trigger set the flag field to 1

so I tested this without updating the flag to 1 (step 3) and it lets me insert the data as i'd expect.

but as soon as I added step 3 and set the inserted rows flag field to 1 the check constraint starts failing again, because it seems to be including the inserting row again in the UDF.

I thought that the constraint would see the row as having the flag field set to 0, as it is executed before the after insert trigger??
0
 
LVL 1

Accepted Solution

by:
PatrickK_W earned 0 total points
Comment Utility
I've re-writtenteh check constraint into an after trigger. this has allowed tme to get some fedbcak and print out variable etc.it also works without any changes, so I think i'll leave it like that!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now