sql server select column by number

前端 未结 5 2031
别跟我提以往
别跟我提以往 2020-12-19 00:29

Can I select specific columns by the number of the columns in SQL? Something like

SELECT columns(0), columns(3), columns(5), columns(8) FROM TABLE

5条回答
  •  感动是毒
    2020-12-19 01:01

    Use UNPIVOT to convert columns to rows. Goes something like this:

    F1 F2 F3 F4 F5 F6 F7 F8 F9 F10

    124000 124001 124002 124003 124004 124005 124006 124007 124008 124009

    -- Converts columns into rows in table #JobNos1

    SELECT JobNo INTO #JobNos1 FROM

    (SELECT F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 FROM #YourTable) AS cp

    UNPIVOT

    (JobNo FOR JobNos IN (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10)) AS up

    124000

    124001

    124002

    124003

    124004

    124005

    124006

    124007

    124008

    124009

    -- It needs a way to select to row so Row # is added to #JobNos2

    CREATE TABLE #JobNos2(JobNo int, Row int)

    INSERT INTO #JobNos2

    SELECT JobNo, ROW_NUMBER() OVER (ORDER BY (SELECT 100)) AS Row from #JobNos1

    Then you can do something like this:

    SET @jobno = SELECT JobNo from #JobNos2 where Row = @SomeRowNumber

提交回复
热议问题