try:
if exists(select a.name from syscolumns a,sysobjects b where a.name='I_id' and b.name='PM_ItemQC' and a.id=b.id and b.xtype='U')
begin
Update PM_ItemQC from PM_ItemQC a set iEleVid = (select Max(I_id) from PM_ItemQC b where a.iEleVid=b.iEleVid)
end但是我觉得你的是同一张表,应该你的方法也可以啊
if exists(select a.name from syscolumns a,sysobjects b where a.name='I_id' and b.name='PM_ItemQC' and a.id=b.id and b.xtype='U')
begin
Update PM_ItemQC from PM_ItemQC a set iEleVid = (select Max(I_id) from PM_ItemQC b where a.iEleVid=b.iEleVid)
end但是我觉得你的是同一张表,应该你的方法也可以啊
Update PM_ItemQC set iEleVid = I_id
和
if exists(select a.name from syscolumns a,sysobjects b where a.name='I_id' and b.name='PM_ItemQC' and a.id=b.id and b.xtype='U')
Update PM_ItemQC set iEleVid = I_id
没有问题的,
单独执行:
select a.name from syscolumns a,sysobjects b where a.name='I_id' and b.name='PM_ItemQC' and a.id=b.id and b.xtype='U'
的结果?
Update PM_ItemQC set iEleVid = I_id
看看结果。
print 'ddd'当存在字段I_id的时候,打印出ddd,但不存在的时候,不打印。这说明语句是对的。
只是不知道为什么不存在字段I_id的时候,提示列名 'I_id' 无效。他不应该执行的啊。
exec('Update PM_ItemQC set iEleVid = I_id')因为sqlserver在预编译的时候会检查列的完整性,所以要用exec来屏蔽