Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2004, 02:51 PM
Registered User
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tricks with Constraints

I have an app with a UI that allows a user to input a blank string, i.e. ''. These columns have Not Null attributes, but the blank string bypasses that catch, of course. This data is later sent to other apps that do not like the ''.

Is there a way I can trap the '' and change it to 'Unknown', at the same time leaving the Not Null attribute in place and allowing other valid data to pass through? Can I do this with a constraint, or am I limited to a trigger to manipulate the data?

TIA
Elizabeth
 
Old May 26th, 2004, 03:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

In the first place why don't you check that from the UI front? It is better that you don't let blank strings get submitted from the UI, than sending blank strings and then checking the same again at the backend for validity. Why do you have to hit the backend to validate data, when you know you should not let blank strings pass on to database.

I will not say constraints would help you do that.

Hope that helps.
Cheers!

_________________________
-Vijay G
Strive for Perfection
 
Old May 26th, 2004, 04:11 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could create a constraint that dissallows ''. I just created one that looks like this: ([CharColumn] <> '')

When I tried to insert '', it failed. This of course doesn't help you to change the blank to 'unknown' automatically. For that you could only use a trigger or some logic in the UI.

Peter
-------------------------
Work smarter, not harder
 
Old June 6th, 2004, 05:28 AM
Authorized User
 
Join Date: Jun 2003
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I would do this in the UI, or even better in the BO layer (business objects layer). Something like: just before saving, if TheField = '' then set TheField to 'Unknown'. Also, i would add a constraint for not allowing empty values for TheField column. This ensures that the column will not contain empty values, even if the values are not inputed through your app that does the replacing of '' with 'Unknown'. And so the other app that doesn't accept empty values, will not break.

I think the user should be aware of the fact that empty values get replaced with 'Unknown', so that when the user wants to filter the rows, will know it has to say [TheField] = 'Unknown' and not [TheField] = '' which will return 0 rows.

In fact i think the best solution is to have an initial value of 'Unknown' for TheField for all new rows, and the user will be able to leave it as is, and save the row, or to enter another value for TheField. Also there should be a broken rule for not accepting empy values for [TheField].

i'm sorry for my english...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dropping constraints epp_b MySQL 2 September 12th, 2006 09:22 PM
How do you suspend constraints? Aaron Edwards SQL Server 2005 2 July 31st, 2006 12:16 PM
Tips/Tricks - Record Finder Code JeffChr BOOK: Access 2003 VBA Programmer's Reference 2 January 6th, 2005 08:07 PM
Rules/Constraints dhay1999 SQL Server 2000 1 July 11th, 2004 01:50 PM
ASP Speed Tricks humbads Classic ASP Databases 0 July 23rd, 2003 03:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.