如何在 SQL Server 2005 中将随机数作为列返回?

How do I return random numbers as a column in SQL Server 2005?(如何在 SQL Server 2005 中将随机数作为列返回?)
本文介绍了如何在 SQL Server 2005 中将随机数作为列返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SQL Server 2005 上运行 SQL 查询,除了从数据库查询 2 列之外,我还想连同它们一起返回 1 列随机数.我试过这个:

I'm running a SQL query on SQL Server 2005, and in addition to 2 columns being queried from the database, I'd also like to return 1 column of random numbers along with them. I tried this:

select column1, column2, floor(rand() * 10000) as column3 
from table1

哪种方式有效,但问题是此查询在每一行返回相同的随机数.每次运行查询时它都是一个不同的数字,但它不会因行而异.我怎样才能做到这一点并为每一行获取一个新的随机数?

Which kinda works, but the problem is that this query returns the same random number on every row. It's a different number each time you run the query, but it doesn't vary from row to row. How can I do this and get a new random number for each row?

推荐答案

我意识到这是一篇较旧的帖子......但你不需要视图.

I realize this is an older post... but you don't need a view.

select column1, column2, 
  ABS(CAST(CAST(NEWID() AS VARBINARY) AS int)) % 10000 as column3 
from table1

这篇关于如何在 SQL Server 2005 中将随机数作为列返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)