Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update Columns dependency over other column on same table


Update Columns dependency over other column on same table

Author
Message
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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
Crazy
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16523 Visits: 16991
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
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 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)
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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!

Hehe
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16523 Visits: 16991
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 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)
SrcName
SrcName
SSC-Enthusiastic
SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)

Group: General Forum Members
Points: 122 Visits: 394
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16523 Visits: 16991
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)
pjcafonsosemefeito
pjcafonsosemefeito
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
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.
SrcName
SrcName
SSC-Enthusiastic
SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)SSC-Enthusiastic (122 reputation)

Group: General Forum Members
Points: 122 Visits: 394
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
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4239 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sean Pearce
Sean Pearce
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1168 Visits: 3432
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search