You are not logged in. Your edit will be placed in a queue until it is peer reviewed.
We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
-
i looked at table level check constraints but doesnt look there is any way to pass the values being inserted or updated to the function, do you know how to ?np-hard– np-hard2009-05-14 22:11:05 +00:00Commented May 14, 2009 at 22:11
-
Okay, I posted a sample script that will help you prove what I'm talking about. I tested it and it works. If you look at the two commented lines, you'll see the message I get. Nota bene, in my implementation, I merely ensure that you cannot add a second item with the same Id which is active if there is already one active one. You could modify the logic such that if there is an active one, you cannot add any item with the same id. With this pattern, the possibilities are pretty much endless.D. Patrick– D. Patrick2009-05-14 22:24:00 +00:00Commented May 14, 2009 at 22:24
-
I'd prefer the same logic in a trigger. "a query in a scalar function... can create big problems if your CHECK constraint relies on a query and if more than one row is affected by any update. What happens is that the constraint gets checked once for each row before the statement completes. That means statement atomicity is broken and the function will be exposed to the database in an inconsistent state. The results are unpredicable and inaccurate." See: blogs.conchango.com/davidportas/archive/2007/02/19/…onedaywhen– onedaywhen2009-05-15 08:21:43 +00:00Commented May 15, 2009 at 8:21
-
That's only partially true onedaywhen. The database behaves consistently and predictably. The check constraint will execute after the row is added to the table and before the transaction is committed by the dbms and you can count on that. That blog was talking about a pretty unique problem where you need to execute the constraint against a set of inserts rather than just one insert at a time. ashish is asking for a constraint on one insert at a time and this constraint will work accurately, predictably, and consistently. I'm sorry if this sounded terse; I was running out of characters.D. Patrick– D. Patrick2009-05-15 13:34:37 +00:00Commented May 15, 2009 at 13:34
-
3This works great for inserts but doesn't seem to work for updates. E.G. Adding this after the other inserts works here when I didn't expect it to. INSERT INTO CheckConstraint VALUES (1, 'No ProblemsA', 2); update CheckConstraint set Recordstatus=1 where name = 'No ProblemsA'dwidel– dwidel2009-08-24 17:13:16 +00:00Commented Aug 24, 2009 at 17:13
|
Show 2 more comments
How to Edit
- Correct minor typos or mistakes
- Clarify meaning without changing it
- Add related resources or links
- Always respect the author’s intent
- Don’t use edits to reply to the author
How to Format
-
create code fences with backticks ` or tildes ~
```
like so
``` -
add language identifier to highlight code
```python
def function(foo):
print(foo)
``` - put returns between paragraphs
- for linebreak add 2 spaces at end
- _italic_ or **bold**
- indent code by 4 spaces
- backtick escapes
`like _so_` - quote by placing > at start of line
- to make links (use https whenever possible)
<https://example.com>[example](https://example.com)<a href="/api/flow.js?q=https%3A%2F%2Fexample.com">example</a>
How to Tag
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
- complete the sentence: my question is about...
- use tags that describe things or concepts that are essential, not incidental to your question
- favor using existing popular tags
- read the descriptions that appear below the tag
If your question is primarily about a topic for which you can't find a tag:
- combine multiple words into single-words with hyphens (e.g. python-3.x), up to a maximum of 35 characters
- creating new tags is a privilege; if you can't yet create a tag you need, then post this question without it, then ask the community to create it for you
lang-sql