

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 更改 Python 和 Perl 应用程序以支持数据库从Microsoft SQL Server 迁移至兼容 Amazon Aurora PostgreSQL 的版本
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition"></a>

*Dwarika Patra 和 Deepesh Jayaprakash，Amazon Web Services*

## Summary
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-summary"></a>

此模式描述了将数据库从 Microsoft SQL Server 迁移到 Amazon Aurora PostgreSQL 兼容版时可能需要对应用程序存储库进行的更改。该模式假设这些应用程序基于 Python 或 Perl，并为这些脚本语言提供单独的指令。

将 SQL Server 数据库迁移至兼容 Aurora PostgreSQL 的数据库涉及架构转换、数据库对象转换、数据迁移和数据加载。由于 PostgreSQL 和 SQL Server 之间存在差异（与数据类型、连接对象、语法和逻辑有关），因此最困难的迁移任务是对代码库进行必要的更改，使其能够在 PostgreSQL 中正常运行。

对于基于 Python 的应用程序，连接对象和类分散在整个系统中。此外，Python 代码库可能使用多个库来连接到数据库。如果数据库连接接口发生变化，运行应用程序内联查询的对象也需要更改。

对于基于 Perl 的应用程序，更改涉及连接对象、数据库连接驱动程序、静态和动态内联 SQL 语句以及应用程序如何处理复杂的动态 DML 查询和结果集。

迁移应用程序时，您还可以考虑 AWS 上可能的增强功能，例如使用 Amazon Simple Storage Service (Amazon S3) 访问替换 FTP 服务器。

应用程序迁移过程涉及以下挑战：
+ 连接对象。如果连接对象分散在具有多个库和函数调用的代码中，您可能必须找到一种通用方法来更改它们以支持 PostgreSQL。
+ 记录检索或更新期间的错误或者异常处理。如果对返回变量、结果集或数据帧的数据库进行条件创建、读取、更新和删除 (CRUD) 操作，则任何错误或异常都可能导致应用程序错误并产生级联效应。应通过适当的验证和保存点来仔细处理这些问题。此类保存点之一是调用 `BEGIN...EXCEPTION...END` 块内的大型内联 SQL 查询或数据库对象。
+ 控制事务及其验证。其中包括手动和自动提交与回滚。Perl 的 PostgreSQL 驱动程序要求您始终明确设置自动提交属性。
+ 处理动态 SQL 查询。这需要对查询逻辑和迭代测试有深入的了解，以确保查询按预期工作。
+ 性能。您应该确保代码更改不会导致应用程序性能下降。

此模式详细解释了转换进程。

## 先决条件和限制
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-prereqs"></a>

**先决条件**
+ Python 和 Perl 语法工作知识。
+ SQL Server 和 PostgreSQL 基本技能。
+ 了解现有的应用程序架构。
+ 访问您的应用程序代码、SQL Server 数据库以及 PostgreSQL 数据库。
+ 使用开发、测试和验证应用程序更改的凭证访问 Windows 或 Linux (或其他 Unix)开发环境。
+ 对于基于 Python 的应用程序，您的应用程序可能需要的标准 Python 库，例如用于处理数据帧的 **Pandas**，以及用于数据库连接的 **psycopg2**。**SQLAlchemy**
+ 对于基于 Perl 应用程序，需要带有依赖库或模块的 Perl 包。全面的 Perl 存档网络 (CPAN) 模块可支持大多数应用程序要求。
+ 所有必需依赖自定义库或模块。 
+ 用于对 SQL Server 进行读取访问和 read/write 访问 Aurora 的数据库凭据。
+ PostgreSQL 通过服务和用户验证和调试应用程序更改。
+ 在应用程序迁移期间访问开发工具，例如Visual Studio Code、 Sublime Text 或 **pgAdmin**。

**限制**
+ 某些 Python 或 Perl 版本、模块、库以及包与云环境不兼容。
+ 某些用于 SQL Server 的第三方库和框架无法替换支持 PostgreSQL 迁移。 
+ 性能变化可能需要更改应用程序、内联 Transact-SQL (T-SQL) 查询、数据库函数以及存储过程。
+ PostgreSQL 支持表名、列名和其他数据库对象小写名称。 
+ 某些数据类型（例如 UUID 列）仅以小写形式存储。Python 和 Perl 应用程序必须要处理此类大小写差异。 
+ 必须使用 PostgreSQL 数据库相应文本列的正确数据类型来处理字符编码差异。                               

**产品版本**
+ Python 3.6 或更高版本（使用支持您的操作系统的版本）
+ Perl 5.8.3 或更高版本（使用支持您的操作系统的版本）
+ 兼容 Aurora PostgreSQL 的版本 4.2 或更高版本（查看[详细信息](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/AuroraPostgreSQL.Updates.20180305.html#AuroraPostgreSQL.Updates.20180305.42)）

## 架构
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-architecture"></a>

**源技术堆栈**
+ 脚本(应用程序编程)语言：Python 2.7 或更高版本，或 Perl 5.8 
+ 数据库：Microsoft SQL Server 版本 13
+ 操作系统：Red Hat Enterprise Linux (RHEL) 7 

**目标技术堆栈**
+ 脚本(应用程序编程)语言：Python 3.6 或更高版本，或 Perl 5.8 或更高版本 
+ 数据库：Aurora PostgreSQL-Compatible 4.2
+ 操作系统：RHEL 7 

**迁移架构**

![\[将带有 SQL Server 的 Perl 或 Python 应用程序迁移至兼容 Aurora PostgreSQL\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/b64de64a-bd55-4db7-ba7b-0a2557862af1/images/b8fab3e2-ded5-4f58-86bf-3f645252e9fc.png)


## 工具
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-tools"></a>

**AWS 工具和服务**
+ [Aurora PostgreSQL–Compatible Edition](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html) 是一个完全托管式、兼容 PostgreSQL 和 ACID 的关系数据库引擎，结合了高端商用数据库的速度和可靠性，同时还具有开源数据库的成本效益。Aurora PostgreSQL 是 PostgreSQL 的直接替代品，可以让您通过简单且经济高效的方式设置、运行和扩展新的和现有的 PostgreSQL 部署。
+ [AWS 命令行界面（AWS CLI）](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html)是一种开源工具，它使您能够使用命令行 Shell 中的命令与 Amazon Web Services 交互。

**其他工具**
+ [Python 和 P](https://www.python.org/) [ostgresSQL 数据库连接库，比如 psycopg2 和 [SQLAlchemy](https://www.sqlalchemy.org/)](https://pypi.org/project/psycopg2/)
+ [Perl](https://www.perl.org/) 及其 [DBI 模块](https://metacpan.org/pod/DBD::Pg)
+ [PostgreSQL 交互式终端](https://www.postgresql.org/docs/13/app-psql.html) (psql)

## 操作说明
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-epics"></a>

### 将您的应用程序存储库迁移至 PostgreSQL — 高级步骤
<a name="migrate-your-application-repository-to-postgresql-ndash-high-level-steps"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 按照以下代码转换步骤将您的应用程序迁移至 PostgreSQL。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html)以下操作说明详细说明了 Python 和 Perl 应用程序的一些转换任务。 | 应用程序开发人员 | 
| 为迁移的每个步骤使用清单。 | 将以下内容添加到应用程序迁移的每个步骤（包括最后一步）的清单中：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 

### 分析和更新您的应用程序 — Python 代码库
<a name="analyze-and-update-your-application-ndash-python-code-base"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 分析现有的 Python 代码库。 | 您的分析应包含以下内容，以简化应用程序迁移过程：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 
| 将您的数据库连接转换至支持 PostgreSQL。 | 大多数 Python 应用程序使用 **pyodbc** 库连接 SQL Server 数据库，如下所示。<pre>import pyodbc<br />....<br />try:<br />    conn_string = "Driver=ODBC Driver 17 for SQL<br />    Server;UID={};PWD={};Server={};Database={}".format (conn_user, conn_password,<br />    conn_server, conn_database)<br />    conn = pyodbc.connect(conn_string)<br />    cur = conn.cursor()<br />    result = cur.execute(query_string)<br />    for row in result:<br />    print (row)<br />except Exception as e:<br />    print(str(e))</pre>将数据库连接转换至支持 PostgreSQL，如下所示。<pre>import pyodbc<br />import psycopg2<br />....<br />try:<br />    conn_string = ‘postgresql+psycopg2://’+<br />    conn_user+’:’+conn_password+’@’+conn_server+’/’+conn_database<br />    conn = pyodbc.connect(conn_string, connect_args={‘options’:’-csearch_path=dbo’})<br />    cur = conn.cursor()<br />    result = cur.execute(query_string)<br />    for row in result:<br />    print (row)<br />except Exception as e:<br />    print(str(e))</pre> | 应用程序开发人员 | 
| 将内联 SQL 查询更改为 PostgreSQL。 | 将您的内联 SQL 查询转换为与 PostgreSQL 兼容的格式。例如，以下 SQL Server 查询从表中检索字符串。<pre>dtype = "type1"<br />stm = ‘"SELECT TOP 1 searchcode FROM TypesTable (NOLOCK)<br />WHERE code="’ + "’" + str(dtype) + "’"<br /># For Microsoft SQL Server Database Connection<br />engine = create_engine(‘mssql+pyodbc:///?odbc_connect=%s’ % urllib.parse.quote_plus(conn_string), connect_args={‘connect_timeout’:login_timeout})<br />conn = engine_connect()<br />rs = conn.execute(stm)<br />for row in rs:<br />    print(row)</pre>转换后，与 PostgreSQL 兼容的内联 SQL 查询如下所示。<pre>dtype = "type1"<br />stm = ‘"SELECT searchcode FROM TypesTable<br />WHERE code="’ + "’" + str(dtype) + "’ LIMIT 1"<br /># For PostgreSQL Database Connection<br />engine = create_engine(‘postgres+psycopg2://%s’ %conn_string, connect_args={‘connect_timeout’:login_timeout})<br />conn = engine.connect()<br />rs = conn.execute(stm)<br />for row in rs:<br />    print(row)</pre> | 应用程序开发人员 | 
| 处理动态 SQL 查询。 | 动态 SQL 可以出现在一个脚本或多个 Python 脚本中。前面的示例展示了如何使用 Python 的字符串替换函数插入变量以构建动态 SQL 查询。另一种方法是在适用的情况下在查询字符串中附加变量。 在以下示例中，查询字符串是根据函数返回的值动态构造的。<pre>query = ‘"SELECT id from equity e join issues i on e.permId=i.permId where e.id’"<br />query += get_id_filter(ids) + " e.id is NOT NULL</pre>这些类型的动态查询在应用程序迁移过程中非常常见。请按照以下步骤处理动态查询：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 
| 处理结果集、变量与数据框。 | 对于 Microsoft SQL Server，您可以使用 Python 方法（例如 `fetchone()` 或 `fetchall()`）从数据库中检索结果集。您也可以使用 `fetchmany(size)` 并指定要从结果集中返回的记录数。为此，您可使用 **pyodbc** 连接对象，如以下示例中所示。**pyodbc (Microsoft SQL Server)**<pre>import pyodbc <br />server = 'tcp:myserver.database.windows.net' <br />database = 'exampledb' <br />username = 'exampleusername' <br />password = 'examplepassword' <br />conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)<br />cursor = conn.cursor()<br />cursor.execute("SELECT * FROM ITEMS") <br />row = cursor.fetchone() <br />while row: <br />    print(row[0])<br />    row = cursor.fetchone()</pre>**在 Aurora 中，要执行类似的任务，例如连接到 PostgreSQL 和获取结果集，你可以使用 psycopg2 或。**SQLAlchemy****这些 Python 库提供了连接模块和游标对象来遍历 PostgreSQL 数据库记录，如以下示例所示。**psycopg2（兼容Aurora PostgreSQL）**<pre>import psycopg2<br />query = "SELECT * FROM ITEMS;"<br />//Initialize variables<br />host=dbname=user=password=port=sslmode=connect_timeout=""<br />connstring = "host='{host}' dbname='{dbname}' user='{user}' \<br />password='{password}'port='{port}'".format(host=host,dbname=dbname,\<br />user=user,password=password,port=port) <br />conn = psycopg2.connect(connstring)<br />cursor = conn.cursor()<br />cursor.execute(query)<br />column_names = [column[0] for column in cursor.description]<br />print("Column Names: ", column_names)<br />print("Column values: "<br />for row in cursor:<br />    print("itemid :", row[0])<br />    print("itemdescrption :", row[1])<br />    print("itemprice :", row[3]))</pre>**SQLAlchemy （兼容 Aurora PostgreSQL）**<pre>from sqlalchemy import create_engine<br />from pandas import DataFrame<br />conn_string = 'postgresql://core:database@localhost:5432/exampledatabase'<br />engine = create_engine(conn_string)<br />conn = engine.connect()<br />dataid = 1001<br />result = conn.execute("SELECT * FROM ITEMS")<br />df = DataFrame(result.fetchall())<br />df.columns = result.keys()<br />df = pd.DataFrame()<br />engine.connect()<br />df = pd.read_sql_query(sql_query, engine, coerce_float=False)<br />print("df=", df)</pre> | 应用程序开发人员 | 
| 在迁移期间和迁移之后测试应用程序。 | 测试迁移的 Python 应用程序是持续的过程。由于迁移包括连接对象更改（**psycopg2** 或 **SQLAlchemy**）、错误处理、新功能（数据框）、内联 SQL 更改、批量复制功能（`bcp`而不是`COPY`）和类似更改，因此在应用程序迁移期间和迁移之后都必须对其进行仔细测试。检查：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 

### 分析和更新您的应用程序 — Perl 代码库
<a name="analyze-and-update-your-application-ndash-perl-code-base"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 分析现有 Perl 代码库。 | 您的分析应包含以下内容，以简化应用程序迁移过程。您应确定：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 
| 转换 Perl 应用程序和 DBI 模块的连接以支持 PostgreSQL。 | 基于Perl的应用程序通常使用 Perl DBI 模块，它是 Perl 编程语言的标准数据库访问模块。您可以为 SQL Server 和 PostgreSQL 使用相同 DBI 模块和不同的驱动程序。有关所需 Perl 模块、安装和其他说明的更多信息，请参阅 [DBD::Pg documentation](https://metacpan.org/pod/DBD::Pg)。以下示例连接到与 Aurora PostgreSQL 兼容的网址 `exampletest-aurorapg-database.cluster-sampleclusture.us-east.-rds.amazonaws.com`。<pre>#!/usr/bin/perl<br />use DBI;<br />use strict;<br />my $driver = "Pg";<br />my $hostname = "exampletest-aurorapg-database-sampleclusture.us-east.rds.amazonaws.com"<br />my $dsn = "DBI:$driver: dbname = $hostname;host = 127.0.0.1;port = 5432";<br />my $username = "postgres";<br />my $password = "pass123";<br />$dbh = DBI->connect("dbi:Pg:dbname=$hostname;host=$host;port=$port;options=$options",<br />      $username,<br />      $password,<br />      {AutoCommit => 0, RaiseError => 1, PrintError => 0}<br />      );</pre> | 应用程序开发人员 | 
| 将内联 SQL 查询更改为 PostgreSQL。 | 您的应用程序可能包含带有 `SELECT`、`DELETE`、`UPDATE` 的内联 SQL 查询，以及包含 PostgreSQL 不支持的查询子句的类似语句。例如，PostgreSQL 中不支持 `TOP` 和 `NOLOCK` 等查询关键字。以下示例说明如何处理 `TOP`、`NOLOCK` 和 Boolean 变量。SQL Server 中：<pre>$sqlStr = $sqlStr<br />. "WHERE a.student_id in (SELECT TOP $numofRecords c_student_id   \<br />FROM active_student_record b WITH (NOLOCK) \<br />INNER JOIN student_contributor c WITH (NOLOCK) on c.contributor_id = b.c_st)</pre>对于 PostgreSQL，请转换为：<pre>$sqlStr = $sqlStr<br />. "WHERE a.student_id in (SELECT TOP $numofRecords c_student_id  \<br />FROM active_student_record b INNER JOIN student_contributor c  \<br />on c.contributor_id = b.c_student_contr_id WHERE b_current_1 is true \<br />LIMIT $numofRecords)"</pre> | 应用程序开发人员 | 
| 处理动态 SQL 查询与 Perl 变量。 | 动态 SQL 查询是在应用程序运行时生成 SQL 语句。这些查询是在应用程序运行时根据某些条件动态构建的，因此直到运行时才知道查询的全文。一个例子是一个金融分析应用程序，它每天分析排名前 10 的股票，并且这些股票每天都在变化。SQL 表是根据最佳执行者创建的，并且直到运行时才知道这些值。假设此示例的内联 SQL 查询被传递给包装函数以获取变量中的结果集，然后变量使用条件来确定表是否存在：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html)下面是变量处理的示例，后面是该用例的 SQL Server 和 PostgreSQL 查询。<pre>my $tableexists = db_read( arg 1, $sql_qry, undef, 'writer');<br />my $table_already_exists = $tableexists->[0]{table_exists};<br />if ($table_already_exists){<br /># do some thing<br />}<br />else {<br /># do something else<br />}</pre>SQL Server：<pre>my $sql_qry = "SELECT OBJECT_ID('$backendTable', 'U') table_exists", undef, 'writer')";</pre>PostgreSQL：<pre>my $sql_qry = "SELECT TO_REGCLASS('$backendTable', 'U') table_exists", undef, 'writer')";</pre>以下示例在内联 SQL 中使用一个** **Perl 变量，该变量使用带有 `JOIN` 的 `SELECT` 语句来获取表的主键和键列的位置。SQL Server：<pre>my $sql_qry = "SELECT column_name', character_maxi mum_length \<br />FROM INFORMATION_SCHEMA.COLUMNS \<br />WHERE TABLE_SCHEMA='$example_schemaInfo' \<br />AND TABLE_NAME='$example_table' \<br />AND DATA_TYPE IN ('varchar','nvarchar');";</pre>PostgreSQL：<pre>my $sql_qry = "SELECT c1.column_name, c1.ordinal_position \<br />FROM information_schema.key_column_usage AS c LEFT \<br />JOIN information_schema.table_constraints AS t1 \<br />ON t1.constraint_name = c1.constraint_name \<br />WHERE t1.table_name = $example_schemaInfo'.'$example_table’ \<br />AND t1.constraint_type = 'PRIMARY KEY' ;";</pre> | 应用程序开发人员 | 

### 对基于 Perl 或 Python 的应用程序进行其他更改以支持 PostgreSQL
<a name="make-additional-changes-to-your-perl-based-or-python-based-application-to-support-postgresql"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将其他 SQL Server 结构转换至 PostgreSQL。 | 以下更改适用于所有应用程序，无论编程语言如何。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition.html) | 应用程序开发人员 | 

### 提高性能。
<a name="improve-performance"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 利用 Amazon Web Services 提高性能。 | 迁移至 Amazon Web Services Cloud 时，您可以完善应用程序和数据库设计以利用 Amazon Web Services。例如，如果来自连接到 Aurora PostgreSQL 兼容数据库服务器的 Python 应用程序的查询比原始 Microsoft SQL Server 查询花费更多时间，您可以考虑将历史数据直接创建到 Amazon Simple Storage 来自 Amazon Simple Storage Service (Amazon S3) 存储桶，并使用基于 Amazon Athena 的 SQL 查询为用户控制面板生成报告和分析数据查询。 | 应用程序开发人员、云架构师 | 

## 相关资源
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-resources"></a>
+ [Perl](https://www.perl.org/)
+ [Perl DBI 模块](https://metacpan.org/pod/DBI)
+ [Python](https://www.python.org/)
+ [psycopg2](https://pypi.org/project/psycopg2/)
+ [SQLAlchemy](https://www.sqlalchemy.org/)
+ [批量复制 - PostgreSQL](https://www.postgresql.org/docs/9.2/sql-copy.html)
+ [批量复制 — Microsoft SQL Server](https://docs.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-ver15)
+ [PostgreSQL](https://www.postgresql.org/)
+ [使用 Amazon Aurora PostgreSQL](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/Aurora.AuroraPostgreSQL.html)

## 附加信息
<a name="change-python-and-perl-applications-to-support-database-migration-from-microsoft-sql-server-to-amazon-aurora-postgresql-compatible-edition-additional"></a>

Microsoft SQL Server 和 Aurora PostgreSQL 兼容均符合 ANSI SQL。但是，在将 Python 或 Perl 应用程序从 SQL Server 迁移至 PostgreSQL 时，您仍应注意语法、列数据类型、本地数据库专用函数、批量插入和区分大小写等方面的任何不兼容之处。

以下部分提供有关每个不一致地方的更多信息。

**数据类型比较**

从 SQL Server 到 PostgreSQL 的数据类型更改可能会导致应用程序操作的结果数据出现显着差异。有关数据类型的比较，请参阅 [Sqlines 网站](https://www.sqlines.com/sql-server-to-postgresql)的表格。

**原生或内置 SQL 函数**

SQL Server 和 PostgreSQL 数据库之间的某些函数的行为有所不同。下表提供了对比。


| 
| 
| Microsoft SQL Server | 说明 | PostgreSQL | 
| --- |--- |--- |
| `CAST`  | 将值从一个数据类型转换为另一个数据类型。 | PostgreSQL `type :: operator` | 
| `GETDATE()` | 以某种 `YYYY-MM-DD hh:mm:ss.mmm` 格式返回当前数据库系统的日期和时间。 | `CLOCK_TIMESTAMP` | 
| `DATEADD` | 为日期添加 time/date 间隔。 | `INTERVAL` 表达式 | 
| `CONVERT` | 将值转换为特定数据格式。 | `TO_CHAR` | 
| `DATEDIFF` | 返回两个日期字段相差的天数。 | `DATE_PART` | 
| `TOP` | 限制 `SELECT` 结果集中的行数。 | `LIMIT/FETCH` | 

**匿名区块**

结构化 SQL 查询分为声明、可执行文件以及异常处理等部分。下表比较了 Microsoft SQL Server 和 PostgreSQL 版本的简单匿名块。对于复杂匿名块，我们建议您在应用程序中调用自定义数据库函数。


| 
| 
| Microsoft SQL Server | PostgreSQL | 
| --- |--- |
| <pre>my $sql_qry1=<br />my $sql_qry2 =<br />my $sqlqry = "BEGIN TRAN<br />$sql_qry1 $sql_qry2<br />if @\@error !=0 ROLLBACK<br />TRAN<br />else COMIT TRAN";</pre> | <pre>my $sql_qry1=<br />my $sql_qry2 =<br />my $sql_qry = " DO \$\$<br />BEGIN<br />$header_sql $content_sql<br />END<br />\$\$";</pre> | 

 

**其他区别**
+ **批量插入行：** [Microsoft SQL Server bcp utility](https://docs.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-ver15) 实用程序的 PostgreSQL 等效工具是 [COPY](https://www.postgresql.org/docs/9.2/sql-copy.html)。
+ **区分大小写：**在 PostgreSQL 中，列名区分大小写，因此您必须将 SQL Server 列名转换为小写或大写。当您提取或比较数据，或者将列名放置在结果集或变量中时，这将成为一个因素。以下示例标识了可能以大写或小写形式存储值列。

```
my $sql_qry = "SELECT $record_id FROM $exampleTable WHERE LOWER($record_name) = \'failed transaction\'"; 
```
+ **串联：**SQL Server 使用 `+` 作为字符串连接的运算符，而 PostgreSQL 则使用`||`。
+ **验证：**在 PostgreSQL 的应用程序代码中使用内联 SQL 查询和函数前，应对其进行测试和验证。
+ **ORM 库的包含：**你也可以寻找包含现有数据库连接库，或者用 PynoModb 等 Python ORM 库替换现有数据库连接库[。[SQLAlchemy](https://www.sqlalchemy.org/)](https://pynamodb.readthedocs.io/en/latest/quickstart.html)这将有助于使用面向对象的范例轻松地查询和操作数据库中的数据。