Query data for all columns except a certain column

Query data for all columns except a certain column:
declare @name varchar(64)
declare @sql varchar(1024)

SET @sql='select ' //Define the select statement to be executed, the logic behind will be pieced together into a query statement for all fields except the specified field
DECLARE C11 CURSOR FOR //Make a cursor and read the names of columns one by one

//The following select statement is to query the names of all columns except the specified column
select name from syscolumns where id=
(select max(id) from sysobjects where xtype='u' and name='表名')
and name <> 'except fields'
OPEN C11

FETCH NEXT FROM C11 INTO //Read out the name of a column each time		
            @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)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326892258&siteId=291194637