如何用SQL语句去除数据全为0的列?

bqj 2003-07-05 09:15:56
一个表中有30个字段,其中有的字段可能数值全为0,如果用SQL语句生成一个新的表,使数据全部为0的列不出现在新表中?
...全文
573 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bqj 2003-07-05
  • 打赏
  • 举报
回复
Help me!
zjcxc 元老 2003-07-05
  • 打赏
  • 举报
回复
declare @sql varchar(1000),@fd varchar(1000)
declare #temp cursor for select name from syscolumns where id=object_id('你要复制的表')

select * into 新表 from 你要复制的表
open #temp
fetch next from #temp into @fd
while @@fetch_status=0
begin
set @sql='if (select sum('+@fd+') from 新表)=0'
+char(13)+'alter table 新表 drop '+@fd
exec(@sql)
fetch next from #temp into @fd
end
close #temp
deallocate #temp
rdsdh 2003-07-05
  • 打赏
  • 举报
回复
Select ... Into TabOut From TabSrc

if not exists(select * from TabOut where TheField <>0)
alter TabOut Drop Field TheField

......
bqj 2003-07-05
  • 打赏
  • 举报
回复
我指的是,如果原表中有多条记录,但如果这些记录中的某几列数值全为0(比如col1、col7),则在新表中不要这些字段(比如去除上例中的col1、col7字段)。
bqj 2003-07-05
  • 打赏
  • 举报
回复
楼上大侠,可能是我写的不清楚,
nboys 2003-07-05
  • 打赏
  • 举报
回复
select * into othertable from tablename where not exists (select * from tablename where col1+col2+...+col30<>0)
hjb111 2003-07-05
  • 打赏
  • 举报
回复
select * into newtable from(slect * from yourtable where col1+col2+...+col30<>0)
newyj 2003-07-05
  • 打赏
  • 举报
回复
如果用客户端软件 vb.delphi就很容易。
1 取得所有字段名 。系统表理查或 通过field对象获得
count(*)求得纪录数
对所有字段循环
select count(fieldN) from table where fieldn=0
剔除所有 count(fieldN)=count(*)
select field1...
into 新表

34,590

社区成员

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

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