sql 查询排除一个字段的其他字段
1、创建测试表,此处以创建6个字段为例,实际100个字段的话,操作类似,
create table test_sel_cols(id number, password varchar2(200), remark1 varchar2(100), remark2 varchar2(100), remark3 varchar2(100), remark4 varchar2(100));
2、通过系统视图,查看表结构;可以看到新建表的所有字段;
select * from user_tab_cols t where table_name = 'TEST_SEL_COLS';
3、编写sql,从视图中获取去掉password的所有字段;
select 'select '||listagg(column_name, ',') within group(order by column_id) ||' from TEST_SEL_COLS' str from user_tab_cols t where table_name = 'TEST_SEL_COLS' and column_name <> 'PASSWORD';
4、将上步查询结果复制出,即可生成所需sql如下:
select ID, REMARK1, REMARK2, REMARK3, REMARK4 from TEST_SEL_COLS
可以发现少了password字段;
但是可以使用动态语句来 偷下懒
DECLARE @cols AS VARCHAR(4000)
SELECT @cols = isnull(@cols,'') + NAME +',' FROM syscolumns s
WHERE 1=1 AND id = (SELECT id FROM sysobjects WHERE NAME ='yourtable') --and name <> '这里填写你徐压迫排除的字段'
SET @cols = LEFT(@cols,LEN(@cols)-1 )
EXEC ('select ' + @cols + ' from yourtable')
--备注:这个是tsql代码,只针对mssql
2013-11-07
1.select * from [tablename] --查询所有列
2.select a, b, c from [tablename] --查询指定的列
不能直接查询表的信息吗?
select
* from user_col_comments
where table_name = upper( '某表名 ');