去除sql中的查詢欄位

2020-08-13 16:42:30

pgsql中的使用

SELECT
'SELECT ' || array_to_string(ARRAY(
SELECT 'o' || '.' || c.column_name
       FROM information_schema.columns As c
          WHERE table_name = '表名'  AND  c.column_name NOT IN('排除欄位', '排除欄位')), ',') || ' FROM 表名 As o' As sqlstmt

eg:
SELECT
'SELECT ' || array_to_string(ARRAY(
SELECT 'o' || '.' || c.column_name
       FROM information_schema.columns As c
          WHERE table_name = 'prm_prom_bill'  AND  c.column_name NOT IN('id', 'create_user')), ',') || ' FROM prm_prom_bill As o' As sqlstmt

 

 

 查詢出來的內容爲 輸出的sql 已經去除掉除外的欄位,例子中輸入的是 id和create_user欄位

下面 下麪的這個應該是mysql的,我沒有測試,有需求的可以自測

declare @field nvarchar(1000)
select @field=isnull(@field+',','')+quotename(Name) from syscolumns where ID=object_id('表名') and Name not in('排除1','排除2') 
exec('select '+@field+' from 表名')