使用数据创建序列号列索引到表中

时间:2013-11-05 10:05:26

标签: sql ms-access-2010

我想做this post之类的事情,所以我试过了:

SELECT 
   ROW_NUMBER() OVER(ORDER BY t.[Data Saida] ) AS id, 
   t.codigo, t.[Data Saida], t.Entidade, t.DataEnt, 
   t.[GTEntT Nº], t.Estado, t.[GTSaida Nº], t.[Observações1], 
   t.Eequisitante, t.Certificado, T.Resultado, T.Seleccionar, t.[Tipo de Intervenção]
FROM 
   [Movimento ferramentas] t;

但是我最终得到了类似

的东西
  

查询表达式中的语法错误(缺少运算符)ROW_NUMBER()OVER(ORDER BY t。[Data Saida])

是因为ROW_NUMBER() OVER()只是SQL Server还是我做错了什么?

我正在使用MS Access 2010。

这是该表中的一行:

Row of [Movimento ferramentas]

1 个答案:

答案 0 :(得分:1)

要向现有表格添加自动编号字段,只需在设计视图中打开表格,输入Field Name,然后从Data Type的下拉列表中选择“自动编号”:

AutoNumber.png

Access将使用表中任何现有记录的AutoNumber值填充新字段。

编辑re:影响自动编号值应用于现有记录的顺序

与许多其他数据库操作一样,在将AutoNumber值分配给现有记录时,Access基本上不会使用任何特定顺序的保证。但是,如果我们查看几个示例,我们可以看到Access 可能如何做到这一点。

考虑以下名为[Events]的示例表。行以随机顺序输入,没有主键:

EventDate   Event         
----------  --------------
2012-06-01  it's June     
2012-10-01  it's October  
2012-09-01  it's September
2012-12-01  it's December 
2012-11-01  it's November 
2012-07-01  it's July     
2012-04-01  it's April    
2012-08-01  it's August   
2012-02-01  it's February 
2012-01-01  it's January  
2012-03-01  it's March    
2012-05-01  it's May      

现在我们只需使用上面的过程添加一个名为[ID]的自动编号字段。在那之后

SELECT * FROM Events ORDER BY ID

返回

EventDate   Event           ID
----------  --------------  --
2012-06-01  it's June        1
2012-10-01  it's October     2
2012-09-01  it's September   3
2012-12-01  it's December    4
2012-11-01  it's November    5
2012-07-01  it's July        6
2012-04-01  it's April       7
2012-08-01  it's August      8
2012-02-01  it's February    9
2012-01-01  it's January    10
2012-03-01  it's March      11
2012-05-01  it's May        12

现在让我们回到表的旧副本,看看是否存在主键有所不同。我们将[事件日期]设为主键,保存对表格的更改,然后然后添加[ID]自动编号字段。完成后,上面的select语句给我们

EventDate   Event           ID
----------  --------------  --
2012-06-01  it's June        1
2012-10-01  it's October     2
2012-09-01  it's September   3
2012-12-01  it's December    4
2012-11-01  it's November    5
2012-07-01  it's July        6
2012-04-01  it's April       7
2012-08-01  it's August      8
2012-02-01  it's February    9
2012-01-01  it's January    10
2012-03-01  it's March      11
2012-05-01  it's May        12
嗯,嗯,同样的事情。因此,即使存在主键,看起来自动编号值也会以自然顺序(记录添加到表中的顺序)分配给表。

好的,如果是这种情况,那么让我们使用make-table查询以不同的顺序创建表的新副本

SELECT Events.EventDate, Events.Event 
INTO Events2
FROM Events
ORDER BY Events.EventDate;

现在让我们将[ID]自动编号字段添加到该新表中,看看我们得到了什么:

SELECT * FROM Events2 ORDER BY ID

返回

EventDate   Event           ID
----------  --------------  --
2012-01-01  it's January     1
2012-02-01  it's February    2
2012-03-01  it's March       3
2012-04-01  it's April       4
2012-05-01  it's May         5
2012-06-01  it's June        6
2012-07-01  it's July        7
2012-08-01  it's August      8
2012-09-01  it's September   9
2012-10-01  it's October    10
2012-11-01  it's November   11
2012-12-01  it's December   12

如果那是我们想要的订单,那么我们可以删除[Events]表并将[Events2]重命名为[Events]。