473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Understanding constraints and binding

I'm implementing some database formatting and I need that values within
a column have certain limits ... let's say for example, they shouldn't
be <0 or >10000, but in the case I'm inserting values bigger then 10000
I would like that MSSQL "clip" this value to the upper limit (10000 in
this case) and the same with the lower limit (zero in this case).
Is that possible? or SQL just respond me with an error when the values
go beyond those limits and will abort the transaction?
Can someone put some light on this please???

Nacho

Aug 7 '06 #1
5 1334
Nacho (na*********@gmail.com) writes:
I'm implementing some database formatting and I need that values within
a column have certain limits ... let's say for example, they shouldn't
be <0 or >10000, but in the case I'm inserting values bigger then 10000
I would like that MSSQL "clip" this value to the upper limit (10000 in
this case) and the same with the lower limit (zero in this case).
Is that possible? or SQL just respond me with an error when the values
go beyond those limits and will abort the transaction?
Can someone put some light on this please???
You would need a trigger:

CREATE TRIGGER tri ON tbl FOR INSERT, UPDATE AS
UPDATE tbl
SET col = CASE WHEN t.col < 0 THEN 0
WHEN t.col 10000 THEN 0
ELSE t.col
END
FROM tbl t
JOIN inserted i ON t.keycol = i.keycol

If you have a constraint, you would have to drop that constraint. Or
implement an INSTEAD OF trigger instead.

I would question the wise in destroying data in this way, though. Better
would be to accept the data as-is, and then handle it in the query.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 7 '06 #2
I would only add that a WHERE clause on the UPDATE in the trigger
would save updates when the value is already within the range:

WHERE t.col < 0 OR T.col 10000

Roy Harvey
Beacon Falls, CT

On Mon, 7 Aug 2006 11:06:31 +0000 (UTC), Erland Sommarskog
<es****@sommarskog.sewrote:
>Nacho (na*********@gmail.com) writes:
>I'm implementing some database formatting and I need that values within
a column have certain limits ... let's say for example, they shouldn't
be <0 or >10000, but in the case I'm inserting values bigger then 10000
I would like that MSSQL "clip" this value to the upper limit (10000 in
this case) and the same with the lower limit (zero in this case).
Is that possible? or SQL just respond me with an error when the values
go beyond those limits and will abort the transaction?
Can someone put some light on this please???

You would need a trigger:

CREATE TRIGGER tri ON tbl FOR INSERT, UPDATE AS
UPDATE tbl
SET col = CASE WHEN t.col < 0 THEN 0
WHEN t.col 10000 THEN 0
ELSE t.col
END
FROM tbl t
JOIN inserted i ON t.keycol = i.keycol

If you have a constraint, you would have to drop that constraint. Or
implement an INSTEAD OF trigger instead.

I would question the wise in destroying data in this way, though. Better
would be to accept the data as-is, and then handle it in the query.
Aug 7 '06 #3
Roy Harvey (ro********@snet.net) writes:
I would only add that a WHERE clause on the UPDATE in the trigger
would save updates when the value is already within the range:

WHERE t.col < 0 OR T.col 10000

Good point! Thanks, Roy!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 7 '06 #4
Thanks to both of you!!!
It's good point to handle it in the query (I suposse when you read
values after) but then, how it would be? How can I clip values in the
select statement ?

Erland Sommarskog wrote:
Roy Harvey (ro********@snet.net) writes:
I would only add that a WHERE clause on the UPDATE in the trigger
would save updates when the value is already within the range:

WHERE t.col < 0 OR T.col 10000


Good point! Thanks, Roy!
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 7 '06 #5
Nacho (na*********@gmail.com) writes:
Thanks to both of you!!!
It's good point to handle it in the query (I suposse when you read
values after) but then, how it would be? How can I clip values in the
select statement ?
With a CASE expression very similar to the one I had in my
UPDATE statement in my sample trigger.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: bruce stockwell | last post by:
Using 'self' in classes seems pretty straight forward. My curiosity is why I have to use it. Shouldn't it be implied? If I create an instance of 'human' called 'bruce' and call the method 'blink'...
2
by: Paul | last post by:
Hi: I am not sure whether somebody can sort it out for me. I am doing data transfer from one oracle database to another. Both of them has the same structure (like same tables etc).the only...
0
by: Rajesh Kapur | last post by:
We use InnoDB tables and foreign key constraints extensively. The mysqldump backs up the database tables in alphabetical order with foreign key constraints defined in the create statement of each...
4
by: Dmitri | last post by:
I just looked at a coworker's stored procedure and this person is dropping 4 Foreign key constraints and then re-adding them after processing the required logic (updating rows in the 4 tables in...
2
by: DW | last post by:
Greetings: I have to do a one-off forceful change of some data in a database. I need to disable some FK constraints, make the data change, and then re-enable the constraints. My process will...
10
by: serge | last post by:
I am doing a little research on Google about this topic and I ran into this thread: ...
0
by: BobTheDatabaseBoy | last post by:
i've Googled some this morning, but to my surprise, i don't find any offering (for fee or open source), which would integrate with, say Jakarta Struts, to provide the UI edits from cataloged...
0
by: webmaster | last post by:
Pardon my being a total C# noob. I'm trying to take apart the dotNet Time Tracker dotNet C# starterkit sample application and replicate a part of the code. At a high level, I have a very...
13
by: Chris Carlen | last post by:
Hi: I have begun learning Python by experimenting with the code snippets here: http://hetland.org/writing/instant-python.html In the section on functions, Magnus Lie Hetland writes: ...
4
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.