如何在sql server 2008中生成随机布尔值?

时间:2022-02-25 23:58:42

Im writing a database for some kind of university and there is a table named

我正在为某种大学写一个数据库,并且有一个名为的表

Contact_Assign Its parameters are:

Contact_Assign其参数是:

Is_Instructor       UD_BOOLEAN NOT NULL,
Is_TeacherAssistant UD_BOOLEAN NOT NULL,
Is_Student          UD_BOOLEAN NOT NULL,
Registration_ID     UD_ID      NOT NULL,
Contact_ID          UD_ID      NOT NULL,

now I want to insert dummy data in this table but I have no idea how can I do this for the boolean parameters.

现在我想在此表中插入虚拟数据,但我不知道如何为布尔参数执行此操作。

PS. UD_BOOLEAN is

PS。 UD_BOOLEAN是

CREATE TYPE UD_BOOLEAN FROM BIT

any idea how?

任何想法怎么样?

3 个解决方案

#1


6  

If you are only generating one row, you could use something as simple as:

如果您只生成一行,则可以使用以下简单的内容:

SELECT CAST(ROUND(RAND(),0) AS BIT)

However, if you are generating more than one row, RAND() will evaluate to the same value for every row, so please see Martin Smith's answer.

但是,如果您生成多行,RAND()将评估每行的相同值,因此请参阅Martin Smith的答案。

#2


19  

You can use

您可以使用

CRYPT_GEN_RANDOM(1) % 2

The advantages over RAND are that it is stronger cryptographically (you may not care) and that if inserting multiple rows it is re-evaluated for each row.

与RAND相比,它具有更强的加密性(您可能不关心),并且如果插入多行,则会对每行进行重新评估。

DECLARE @T TABLE(
  B1 BIT,
  B2 BIT);

INSERT INTO @T
SELECT TOP 10 CRYPT_GEN_RANDOM(1)%2,
              CAST(ROUND(RAND(), 0) AS BIT)
FROM   master..spt_values

SELECT *
FROM   @T 

would give the same value in all rows for the second column

将在第二列的所有行中给出相同的值

#3


3  

If you want a different value for the second column, you can use newid(). Here is an example:

如果要为第二列使用不同的值,可以使用newid()。这是一个例子:

select cast((case when left(newid(), 1) between '0' and '7' then 1 else 0 end) as bit)

You would only need newid() if you were inserting more than one row in a single statement.

如果在单个语句中插入多行,则只需要newid()。

#1


6  

If you are only generating one row, you could use something as simple as:

如果您只生成一行,则可以使用以下简单的内容:

SELECT CAST(ROUND(RAND(),0) AS BIT)

However, if you are generating more than one row, RAND() will evaluate to the same value for every row, so please see Martin Smith's answer.

但是,如果您生成多行,RAND()将评估每行的相同值,因此请参阅Martin Smith的答案。

#2


19  

You can use

您可以使用

CRYPT_GEN_RANDOM(1) % 2

The advantages over RAND are that it is stronger cryptographically (you may not care) and that if inserting multiple rows it is re-evaluated for each row.

与RAND相比,它具有更强的加密性(您可能不关心),并且如果插入多行,则会对每行进行重新评估。

DECLARE @T TABLE(
  B1 BIT,
  B2 BIT);

INSERT INTO @T
SELECT TOP 10 CRYPT_GEN_RANDOM(1)%2,
              CAST(ROUND(RAND(), 0) AS BIT)
FROM   master..spt_values

SELECT *
FROM   @T 

would give the same value in all rows for the second column

将在第二列的所有行中给出相同的值

#3


3  

If you want a different value for the second column, you can use newid(). Here is an example:

如果要为第二列使用不同的值,可以使用newid()。这是一个例子:

select cast((case when left(newid(), 1) between '0' and '7' then 1 else 0 end) as bit)

You would only need newid() if you were inserting more than one row in a single statement.

如果在单个语句中插入多行,则只需要newid()。