为什么刚添加一列,就updata就报错?

ahui_net 2007-01-19 12:08:12
alter table _Crm_Company Add [CustomCode] nvarchar(50) not null default ''
update _Crm_Company set CustomCode = EnglishName

会报列不存在

后来改成这样也是


alter table _Crm_Company Add [CustomCode] nvarchar(50) not null default ''

while( 1 = 1 )
begin

WAITFOR DELAY '00:00:15'

if exists( select [name] from syscolumns where id=object_id('_Crm_Company') and [name] = 'CustomCode' )
begin
update _Crm_Company set CustomCode = EnglishName
break
end
end
...全文
464 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahui_net 2007-02-15
  • 打赏
  • 举报
回复
高手呢,还没有人回复?!
ahui_net 2007-02-15
  • 打赏
  • 举报
回复
继续顶
yjlhch 2007-01-20
  • 打赏
  • 举报
回复
关注
ahui_net 2007-01-20
  • 打赏
  • 举报
回复
再顶,顶顶顶
ahui_net 2007-01-20
  • 打赏
  • 举报
回复
marco08 2007-01-19
  • 打赏
  • 举报
回复
EnglishName列是否存在為NULL的數據?
hyc_music1981 2007-01-19
  • 打赏
  • 举报
回复
确认EnglishName列存在?
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
直接写是没问题的,但是我是这样的就不行,加go也不行
if not exists( select [name] from syscolumns where id=object_id('_Crm_Company') and [name] = 'CustomCode' )
begin
alter table _Crm_Company Add [CustomCode] nvarchar(50) not null default ''
update _Crm_Company set CustomCode = EnglishName
end
baoshan 2007-01-19
  • 打赏
  • 举报
回复
--try
alter table _Crm_Company Add [CustomCode] nvarchar(50) not null default ''
go
update _Crm_Company set CustomCode = EnglishName
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
因为这个sql会重复运行,不能每次都updata
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
只有再 if not exists( select [name] from syscolumns where id=object_id('Authors') and [name] = 'CustomCode' ) 条件成立才运行
update [dbo].[Authors] set [CustomCode] = [Name]
zorou_fatal 2007-01-19
  • 打赏
  • 举报
回复
如果你要判断的话
USE [Duwamish7]
GO
/****** 对象: Table [dbo].[Authors] 脚本日期: 01/19/2007 13:32:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

if not exists( select [name] from syscolumns where id=object_id('Authors') and [name] = 'CustomCode' )
begin
alter table [dbo].[Authors] Add [CustomCode] nvarchar(50) not null default ''
end
GO
update [dbo].[Authors] set [CustomCode] = [Name]
zorou_fatal 2007-01-19
  • 打赏
  • 举报
回复
USE [Duwamish7]
GO
/****** 对象: Table [dbo].[Authors] 脚本日期: 01/19/2007 13:32:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter table [dbo].[Authors] Add [CustomCode] nvarchar(50) not null default ''
GO
update [dbo].[Authors] set [CustomCode] = [Name]

这样可以。
我是SQL SERVER 2005 我用的DUWAMISH7的库测试的
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
baoshan(石头) 的加go会报语法错
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
47522341(睡到8:30) 我在sql 2000 也测试过了,你的代码运行不能通过
ahui_net 2007-01-19
  • 打赏
  • 举报
回复
没有 null数据
47522341(睡到8:30) 我这里执行不对啊
------------------------
忘了说明我的是sql 2005了
baoshan 2007-01-19
  • 打赏
  • 举报
回复
--try
if not exists( select [name] from syscolumns where id=object_id('_Crm_Company') and [name] = 'CustomCode' )
alter table _Crm_Company Add [CustomCode] nvarchar(50) not null default ''
go
update _Crm_Company set CustomCode = EnglishName

da21 2007-01-19
  • 打赏
  • 举报
回复
你的表有数据吗?如果该表有数据,那么加一个not null 列是不行的。
47522341 2007-01-19
  • 打赏
  • 举报
回复
楼上所述代码可以执行通过;没有问题;

if object_id(N'crm_company') is not null
drop table crm_company
go

create table crm_company(f1 int)
go
insert into crm_company values(1)
go

if not exists( select [name] from syscolumns where id=object_id('Crm_Company') and [name] = 'CustomCode' )
begin
alter table Crm_Company Add [CustomCode] nvarchar(50) not null default ''

update Crm_Company set CustomCode = 'df'
end

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