| Author |
Topic |
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-09-26 : 09:42:37
|
| hey guys,Im new here. I have three columsn in a table named XYZ. The column names are M,N,O,X, Y, and Z. I want to create a rule that will check if either of the three columns (X, Y or Z) have been filled in. If all three are empty then the record cant be saved. How can i do this? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-26 : 10:10:30
|
| Use COALESCE.SELECT *FROM XYZWHERE COALESCE(X, Y, Z) IS NULLOrSELECT *FROM XYZWHERE X IS NULL AND Y IS NULL OR Z IS NULLPeter LarssonHelsingborg, Sweden |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-26 : 10:56:22
|
| How do you send data to the table?If you use frint end application, check there and send only valid data to the tableMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-26 : 10:59:12
|
quote: Originally posted by Peso Use COALESCE.SELECT *FROM XYZWHERE COALESCE(X, Y, Z) IS NULLOrSELECT *FROM XYZWHERE X IS NULL AND Y IS NULL OR AND Z IS NULLPeter LarssonHelsingborg, Sweden
MadhivananFailing to plan is Planning to fail |
 |
|
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-09-26 : 13:54:11
|
| hey madhianan,thanks for the reply. it was very helpful. However i have stumbled upon yet another problem related to this query. Heres how it goes: How can i get the values of multiple columns in rules? I was Googling up rules and all i could find was the method to use single column. Let me give you an example:In an table i have many columns, three of which are X,Y & Z. I want to create a rule that checks if the sum of three columns are greater than 1000. If i do manage creating a rule and bind it to column X - how do i access the value of Y and Z from my Rule. Could some give explain. A peice of code would be really helpful.Thanks. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-09-26 : 15:18:57
|
| CREATE RULE id_chk AS @id BETWEEN 0 and 10000GOCREATE TABLE cust_sample ( cust_id int PRIMARY KEY, ref_id int, cust_name char(50), cust_address char(50), cust_credit_limit money, )GOsp_bindrule id_chk, 'cust_sample.cust_id'GO----------------------------------------------I'm sorry I didnt follow the instructions earlier X002548. I've addded the code now. My problem is, if you look at the code you'll see that the rule checks if (@id) is between 0 and 1000. Once the rule is bound to the column "cust_id" in the "cust_sample" table, the variable @id automaticaly gets the value of "cust_id" when called. Suppose I wanted to check if the value of ref_id is between 5000 and 7500 too, what modifications would i have to make to exisitng rule? Also, now that i guess i'll have two variables in my rule, how would i bind my rule to the columns. COuld you please explain now? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-26 : 19:31:41
|
[code] CREATE RULE id_chk AS @id BETWEEN 0 and 10000 OR @id BETWEEN 5000 and 7500[/code] KH |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-09-26 : 21:33:19
|
I think a CHECK constraint may be closer to what you want.alter table MyTableadd constraint CK_MyTable_Values_Correctcheck(cust_id between 0 and 10000 andref_id between 5000 and 7500) CODO ERGO SUM |
 |
|
|
mridang_agarwal
Starting Member
22 Posts |
Posted - 2006-09-26 : 23:53:12
|
| CREATE RULE id_chk AS @id BETWEEN 0 and 10000 AND @rid BETWEEN 5000 and 7500okay, how would bind this to the cust_id and ref_id column? |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2006-09-27 : 00:13:40
|
| You can't. A rule definition can only reference one value, and it's a dummy value at that. It only references a column when the rule is bound to it, not when it's defined.If you need to compare multiple columns as part of the condition then you have to use a CHECK constraint as MVJ suggested. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-27 : 01:02:29
|
I am confused.First you wanted to check if three columns are empty. Later you want to see if the sum of three columns across one record has the sum of 1000 or moreALTER TABLE [dbo].[MyTable] ADD CONSTRAINT [CK_MyCheckConstraint] CHECK ([col1] + [col2] + [col3] >= 1000)GO And now you want to see if the three columns has individual numberic ranges?ALTER TABLE [dbo].[MyTable] ADD CONSTRAINT [CK_MyCheckConstraint] CHECK (([col1] between 0 and 1000) and ([col2] between 5000 and 7500) and ([col3] between 1000 and 4123))GO Peter LarssonHelsingborg, Sweden |
 |
|
|
|