多表插入问题

一者仁心 2008-01-04 09:28:18
从多表插入b表
b有些字段这些表没有
没有的字段默认为空 要怎么做
...全文
46 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
一者仁心 2008-01-04
  • 打赏
  • 举报
回复
我想要一条动态的语句 把所有表的内容都可以更新到b表

select
case when exists(select 1 from syscolumns where id=(select id from sysobjects where name='表名') and name='字段名1') then 字段名1 else '' end,
case when exists(select 1 from syscolumns where id=(select id from sysobjects where name='表名') and name='字段名2') then 字段名1 else '' end,
case when exists(select 1 from syscolumns where id=(select id from sysobjects where name='表名') and name='字段名3') then 字段名1 else '' end,
case when exists(select 1 from syscolumns where id=(select id from sysobjects where name='表名') and name='字段名4') then 字段名1 else '' end
from 表名

我的问题是 如果当前表没有这个字段 他就会报错了 有没有什么解决方法
dawugui 2008-01-04
  • 打赏
  • 举报
回复
从多表插入b表
b有些字段这些表没有
没有的字段默认为空 要怎么做

insert into B select col1,col2,col3 = ''/null from (多表的查询) t

col1,col2代表有的字段,col3代表没有的字段.
ping3000 2008-01-04
  • 打赏
  • 举报
回复
本来就是null啊,如果不要null可以设置默认值,或者在插入的select语句中加默认值

34,591

社区成员

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

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