sql server 关于排除指定列的问题

jack15850798154 2012-02-27 04:41:26
如何排除表中 指定某一列,其他列全部都显示出来?注:表中的其他的列名可能不是固定的。
...全文
530 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
jack15850798154 2012-03-01
  • 打赏
  • 举报
回复
问题解决了,谢谢大家!
勿勿 2012-02-28
  • 打赏
  • 举报
回复
老老实实写、
rfq 2012-02-28
  • 打赏
  • 举报
回复
declare @strSql nvarchar(1000)=''
declare @strCols nvarchar(1000)=''
select @strCols=@strCols+','+name from sys.columns where object_id=object_id('章节表') and name<>'条目编号'
set @strSql=N'select ' +stuff(@strCols,1,1,'') +' from 章节表'
exec(@strSql)
jack15850798154 2012-02-27
  • 打赏
  • 举报
回复
谢谢各位老大,还有一个问题可以帮我看看吗?
select * into #table from
(
select b.userid,c.username
from t_as_group a left join t_as_user b on a.asgroupid=b.asgroupid
left join t_user c on c.userid=b.userid
where b.state=1 and a.asgroupname='xxx'
) t

drop table #table

--需设置SQL SERVER 兼容级别 选项兼容级别 (SQL SERVER 90)
select * from
(select * from #table) a
pivot
(max(userid) for username in ([郭xx],[唐xx],[陶xx],[吴xx],[许xx],[叶xx])) b



--需设置SQL SERVER 兼容级别 选项兼容级别 (SQL SERVER 90)
select * from
(select * from #table) a
pivot
(
max(userid) for username in (
(select username from #table)
)) b
为什么这样不可以,有什么方法可以实现动态的吗?谢谢了
dawugui 2012-02-27
  • 打赏
  • 举报
回复
得到表中除Col1、Col2的所有列

例如:userno_fm、userno_to
create table test(
num int identity(1,1),
userno_fm varchar(10),
userno_to varchar(10),
username varchar(10))
select * from test

declare @sql varchar(8000)
select @sql=''
select @sql=@sql+','+[name] from
(select [name] from syscolumns where object_id(N'[test]')=[id] and [name] not in ('userno_fm','userno_to')) A

set @sql='select '+stuff(@sql,1,1,'')+' from [test]'
--print @sql
exec (@sql)

drop table test


select * from syscolumns where id=object_id('tableName') and name not in('a','b')

  • 打赏
  • 举报
回复
declare @col varchar(2000)=''
select @col=@col+','+a.name
from (select name from syscolumns where id=object_id('tbl')) a
where a.name<>'ida'--排出的列,如果是多列,就用where name not in(排除列列表)
print @col
select @col='select '+right(@col,len(@col)-1)+' from tbl'
print @col
exec(@col)
  • 打赏
  • 举报
回复
declare @col varchar(2000)=''
select @col=@col+','+a.name
from (select name from syscolumns where id=object_id('tbl')) a
where a.name<>'ida'
print @col
select @col='select '+right(@col,len(@col)-1)+' from tbl'
print @col
exec(@col)


--这个我已经调试好了,楼主改改就可以使用了
水族杰纶 2012-02-27
  • 打赏
  • 举报
回复
declare @sql varchar(max)
set @sql='select '
select @sql=@sql+name+','
from sys.columns
where object_Id=object_id('表名')
and name!='排除列'
select @sql
PS:其他条件自己拼进去 然后exec动态执行
水族杰纶 2012-02-27
  • 打赏
  • 举报
回复
要么老实写
要么查系统表去拼
没什么快捷的方法
jack15850798154 2012-02-27
  • 打赏
  • 举报
回复
注:需要排除的列名是固定的。

34,590

社区成员

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

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