用于随机填充行的MySql过程仅填充一行

标签 mysql sql

我有一个随机填充表中记录的过程。 但是当我执行该过程时,该过程仅插入一行。 谁能帮忙!

create procedure aa 

 ( @row INT
  )

as
begin
declare @numint as int
 declare @string as  VARCHAR(10)  
declare @length as INT
declare @code as  INT
declare @oid  as  uniqueidentifier

set @numint =0;

WHILE @numint <= @row 

BEGIN 
   SET @numint = @numint + 1; 

   set @oid = newid(); 

   SET @length = ROUND(10*RAND(),0);

   SET @string = ''; 

   WHILE @length > 0 BEGIN 

      SET @length = @length-1;

      SET @code = ROUND(10*RAND(),0) - 1; 

      IF @code BETWEEN 1 AND 26  
         SET @string = @string + CHAR(ASCII('a')+@code-1);
      ELSE
         SET @string = @string + ' '; 
   END 
end
   -- Ready for the record

   SET NOCOUNT ON;

   INSERT INTO test_table VALUES (

      @oid,

      @numint,

      ROUND(2000000*RAND()-1000000,2),

      CONVERT(DATETIME, ROUND(50000*RAND()-10000,2)),

      @string

   )
END

exec procedure '10'

最佳答案

您的插入语句位于循环之外。将其向上移动到第一个循环内,您应该获得正确的行数。

关于用于随机填充行的MySql过程仅填充一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236504/

相关文章:

Python Scraper无法在mysql数据库中保存度数符号°

mysql - 从另一个表中选择列并创建一个列

mysql - 返 repo 买日期在注册日期后 7 天内的所有结果

c# - ASP.NET MVC 5/C# Foreach 循环 - 重复相同的数据

mysql - 使用单独的更新列插入重复键更新批处理

mysql - 如何在 MySQL 和 Cassandra 中获取行组

php - MYSQL-在主表和子表中创建和存储主键

mysql - 从 MYSQL 数据库中选择子记录的有效方法

c++ - fatal error C1083 : Cannot open include file: 'mysql_connection.h' in release mode

php - 更改 PHP 输出的特定字符的颜色