Showing posts with label constraint. Show all posts
Showing posts with label constraint. Show all posts

Tuesday, March 27, 2012

Changing column data type constraint

I am trying to change the data type of two columns in a SQL database.The columns were created using the“smallint” data type.Since they are used to identify document and document sections rather than mathematical functions, I think they should have been constrained to a text data type such as nvarchar.When I try to concatenate with a query the result is a mathematical addition of the numbers, butI am trying to combine the two numbers as a string with a "-" between them.

I have not had any success in changing the data type of the two columns.Apparently, the original database was set up for full text search and won’t let me change the column data type.I keep getting this error message:

'Full Documents' table

- Unable to modify table.

Timeout expired.The timeout period elapsed prior to completion of the operation or the server is not responding.

The statement has been terminated.

I ran a query to increase the timeout period (which succeeded in increasing the timeout but still got the same error message when trying to change the column data type). My research suggests that this is really a matter of the column data constraints related to the full text search issue.

Any suggestions on how to change the data type of these columns?

Since this is a general SQL question, I'm moving it to a more general forum where you'll get a better answer.

Mike

Tuesday, March 20, 2012

Changing a BIT to an INT where there's a CONSTRAINT and a DEFAULT

A few weeks ago a client asked me to add a column to a table so I
created this script:

ALTER TABLE dbo.tblIndividual ADD fldRenewalStatus BIT NOT NULL
CONSTRAINT fldRenewalStatus_Default DEFAULT 0

Now they want to change it from a BIT to an INT, to store an enum.
Fair enough. However, no matter how much I wrangle with a script, I
can't find a reliable way to alter the column. I've mixed and matched
the following and nothing seems to work:

EXEC sp_unbindefault 'tblIndividual.fldRenewalStatus'

DROP DEFAULT DF_tblIndividual_fldRenewalStatus

ALTER TABLE tblIndividual
DROP CONSTRAINT fldRenewalStatus_Default

ALTER TABLE tblIndividual
DROP COLUMN fldRenewalStatus
GO

ALTER TABLE tblIndividual
ADD fldRenewalStatus int NOT NULL
CONSTRAINT fldRenewalStatus_Default DEFAULT 0

Thoughts?

Thanks

EdwardALTER the table to add a new INT column. Set the value of that column
based on the data in the BIT column. Alter the table to drop the BIT
column. Rename the INT column.

Roy Harvey
Beacon Falls, CT

On 2 Jan 2007 09:19:00 -0800, teddysnips@.hotmail.com wrote:

Quote:

Originally Posted by

>A few weeks ago a client asked me to add a column to a table so I
>created this script:
>
>ALTER TABLE dbo.tblIndividual ADD fldRenewalStatus BIT NOT NULL
>CONSTRAINT fldRenewalStatus_Default DEFAULT 0
>
>Now they want to change it from a BIT to an INT, to store an enum.
>Fair enough. However, no matter how much I wrangle with a script, I
>can't find a reliable way to alter the column. I've mixed and matched
>the following and nothing seems to work:
>
>EXEC sp_unbindefault 'tblIndividual.fldRenewalStatus'
>
>DROP DEFAULT DF_tblIndividual_fldRenewalStatus
>
>ALTER TABLE tblIndividual
>DROP CONSTRAINT fldRenewalStatus_Default
>
>ALTER TABLE tblIndividual
>DROP COLUMN fldRenewalStatus
>GO
>
>ALTER TABLE tblIndividual
>ADD fldRenewalStatus int NOT NULL
>CONSTRAINT fldRenewalStatus_Default DEFAULT 0
>
>
>Thoughts?
>
>Thanks
>
>Edward

|||On 2 Jan 2007 09:19:00 -0800, teddysnips@.hotmail.com wrote:

Quote:

Originally Posted by

>A few weeks ago a client asked me to add a column to a table so I
>created this script:
>
>ALTER TABLE dbo.tblIndividual ADD fldRenewalStatus BIT NOT NULL
>CONSTRAINT fldRenewalStatus_Default DEFAULT 0
>
>Now they want to change it from a BIT to an INT, to store an enum.
>Fair enough. However, no matter how much I wrangle with a script, I
>can't find a reliable way to alter the column. I've mixed and matched
>the following and nothing seems to work:


Hi Edward,

You can do as Roy suggests, or you can run the following script:

ALTER TABLE dbo.tblIndividual
DROP CONSTRAINT fldRenewalStatus_Default;

ALTER TABLE dbo.tblIndividual
ALTER COLUMN fldRenewalStatus INT NOT NULL;

ALTER TABLE dbo.tblIndividual
ADD CONSTRAINT fldRenewalStatus_Default DEFAULT 0 FOR fldRenewalStatus;

Running the ALTER COLUMN might take long if you have lots of data!

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||On Wed, 03 Jan 2007 00:06:18 +0100, Hugo Kornelis
<hugo@.perFact.REMOVETHIS.info.INVALIDwrote:

Quote:

Originally Posted by

>You can do as Roy suggests, or you can run the following script:


Much simpler, of course. Somehow I had the idea that bit would not
convert to int. Thanks for the diplomatic correction. 8-)

Roy|||Hugo Kornelis wrote:

Quote:

Originally Posted by

On 2 Jan 2007 09:19:00 -0800, teddysnips@.hotmail.com wrote:
>

Quote:

Originally Posted by

A few weeks ago a client asked me to add a column to a table so I
created this script:

ALTER TABLE dbo.tblIndividual ADD fldRenewalStatus BIT NOT NULL
CONSTRAINT fldRenewalStatus_Default DEFAULT 0

