SQL to return first two columns of a table

前端 未结 8 583
猫巷女王i
猫巷女王i 2020-12-10 05:04

Is there any SQL lingo to return JUST the first two columns of a table WITHOUT knowing the field names?

S

相关标签:
8条回答
  • 2020-12-10 06:00

    There it´s

    declare @select varchar(max)
    set @select = 'select '
    
    select @select=@select+COLUMN_NAME+','
    from information_schema.columns
    where table_name = 'TABLE' and ordinal_position <= 2
    
    set @select=LEFT(@select,LEN(@select)-1)+' from TABLE'
    exec(@select)
    
    0 讨论(0)
  • 2020-12-10 06:00

    Or do I have to go the long way around and find out the column names first? How would I do that?

    It's pretty easy to do manually.
    Just run this first

    select * from tbl where 1=0
    

    This statement works on all major DBMS without needing any system catalogs. That gives you all the column names, then all you need to do is type the first two

    select colname1, colnum2 from tbl
    
    0 讨论(0)
提交回复
热议问题