SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Default Constraint Error


Default Constraint Error

Author
Message
ColdCoffee
ColdCoffee
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5815 Visits: 5553
Hi all,

Please run the following code, u ll get 2 tables created;

one a real table and the other a table due to "select top 0 *"...

if object_id('Table1') is not null
drop table dbo.Table1
Create table dbo.Table1
(
c1 int
,c2 varchar(150)
,c3 varchar(150)
,c4 nvarchar(150)
)

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

exec ('select top 0 * into dbo.table2 from dbo.Table1 ')
exec ('alter table dbo.table2 add constraint default_c4 default ''N/A'' for c4')



When i run the above in query window, i get no error; even i can insert data into table2 and the default constraint works pretty well.

but when i run the same code through SQL Server Agent, i get the following error:
Column 'c4' in table 'table2' is invalid for creating a default constraint. [SQLSTATE 42000]

Have u guys ever faced this issue?? if yes, please help me out in resolving this and explaning me why it happened..

Thanks in Advance!
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71110 Visits: 32909
Maybe I missed a step or something, but I just tested your script against a test database using SQL Agent and it ran without errors.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
ColdCoffee
ColdCoffee
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5815 Visits: 5553
@Grant, even i faced similar result as results... when i run the code as a step in sql server agent, i am not getting the error **AT TIMES**... but when i put that inside a SP and run that, i get that error.. make me totally flummoxed..i dont know if my code is wrong or if my Agent is wrong.. have u heard of issues like that before Grant??

Oh, btw i use SQL Server 2005 SP2 Enterprise Edition
ColdCoffee
ColdCoffee
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5815 Visits: 5553
Guyz any updates on this?? do u guys needs more info on the error, i can give..
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)SSC Guru (71K reputation)

Group: General Forum Members
Points: 71110 Visits: 32909
No, I'm still not sure what's up.

One difference is I ran it on 2008. But there's little reason it should be different.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
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