Sql 随机更新一条数据返回更新数据的ID编号
DECLARE @parimaryTable(临时表) Table(prizecode varchar(50));
update top (1) 数据表 set 字段a='数值' ,字段b=‘数值’ output deleted.prizecode into @parimaryTable(临时表) where 条件字段 ; select * from @parimaryTable
样例
public string GetUserPrizeChangeListRamdom(string customerId, int userid) { if (!customerId.Equals("0")) using (var db = new SqlServer()) { var updateSql = string.Format(@"DECLARE @parimaryTable Table(prizecode varchar(50)); update top (1) UserPrizeChangeList set consumerid='{0}' ,userid={1},status=1,usetime=getdate() output deleted.prizecode into @parimaryTable where status=0 ; select * from @parimaryTable", db.InjectReplace(customerId), userid); return db.GetScalar<string>(updateSql); } else return null; }