October 3, 2013 at 11:54 am
Hi all
I have the following table:
CREATE TABLE usTab1
(
Col1 INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Col2 Tinyint DEFAULT (0) NOT NULL,
Col3 NVARCHAR NOT NULL
)
I must include a constraint over Col3, that column can be update only if Col2 is 0 other way don't allow.
I can do with a trigger, but I'm looking for a DDL solution directly on table.
Paulo Afonso
:crazy:
October 3, 2013 at 12:09 pm
pjcafonso (10/3/2013)
Hi allI have the following table:
CREATE TABLE usTab1
(
Col1 INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Col2 Tinyint DEFAULT (0) NOT NULL,
Col3 NVARCHAR NOT NULL
)
I must include a constraint over Col3, that column can be update only if Col2 is 0 other way don't allow.
I can do with a trigger, but I'm looking for a DDL solution directly on table.
Paulo Afonso
:crazy:
By saying update do you mean that the value for Col3 can be anything but during an update if Col2 = 0 then you can't change Col3? That can't be done with ddl. To control logic for an update like that it would have to be a trigger.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 3, 2013 at 1:15 pm
Yes, If Col2 = 0 is allow to changing Col3.
If Col2 = 1 then Col3 can't be changed, is not allowed update there value.
About DDL I mean Data Definition Language, over the Table definition!
:hehe:
October 3, 2013 at 1:33 pm
pjcafonso (10/3/2013)
Yes, If Col2 = 0 is allow to changing Col3.If Col2 = 1 then Col3 can't be changed, is not allowed update there value.
About DDL I mean Data Definition Language, over the Table definition!
:hehe:
Yes I understand DDL means. 😉
You can't define the behavior of an update with constraints on the table like that. You will have to use a trigger for this.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 3, 2013 at 1:35 pm
if you define constraint then you don't have situation for update,
because there will be no rows with impaired restriction, unless you disable constraint.
October 3, 2013 at 2:01 pm
SrcName (10/3/2013)
if you define constraint then you don't have situation for update,because there will be no rows with impaired restriction, unless you disable constraint.
???
I am curious how you would meet the requirements of the OP using a constraint.
Here is the sample table with some data.
CREATE TABLE usTab1
(
Col1 INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Col2 Tinyint DEFAULT (0) NOT NULL,
Col3 NVARCHAR(30) NOT NULL
)
insert usTab1
select 0, 'Allows change' union all
select 1, 'Can''t change'
select * from usTab1
How can you write a constraint that won't allow an update to Col3 when the value of Col2 = 0?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 3, 2013 at 2:02 pm
Thanks all
I still have the implementation on a trigger, but I'm looking for other way...
If I found anything, I keep you updated.
October 3, 2013 at 3:30 pm
Sean Lange (10/3/2013)
SrcName (10/3/2013)
if you define constraint then you don't have situation for update,because there will be no rows with impaired restriction, unless you disable constraint.
???
I am curious how you would meet the requirements of the OP using a constraint.
Here is the sample table with some data.
CREATE TABLE usTab1
(
Col1 INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Col2 Tinyint DEFAULT (0) NOT NULL,
Col3 NVARCHAR(30) NOT NULL
)
insert usTab1
select 0, 'Allows change' union all
select 1, 'Can''t change'
select * from usTab1
How can you write a constraint that won't allow an update to Col3 when the value of Col2 = 0?
i don't say that this may solve with constraint, and there isn't way to do that.
I just think step behind on this request [b]I must include a constraint over Col3, that column can be update only if Col2 is 0 other way don't allow[/b], if we define constraint on table for this requirement there would't be any update.
constraint literally
October 3, 2013 at 6:36 pm
Sean Lange (10/3/2013)
How can you write a constraint that won't allow an update to Col3 when the value of Col2 = 0?
I am not sure if this is what you and/or the OP had in mind but give it a try.
CREATE TABLE #usTab1
(
Col1 INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Col2 Tinyint DEFAULT (0) NOT NULL,
Col3 NVARCHAR(30) NOT NULL
);
insert #usTab1
select 0, 'Allows change'
union all
select 1, 'Can''t change'
;
select * from #usTab1;
ALTER TABLE #usTab1 WITH NOCHECK
ADD CONSTRAINT my_chk CHECK (Col2=0 AND Col3=Col3) ;
GO
UPDATE #usTab1
SET Col3 = 'works'
WHERE Col1 = 1;
GO
UPDATE #usTab1
SET Col3 = 'A bust'
WHERE Col1 = 2;
GO
BEGIN TRANSACTION T1;
ALTER TABLE #usTab1
DROP CONSTRAINT my_chk;
insert #usTab1 select 0, 'Allows change';
insert #usTab1 select 1, 'Can''t change';
ALTER TABLE #usTab1 WITH NOCHECK
ADD CONSTRAINT my_chk CHECK (Col2=0 AND Col3=Col3) ;
COMMIT TRANSACTION T1;
select * from #usTab1;
GO
DROP TABLE #usTab1;
I'd need to check whether the scope of the TRANSACTION covers the ALTER TABLE statements but I think it does.
Also, this won't work if you're doing a MERGE that UPDATEs and INSERTs.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 4, 2013 at 2:38 am
October 6, 2013 at 6:01 pm
Sean Pearce (10/4/2013)
Dwain, that constraint won't allow you to disable editing.
UPDATE #usTab1
SET Col2 = 1
WHERE Col1 = 1;
GO
Actually Sean, I never said it would. The OP wanted to disable changing Col3 based on the value of Col2. The CONSTRAINT I provided seems to do that.
Having said that, I consider this sort of a "stupid SQL DDL trick" and I'm not sure I'd ever use it myself. A trigger is probably the better way to do it because of the additional control it provides.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
October 7, 2013 at 10:49 am
dwain.c (10/6/2013)
Sean Pearce (10/4/2013)
Dwain, that constraint won't allow you to disable editing.
UPDATE #usTab1
SET Col2 = 1
WHERE Col1 = 1;
GO
Actually Sean, I never said it would. The OP wanted to disable changing Col3 based on the value of Col2. The CONSTRAINT I provided seems to do that.
Having said that, I consider this sort of a "stupid SQL DDL trick" and I'm not sure I'd ever use it myself. A trigger is probably the better way to do it because of the additional control it provides.
Dwain, I meant no offence but simply wanted to point out to the OP that the constraint wouldn't allow editing Col2.
Having said that, I like your "stupid SQL DDL trick" and thought it was quite clever. I would also use a trigger, it is definitely the better way to implement this type of constraint.
October 7, 2013 at 5:52 pm
Sean Pearce (10/7/2013)
Dwain, I meant no offence ...
No problem sir and none taken.
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply