在SQL Server中尝试过这个脚本但是它在postgres sql中不起作用

时间:2016-07-26 07:09:03

标签: postgresql-9.4

declare @id int
SET @id = 1

WHILE (@id <= 100) LOOP
BEGIN
    INSERT INTO testing (name) 
    VALUES ('nick' + CONVERT(VARCHAR(5), @id),)

    SELECT @id = @id + 1
END LOOP;

在SQL Server中尝试过这个脚本,但它在PostgreSQL中不起作用。

它抛出了这个错误:

  

“错误:输入结束时语法错误LINE 15:END LOOP;”

1 个答案:

答案 0 :(得分:0)

您不能在PL / pgSQL函数(或do块)之外的Postgres中使用类似过程的代码

Postgres还尊重SQL标准,因此使用||来连接字符串,使用cast()运算符将值转换为不同的数据类型。

在Postgres中,您也不需要循环,您可以使用generate_series()动态生成行。

insert into testing(name) 
select 'nick' || cast(i as varchar)
from generate_series(1,100) i;