将SQL Server脚本转换为PostgreSQL

时间:2016-03-17 06:48:45

标签: sql-server postgresql

SQL Server中有一个需要在PostgreSQL中转换的脚本。 这是脚本:

UPDATE Categories_convert SET Categories_convert.ParentID = 
Categories_convert_1.CategoryID 
FROM Categories_convert LEFT OUTER JOIN Categories_convert AS 
Categories_convert_1 ON Categories_convert.MainGroupID_FK = 
Categories_convert_1.MainGroupID 
WHERE (((Categories_convert.Level)=2));

然后我尝试将其转换为postgres。这是脚本:

UPDATE x_tmp_categories_convert SET orig.parentid = 
cat2.categoryid 
FROM x_tmp_categories_convert LEFT OUTER JOIN x_tmp_categories_convert AS 
cat2 ON x_tmp_categories_convert.maingroupid_fk = 
x_tmp_categories_convert.maingroupid 
WHERE (((cat.level)=2));

请注意,我已经将SQLServer的表 Categories_convert 创建为Postgresql,并将其重命名为 x_tmp_categories_convert 。 postgresql中的所有字段都是小写的。

现在问题是当我执行转换后的脚本到postgresql时,会发生错误:

  

错误:表名" x_tmp_categories_convert"指定不止一次   SQL状态:42712

我在转换中做错了什么?

更新

我已经尝试了@a_horse_with_no_name的答案,但它根本没有更新记录。 parentid 字段仍为空。它应该根据maingoupid_fk映射该分类的所有parentid。

以下是执行建议脚本后的​​记录快照。 出于披露原因,我选择了这个名字。

Records snapshot link

更新v2: 我正在使用php来迁移数据,所以请原谅我使用的变量。 以下是在执行质询更新脚本之前使用的2个插入语句:

INSERT INTO x_tmp_categories_convert(maingroupid,name,vendorid,level,parentid)
VALUES ($id,'$mainGroup',$vendorID,1,Null);

INSERT INTO x_tmp_categories_convert(subgroupid,maingroupid_fk,name,vendorid,level)
VALUES ($id,'$mainGroupId','$subGroup',$vendorID,2);

此外,这是x_tmp_categories_convert表的表定义:

CREATE TABLE x_tmp_categories_convert
(
  categoryid serial NOT NULL,
  parentid double precision,
  name character varying(255),
  level double precision,
  vendorid double precision,
  maingroupid integer,
  subgroupid integer,
  maingroupid_fk integer,
  pageid integer,
  subgroupid_fk integer,
  CONSTRAINT code_pk PRIMARY KEY (categoryid)
)
WITH (
  OIDS=FALSE
);

更新v3: a_horse_with_no_name已经已解决。谢谢

1 个答案:

答案 0 :(得分:0)

我编辑了@a_horse_with_no_name的答案,并使其有效。我想这只是一个复制粘贴错误。

这是最终的脚本:

UPDATE x_tmp_categories_convert 
   SET parentid = cat.categoryid 
FROM x_tmp_categories_convert AS cat
WHERE x_tmp_categories_convert.maingroupid_fk = cat.maingroupid 
AND x_tmp_categories_convert.level = 2;

对于@a_horse_with_no_name,我非常感谢你,因为我无法在没有你帮助的情况下提出解决方案。 +1给你的男人。谢谢