Now they want to change it from a BIT to an INT, to store an enum.
Fair enough. However, no matter how much I wrangle with a script, I
can't find a reliable way to alter the column. I've mixed and matched
the following and nothing seems to work:


>
Hi Edward,
>
You can do as Roy suggests, or you can run the following script:
>
ALTER TABLE dbo.tblIndividual
DROP CONSTRAINT fldRenewalStatus_Default;
>
ALTER TABLE dbo.tblIndividual
ALTER COLUMN fldRenewalStatus INT NOT NULL;
>
ALTER TABLE dbo.tblIndividual
ADD CONSTRAINT fldRenewalStatus_Default DEFAULT 0 FOR fldRenewalStatus;
>
Running the ALTER COLUMN might take long if you have lots of data!


Thanks Hugo - that worked a treat!

Edward

Wednesday, March 7, 2012

Change the default constraint

I hav a table with a column wich has a default constraint. I want to
remove this constraint and to add an other one whitout to delete the column.
It is posible?
Thanks for help!Sure...
ALTER TABLE YourTable
DROP CONSTRAINT DefaultConstraintName
Then...
ALTER TABLE YourTable
ADD CONSTRAINT DefaultConstraintName DEFAULT (default_value) FOR your_column
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"Mihaly" <Mihaly@.discussions.microsoft.com> wrote in message
news:2724888C-8E36-4D03-93F0-06ADCEEC711C@.microsoft.com...
> I hav a table with a column wich has a default constraint. I want to
> remove this constraint and to add an other one whitout to delete the
column.
> It is posible?
> Thanks for help!
>|||Mihaly,
Use alter table to drop the constraint and add the new one.
Example:
use northwind
go
create table t (
colA varchar(25) default ('sql server 2000')
)
go
insert into t default values
go
select * from t
go
declare @.sql nvarchar(4000)
declare @.cnstname sysname
select
@.cnstname = [name]
from
sysobjects as so
where
xtype = 'D'
and parent_obj = object_id('dbo.t')
and col_name(parent_obj, info) = 'colA'
if @.cnstname is not null
begin
set @.sql = N'alter table dbo.t drop constraint ' + @.cnstname
execute sp_executesql @.sql
end
go
alter table t
add constraint df_sql200 default ('sql server 200') for colA
go
insert into t default values
go
select * from t
go
alter table dbo.t drop constraint df_sql200
go
alter table t
add constraint df_sql2005 default ('sql server 2005') for colA
go
insert into t default values
go
select * from t
go
drop table t
go
AMB
"Mihaly" wrote:

> I hav a table with a column wich has a default constraint. I want to
> remove this constraint and to add an other one whitout to delete the colum
n.
> It is posible?
> Thanks for help!
>|||No. Not for you. And whose default is that (ba boom ching!)
Yes, it is possible:
create table test
(
test int,
defaulted int constraint default_test_defaulted default (10)
)
go
insert into test (test) values (1)
go
select * from test
go
alter table test drop constraint default_test_defaulted
--note, I didn't specify a constraint name
alter table test add default 20 for defaulted
--best practice would have included a name, but I wanted you to see what
would happen
--this is the right way:
--alter table test add constraint default_test_defaulted default 20 for
defaulted
--To find the name if you didn't give it a nice name
select * from sysobjects where xtype = 'D' and parent_obj =
object_id('test')
alter table test drop constraint DF__test__defaulted__79A81403
----
Louis Davidson - drsql@.hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Blog - http://spaces.msn.com/members/drsql/
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)
"Mihaly" <Mihaly@.discussions.microsoft.com> wrote in message
news:2724888C-8E36-4D03-93F0-06ADCEEC711C@.microsoft.com...
> I hav a table with a column wich has a default constraint. I want to
> remove this constraint and to add an other one whitout to delete the
> column.
> It is posible?
> Thanks for help!
>

Change the contraint on a published table

Hi,
Is it possible to remove/change a constraint on a table that has been
published, without reinitilising the subscriber?
Setup:
Merge Replication
Subscribers initiate a pull subscription
Kind Regards
Warren Patterson
Warren,
try using sp_addscriptexec.
HTH,
Paul Ibison
|||Hi Paul,
Thanks for your reply.
Unfortunately sp_addscriptexec is buggy and does not work via FTP. It would
solve my problem if it worked in my scenario, but unfortunately, the guys at
Microsoft told me that the reason sp_addscriptexec does not work via FTP is
due to the fact it tries to resolve the FTP address as a UNC path or
something like that.
Do a search in this news group for "Failed to apply script using 'osql'
utility (sp_addscriptexec)" I created a whole thread dedicated to this
issue.
Any other ideas?
Kind Regards
Warren Patterson
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:eTa0DprXEHA.3716@.TK2MSFTNGP11.phx.gbl...
> Warren,
> try using sp_addscriptexec.
> HTH,
> Paul Ibison
>
|||Warren,
thanks for the info - found your thread on
http://www.mcse.ms/archive95-2004-5-641761.html.
If sp_addscriptexec doesn't work for your case, the only way I can see of
solving this issue is manually - linked servers, EM, QA etc
Regards,
Paul Ibison
|||Thanks Paul,
I was afraid that that was the only way.
Well, guess I need to make the 5000km+ round trip :-)
Kind Regards
Warren Patterson
"Paul Ibison" <Paul.Ibison@.Pygmalion.Com> wrote in message
news:%23UgfEE0XEHA.2940@.TK2MSFTNGP09.phx.gbl...
> Warren,
> thanks for the info - found your thread on
> http://www.mcse.ms/archive95-2004-5-641761.html.
> If sp_addscriptexec doesn't work for your case, the only way I can see of
> solving this issue is manually - linked servers, EM, QA etc
> Regards,
> Paul Ibison
>