函数编写SQL如何给记录加序号

有五种方法:1、必须使用临时表来实现selectIDENTITY(int,1,1)ASID_Num,*into#tempfromtableselect*from#tempDropTable#temp2、没有临时表,必须有排序列具有唯一值参考:selectt(selectcount(*)framyourtablewherecol<=A.col)row,*framyourtableAorderbycol3.在原表中添加一列来实现altertableyourtableaddIDintidentityselect*framyourtablealtertableyourtabledropcolumnID4、使用SQLServer2005特有的RANK()OVER()语法(测试客户编号也必须唯一)SELECTRANK()OVER(ORDERBYcustomer)帐号DESC)AS序列号、公司名称FROM客户五、选择客户编号,b。
公司名称FROM客户ASb编号GROUPBYa客户编号,a。
按序列号订购

sql2008关于查询数据时,添加一个自增序列号的列的问题

第一种方法:使用ROW_NUMBER()OVER(ORDERBY你原来的排序方法);第二种方法:为orderby添加相同值的列,例如:selectrow_number()over(orderbyorderid),t1.*from(select1asorderid,t.*fromtablet)t1;第三种方式:使用Identity+临时表,例如:selectIdentity(int,1,1),t.*Into#temptablefromtablet;select*from#temptable;