Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Update Columns dependency over other column on same table Expand / Collapse
Author
Message
Posted Thursday, October 03, 2013 11:54 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, February 08, 2014 6:53 AM
Points: 14, Visits: 42
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
Post #1501307
Posted Thursday, October 03, 2013 12:09 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
pjcafonso (10/3/2013)
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


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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1501313
Posted Thursday, October 03, 2013 1:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, February 08, 2014 6:53 AM
Points: 14, Visits: 42
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!

Post #1501326
Posted Thursday, October 03, 2013 1:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
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!



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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1501331
Posted Thursday, October 03, 2013 1:35 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 12:41 AM
Points: 58, Visits: 308
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.
Post #1501333
Posted Thursday, October 03, 2013 2:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1501344
Posted Thursday, October 03, 2013 2:02 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, February 08, 2014 6:53 AM
Points: 14, Visits: 42
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.

Post #1501345
Posted Thursday, October 03, 2013 3:30 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 12:41 AM
Points: 58, Visits: 308
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 [I must include a constraint over Col3, that column can be update only if Col2 is 0 other way don't allow], if we define constraint on table for this requirement there would't be any update.
constraint literally
Post #1501364
Posted Thursday, October 03, 2013 6:36 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 8:24 PM
Points: 3,589, Visits: 5,095
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1501394
Posted Friday, October 04, 2013 2:38 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 11:49 AM
Points: 739, Visits: 2,470
Dwain, that constraint won't allow you to disable editing.

UPDATE #usTab1
SET Col2 = 1
WHERE Col1 = 1;
GO





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Post #1501465
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse