psql语法错误,尝试运行SQL脚本以创建表格。

huangapple go评论53阅读模式
英文:

psql syntax errors when trying to run sql script to create a table

问题

I was provided a .sql script containing a query to create a table. I trust the source of the script, so I am wondering if the problem is how/where I'm running it. I'm running it in psql, using a call like \i c:/Users/.../sql_script.sql. Here is the code in the script, modified slightly to make it look generic:

CREATE TABLE dbo.mytable (
	id int NOT NULL IDENTITY(1,1),
	userID int NULL,
	Probability DECIMAL(18,8) NULL,
	Conv_Thresh1 int NULL,
	Conv_Thresh2 int NULL,
	DateAddedToDB date NULL
);

ALTER TABLE dbo.mytable ADD CONSTRAINT some_text_i_dont_understand PRIMARY KEY (id);

CREATE NONCLUSTERED INDEX i1 ON mytable  (userID);

And here are the errors I'm seeing:

.sql:8: ERROR:  syntax error at or near "IDENTITY"
LINE 2:  id int NOT NULL IDENTITY(1,1),
                         ^
ALTER TABLE

and

.sql:12: ERROR:  syntax error at or near "NONCLUSTERED"
LINE 1: CREATE NONCLUSTERED INDEX i1 ON mytable  ...

Is this a problem with the script itself? I see similar errors trying to run other "table creation query" sql scripts that have been provided to me, so I'm wondering if it's more about me not understanding psql/postgres. Thank you.

英文:

I was provided a .sql script containing a query to create a table. I trust the source of the script, so I am wondering if the problem is how/where I'm running it. I'm running it in psql, using a call like \i c:/Users/.../sql_script.sql. Here is the code in the script, modified slightly to make it look generic:

CREATE TABLE dbo.mytable (
	id int NOT NULL IDENTITY(1,1),
	userID int NULL,
	Probability DECIMAL(18,8) NULL,
	Conv_Thresh1 int NULL,
	Conv_Thresh2 int NULL,
	DateAddedToDB date NULL
);

ALTER TABLE dbo.mytable ADD CONSTRAINT some_text_i_dont_understand PRIMARY KEY (id);

CREATE NONCLUSTERED INDEX i1 ON mytable  (userID);

And here are the errors I'm seeing:

.sql:8: ERROR:  syntax error at or near "IDENTITY"
LINE 2:  id int NOT NULL IDENTITY(1,1),
                         ^
ALTER TABLE

and

.sql:12: ERROR:  syntax error at or near "NONCLUSTERED"
LINE 1: CREATE NONCLUSTERED INDEX i1 ON mytable  ...

Is this a problem with the script itself? I see similar errors trying to run other "table creation query" sql scripts that have been provided to me, so I'm wondering if it's more about me not understanding psql/postgres. Thank you

答案1

得分: 1

Q: 这是脚本本身的问题吗?

在某种程度上是的。这个脚本显然是为 Microsoft SQL Server 设计的,其语法和特性与 PostgreSQL 在许多方面不同。

英文:

Q: Is this a problem with the script itself?

In a way, yes. This script is apparently meant for Microsoft SQL Server, whose syntax and features differ from PostgreSQL in many aspects.

答案2

得分: 0

以下是翻译好的部分:

"该语法用于 MSSQL Server。对于 PostgreSQL,它可能会是这样的:

CREATE TABLE mytable (
    id INT GENERATED ALWAYS AS IDENTITY
);

更多关于在 PostgreSQL 中使用 identity 的信息,请参阅 identity with postgres。"

英文:

The syntax looks for MSSQL Server. For postgres it would have been like:

CREATE TABLE mytable (
    id INT GENERATED ALWAYS AS IDENTITY
);

More info on using identity with postgres.

huangapple
  • 本文由 发表于 2023年5月18日 01:51:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定