Solved

ways to debug a check constraint which is unsing a UDF

Posted on 2014-01-17
3
664 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
ID: 39787838
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
ID: 39787914
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
ID: 39788287
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 36
SQL Error - Query 6 26
SQL Server Connection String through a VPN 8 25
divide by zero error 23 16
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

777 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