T-SQL 不提供这个功能!

变通的办法有:

  1. 取得该表的建表语句,然后删除不需要的字段;
  2. 把除这个表中的其他需要的字段一个一个写出来;
  3. 把这个字段删除:alter table 表名 drop column 字段名;
  4. 如果是MySql,create table a like b会创建索引;
  5. 新建一个同样的表b,然后删除不要的字段:
    create table b as select * from a;
    alter table b drop column c;
    select * from b;
  6. 利用concat函数拼出除了某字段之外的所有字段:
    select
    concat('select',group_concat(column_name),'from',table_name,';')
    from information_schema.columns
    where table_name='xxxx'
    and table_schema='ccms_xxx' and column_name!='id'
  7. 动态拼接sql,把不想查的列去掉后动态执行sql:
    declare @name varchar(100)
    declare @sql varchar(1000)
    
    SET @sql='select '
    DECLARE C11 CURSOR FOR
    
    select name from syscolumns where id=
    (select max(id) from sysobjects where xtype='u' and name='表名')
    and name <> '不要的字段'
    OPEN C11
    
    FETCH NEXT FROM C11 INTO 
                @name
    IF @@FETCH_STATUS <> 0 
    BEGIN
         DEALLOCATE C11
         RETURN
    END 
    WHILE(@@FETCH_STATUS = 0)
    BEGIN
         SET @sql=@sql+@name+','
    
         FETCH NEXT FROM C11 INTO 
         @name
    END
    DEALLOCATE C11
    
    set @sql=substring(@sql,1,len(@sql)-1)+' from 表名'
    
    exec (@sql)
    View Code

     

posted on 2021-02-19 12:23  HandsomeFa  阅读(6731)  评论(0编辑  收藏  举报