1 if exists(select * from syscolumns where id=OBJECT_ID('Pub_VideoTag') and name='Title') 2 begin 3 4 insert into pub_tag(guid,name,createtime,tagtype,IsRecommend,version,sortnumber) 5 select REPLACE(NEWID(),'-',''), a.Title,GETDATE(),1,0,1,1 from pub_videotag as a inner join Pub_Video as b on a.VideoGuid = b.Guid 6 and b.IsWeiKe = 0 7 where not exists(select Guid from pub_tag where Name = a.Title and tagtype =1) 8 group by a.Title 9 10 insert into pub_tag(guid,name,createtime,tagtype,IsRecommend,version,sortnumber) 11 select REPLACE(NEWID(),'-',''), a.Title,GETDATE(),0,0,1,1 from pub_videotag as a inner join Pub_Video as b on a.VideoGuid = b.Guid 12 and b.IsWeiKe = 1 13 where not exists(select Guid from pub_tag where Name = a.Title and tagtype =0) 14 group by a.Title 15 16 update Pub_VideoTag set Title = (select guid from pub_tag as a where a.name = Pub_VideoTag.Title and 17 a.tagtype = (select ISNULL((CASE WHEN isweike = 1 THEN 0 ELSE 1 END),1) from Pub_Video as a inner join Pub_VideoTag b on a.Guid = b.VideoGuid where b.Guid = Pub_VideoTag.Guid) 18 ) 19 20 EXEC sp_rename '[dbo].[pub_videotag].[Title]', 'TagGuid', 'COLUMN' 21 22 END
代码如图。
我的逻辑是这样的,因为那个pub_videotag表改成了中间表。
所以把之前videotag里面的数据转移到了tag表。
然后再进行修改字段的操作,第一次是没有问题的。
但是第二次执行,会提示列名 'Title' 无效。
按理来说第二次应该不会进那个if语句了才对。还是我这种写法有问题?求大神回答。
字段是否存在?要调用系统表?sysobjects?是sqlserver么?
嗯,是sqlserver。
EXEC sp_rename '[dbo].[pub_videotag].[Title]', 'TagGuid', 'COLUMN'
select * from syscolumns where id=OBJECT_ID('Pub_VideoTag') and name='Title'
看看这样能查到不
简单一点的逻辑如下:
IF EXISTS (select * from syscolumns where id=object_id('t_user') and name='id') BEGIN PRINT '存在' END ELSE BEGIN PRINT '不存在' END
你的if判断貌似也没有错误啊。