Changing A BIT To An INT Where There's A CONSTRAINT And A DEFAULT
Jan 2, 2007
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,I see the following in Books Online: CONSTRAINT--Is an optional keywordindicating the beginning of a PRIMARY KEY, NOT NULL, UNIQUE, FOREIGNKEY, or CHECK constraint definition...But I have a table column defined as follows:[MONTH] [decimal] (2, 0) NOT NULL CONSTRAINT[DF__TBLNAME__MONTH__216361A7] DEFAULT (0)My question: Is "DEFAULT" a constraint, or is it called something else?Thanks,Eric
I have a table set up called tbl_DailySummary_new. In that table I have a column called €˜RevType€™ char(2) NOT NULL Default €˜00€™. I'm trying to populate this table from another table called tbl_DailySummary. The RevType column in tbl_DailySummary allows null values (and has them). When I attempt to insert all records from tbl_dailysummary into tbl_DailySummary_new, I get the following error message:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'RevType', table 'TxnRptg.dbo.tbl_DailySummary_new'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Why dosen€™t the default constraint on the RevType column in the new table pick this up?
I'm a software developer trying to hack my way thru a SQL script to increase the size of a column. The column has a constraint on it that won't allow the alter unless its dropped.
Alter command: ALTER TABLE dbo.QueueBack ALTER Column QBQueue varchar(4) NOT NULL
Message response: Msg 5074, Level 16, State 1, Line 10 The object 'DF__QueueBack__QBQue__76818E95' is dependent on column 'QBQueue'. Msg 4922, Level 16, State 9, Line 10 ALTER TABLE ALTER COLUMN QBQueue failed because one or more objects access this column.
So, my script drops the default constraint, alters the column size and then adds the constraint back. The problem is that the constraint does NOT appear to be the same as it was prior. I can run the Alter command on the column and it will allow me to change the size of the column... where it prohibited me from doing such before. How do I restore the default constraint so it is exactly the way it was before I dropped it?
Thx,
Stretch
---------------------------------
Script for drop, alter, add... modified slightly for simplicity...
declare @Error int
BEGIN TRAN SELECT @Error = 0
DECLARE @defname VARCHAR(100), @cmd VARCHAR(1000)
-- this is retrieved at run time from the sysconstraints table; hard-coded here... set @defname = 'DF__QueueBack__QBCategory'
IF @defname <> '' BEGIN SET @cmd = 'ALTER TABLE dbo.QueueBack DROP CONSTRAINT '+ @defname EXEC(@cmd)
if @@ERROR = -1 BEGIN SET @Error = -1 END END
if @Error = 0 BEGIN -- modify the column ALTER TABLE dbo.QueueBack ALTER Column QBCategory varchar(7) NOT NULL
if @@ERROR = -1 BEGIN SET @Error = -1 END END
if @Error = 0 BEGIN -- Add the default constraint back SET @cmd = 'ALTER TABLE dbo.QueueBack ADD CONSTRAINT '+ @defname + ' DEFAULT ('''') FOR QBCategory' EXEC(@cmd)
if @@ERROR = -1 BEGIN SET @Error = -1 END END
if @Error = 0 BEGIN -- Commit if no error... COMMIT TRAN END ELSE BEGIN -- Rollback if error... ROLLBACK TRAN END
Okay, maybe I'm getting ahead of myself. Using SQL Server Express, VWD and .net 2.0 I've figured out how to drop a Table Column Constraint or Default Value/Binding and then Create it again using a stored procedure. What I can't figure out is how to retrieve that column's constraint value and write it to, say a label, in an aspx page, simply for reference. Is it possible? In this case the Data Type of the column is money. I'm using it to perform a calculation to a column with a value that the user inserts into another column. (Column1(user input) minus Column2(with Default Value) = Column3(Difference). I just want to read Column2's Default Value for reference so I know whether to change it or not.
I can't seem to get the syntax correct for ALTERing an existing column with a default constraint. I've been to Help and BOL. There are examples that show how to use the ALTER command to add a column with a default constraint but not how to do it on an existing column.
I have this problem. I want to add a new primary key to a table but i want the name of the contstraint to be generated by the system. I have this TSQL code.
ALTER TABLE TableTest ADD CONSTRAINT PRIMARY KEY (C1)
Reading the BOL, it says that if you don't supply a name for the constraint it generates one. But I get this error "Incorrect syntax near the keyword 'PRIMARY'".
If I add a name to the constraint, it works fine. I'm using SQL Server 2000
I have a default constratint on DateColumn getdate()-1
I have used enterprise manager to update it to yesterday's date everyday.
I would like to have a SQL which can check for the date in the system or even a trigger which checks when the date changes the constraint is updated itself. If this is not possible I would like to have a stored procedure which I will schedule to run as a job everyday once.
So if today 6/12/2006, the default value in the Datecolumn should be 6/11/2006.
This gives me a error, i tried but could not fix the bug.
Alter Table TABLE_NAME Alter Constraint DF_DATECOLUMN Default getdate()-1
I cannot figure out how to add a default constraint to an existing column. The syntax I'm using is :
ALTER TABLE table_name WITH NOCHECK ADD CONSTRAINT column_name DEFAULT (0)
This gives me a syntax error.
The column was originally added with a default constraint of 1 to a 2.6 million row table. I dropped the existing constraint and need to add the new default constraint of 0 for that column.
What are the tables are having the Default Constraint those table's Script the User can't generate but remaining tables He/She can generate,If i want DENY he/she to do not generate the script those are not having the DF Constraints what should i do.....
 I have a table named [New Item] and created a default constraint on a column, and i wanted to change the data type of the column using the query
alter table [new item] alter column pcs_qty decimal(15,2) not null
but the name of the default constraint is 'DF__New Item__Pcs_Qt__2D12A970' and i am not able to delete the constraint because it contains a space in between.Is there any work around for this.
I tried to delete the constraint by using the query
alter table [new item] drop constraint 'DF__New Item__Pcs_Qt__2D12A970'
but I am getting the exception,
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'DF__New Item__Pcs_Qt__2D12A970'.
Are there any vices to using default constraints on all columns in your table. For example an Int that defaults to 0 or a char or varchar that defaults to ''
I know that 0 and Null are not the same thing. But if your programs don't have the concept of NULL then you have to convert the NULL to zero.
So, DEFAULT CONSTRAINTS on every column. Is it good or Bad?
Hi, can anyone help me please - I'm still struggling with my database. My set up is: Microsoft Visual Web Developer 2005, SQL Express 2005, GoDaddy windows shared hosting. On my PC I have a site which uses membership and the default database which was created after first compilation. This works fine locally. However, I need to export this to the GoDaddy server. To do this I must rename the database (or recreate the project with a newly named database) and figure out how to alter any other settings. I tried running aspnet_regsql.exe command but I really didn't have any luck. I would have thought this would be a straight forward operation. Can anyone give me guidance please. Garrett
Can someone please help , we are trying to change the default language from us_english to british englsish as the date formats have to be in dd/mm/yyy for our european clients.
We tried sp_configure 'default language' 23 Reconfigure . It worked 3 months back.
We are trying this on the reverse , now and it doesnt work.
I thought I had this solved but after running aspnet_regiis again I am having the same problem.
I can't log onto my sql server because the default DB is master. The following command using sqlcmd gives me the master db:
Code:
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' GO
So, to try to fix this i ran:
Code:
EXEC sp_defaultdb 'sa', 'my_default_db' GO
and no luck. I went over to the DB server and checked the permissions under SERVER/SQLEXPRESS > SECURITY > LOGINS. All except NTAuthority and Administrators have my intended DB as the default (including ASPNET).
This is frustrating because I know I have figured it out before but I can't remember what I did. Help appreciated.
Does anybody know how I can change the default value for a column?I was trying to remove the default value in order to add the new oneafterwards. This is what I tried:alter table /table-name/ drop default for /column-name/alter table /table-name/ alter column /column-name/(/new-decl-without-default/)It did not work. I cannot find a solution in the documentation. Maybeyou can help me out?Thank you,Johan
How can i change the default value of a column? I already have a columnnamed DateOfRental but I want to alter it so that it has default valuegetdate()ThanksDavid--http://www.nintendo-europe.com/NOE/...=l&a=Prodigious
I've created a new Stored Procedure template and saved it to .Microsoft SQL Server90ToolsShellTemplatesSqlStored Procedure. I can view (and use) the template via the Template Explorer, however, I want this new template to become my default template.
What do I mean by this? - I want to be able to right-click on stored procedures in my object explorer, click New Stored Procedure, and be forced to use my new template, rather than Create Procedure (New Menu).SQL.
Our security center just scanned my database server and has asked that I change the database server from the default instance name and change the default port used. When installed MS SQL Server 2000 I used the default installation settings. I have installed SP4 and this is all running on a fully patched WIN 2003 Server machine. Is this possible without un-installing and then re-installing?
Best practice talks about the use of name conventions in the SSIS packages.
Is there a way to rename the default names of the tasks in the DataFlow and ControlFlow once and for all, so that each time I drag a new DataFlowTask in, the default name will be DFT and so on.
I have several default constraints defined on a table. When I use theObject Browser and expand the constraints for this table andright-click and then select "Script Object to New Window As Create", acreate constraint statement for a different default constraint isdisplayed than the one I just right-clicked on. For example, I clickon constraint "DF_C" and it shows me "DF_B".The last time I encountered this, the solution was to dump contents ofthe table into another, drop, recreate it, and restore the contents.That's not a good option this time.Is there another way to fised this or at least navigate the catalog tofind out what is "off" about this?Thanks
Hi;I feel like an idiot to be asking this,but how do I change the default drive and folderin which new databases are created?I've been looking all over Server Mgmt Studio,and I can't find anything. I'm thinking that someonecan tell me this off the top of their heads,or slip me a link to a web page.Thanks,
Hi, I'm supporting a SQL 6.5 application through a firewall & I'm new to NT & SQLServer. What's the easiest way to find out what port SQL is listening on? It should be 1433 but the NT guys say 1433 isn't in use. What's the easiest way to change the default port? A search through Books online talks about editing registry keys & I'd prefer not to do this if this can be avoided. Any tips would be appreciated.
If you enter "Create Database test", the database files (mdf file & log file) are created, by default, in:- C:Program FilesMicrosoft SQL ServerMSSQLData I want to change that to:- D:Database Files I sucessfully moved the model database to this location (using the instructions in BOL) assuming that all new databases would now get created in the same location, but they don't. They still get created in: C:Program FilesMicrosoft SQL ServerMSSQLData
So how do I change the default? (It's not satisfactory to have to move each database after it's created)
I am creating SSRS reports on top of SSAS cubes. I want the default value of parameter to change dynamically based on the current year or it should select the last of the parameter values.
IF NOT EXISTS (SELECT TOP 1 1 FROM dbo.syscolumns WHERE id = OBJECT_ID(N'dbo.Employee) and name = 'DoNotCall') BEGIN ALTER TABLE [dbo].[Employee] ADD [DoNotCall] bit not null Constraint DoNot_Call_Default DEFAULT 0 IF ( @@ERROR <> 0 ) GOTO QuitWithRollback END
It just takes a LOT of time in SQL Server Management studio. I have to cancel the query and cancelling takes a whole lot time. I am using SQL Server 2008.
I have installed my modell database onto c:mssql7data...
How do I now change the properties of this so that when someone creates a database, the path it will get created to is set to d: I thought that I could just move model, but it seems that this isn't possible.
Hello,I have SQL Server 2000 Enterprise Edition, and I'd like to change thedefault port to something else (we'll use 2033 for this example). I'vetried to connect from another computer on the same subnet using"ServerName:2033" as the server, but it doesn't connect. It will connect ifI change the Server to use the default port and change the server to connectto on the client to "ServerName", but either I"m not referencing the newport number properly or ????Any help you could provide would be great.Thanks In Advance!Rick