SQL 服务器:OPENQUERY 简而言之(带示例)
SQL Server: OPENQUERY in easy words (with example)
我是 SQL 服务器的新手,我在这里偶然发现了这个:
BEGIN TRANSACTION loadTTAllocations
INSERT INTO @ttaAllocs
SELECT
company_code AS CompanyId,
job_number AS JobNo,
...
is_blocked AS IsBlocked
FROM
OPENQUERY([SRV_TimeTac],
'SELECT DISTINCT
job.individual_value_1 AS job_number,
job.individual_value_3 AS company_code,
...
now() AS queryTimeStamp
FROM
pm_altran.pm_tasks_subprojects AS taskCode
INNER JOIN
pm_altran.pm_tasks_subprojects AS job
ON job.id = taskCode.mother_id
AND job.is_done = 0
AND NOT job.is_blocked
INNER JOIN
pm_node_to_user AS n2u
ON n2u.node_id = taskCode.id
AND n2u.access = 1
AND n2u.is_todo = 1
LEFT JOIN
altran_pm_user_user_settings AS u
ON u.administrators_id = n2u.user_id
WHERE
taskCode.object_type = ''task''
AND taskCode.is_paid_non_working = 0
AND taskCode.id > 50');
SET @rowCount = @@ROWCOUNT
SET @eventDetails = 'End loadTTAllocations: ' + CAST(@rowCount as VARCHAR(10)) + ' rows affected';
COMMIT TRANSACTION loadTTAllocations
问题是OPENQUERY
。
我从文档中了解到的基本上是,它只是在另一台服务器上的查询。在这种情况下 SRV_TimeTac?
所以在这个例子中,我们在另一台名为 "SRV_TimeTac" 的服务器上查询并返回结果,最终加载到名为 @ttaAllocs 的临时 table 中。
这是正确的吗?
感谢您的帮助。
Executes the specified pass-through query on the specified linked
server. This server is an OLE DB data source. OPENQUERY
can be
referenced in the FROM clause of a query as if it were a table name.
OPENQUERY
can also be referenced as the target table of an INSERT
,
UPDATE
, or DELETE
statement. This is subject to the capabilities of
the OLE DB provider. Although the query may return multiple result
sets, OPENQUERY
returns only the first one.
Arguments
linked_server
Is an identifier representing the name of the linked server.
' query '
Is the query string executed in the linked server. The maximum length of the string is 8 KB.
所以,要回答您的问题,是的,这就是您的查询所做的。在你的例子中 linked_server 的值是 SRV_TimeTac
而 ' query ' 的值是你的长文字字符串。 ' query ' 的值在链接服务器上是 运行 并且结果集返回到您 运行 OPENQUERY
所在的服务器。然后将该结果集插入变量 @ttaAllocs
.
有趣的是,然后 COMMIT
你在 INSERT
之前开始的 t运行saction对象。 Table 变量很可能最终被写入光盘,而不是存储在内存中,如果它变得足够大,那么您很可能会将 t运行saction 提交给 tempdb
.
我是 SQL 服务器的新手,我在这里偶然发现了这个:
BEGIN TRANSACTION loadTTAllocations
INSERT INTO @ttaAllocs
SELECT
company_code AS CompanyId,
job_number AS JobNo,
...
is_blocked AS IsBlocked
FROM
OPENQUERY([SRV_TimeTac],
'SELECT DISTINCT
job.individual_value_1 AS job_number,
job.individual_value_3 AS company_code,
...
now() AS queryTimeStamp
FROM
pm_altran.pm_tasks_subprojects AS taskCode
INNER JOIN
pm_altran.pm_tasks_subprojects AS job
ON job.id = taskCode.mother_id
AND job.is_done = 0
AND NOT job.is_blocked
INNER JOIN
pm_node_to_user AS n2u
ON n2u.node_id = taskCode.id
AND n2u.access = 1
AND n2u.is_todo = 1
LEFT JOIN
altran_pm_user_user_settings AS u
ON u.administrators_id = n2u.user_id
WHERE
taskCode.object_type = ''task''
AND taskCode.is_paid_non_working = 0
AND taskCode.id > 50');
SET @rowCount = @@ROWCOUNT
SET @eventDetails = 'End loadTTAllocations: ' + CAST(@rowCount as VARCHAR(10)) + ' rows affected';
COMMIT TRANSACTION loadTTAllocations
问题是OPENQUERY
。
我从文档中了解到的基本上是,它只是在另一台服务器上的查询。在这种情况下 SRV_TimeTac?
所以在这个例子中,我们在另一台名为 "SRV_TimeTac" 的服务器上查询并返回结果,最终加载到名为 @ttaAllocs 的临时 table 中。
这是正确的吗?
感谢您的帮助。
Executes the specified pass-through query on the specified linked server. This server is an OLE DB data source.
OPENQUERY
can be referenced in the FROM clause of a query as if it were a table name.OPENQUERY
can also be referenced as the target table of anINSERT
,UPDATE
, orDELETE
statement. This is subject to the capabilities of the OLE DB provider. Although the query may return multiple result sets,OPENQUERY
returns only the first one.Arguments
linked_server
Is an identifier representing the name of the linked server.' query '
Is the query string executed in the linked server. The maximum length of the string is 8 KB.
所以,要回答您的问题,是的,这就是您的查询所做的。在你的例子中 linked_server 的值是 SRV_TimeTac
而 ' query ' 的值是你的长文字字符串。 ' query ' 的值在链接服务器上是 运行 并且结果集返回到您 运行 OPENQUERY
所在的服务器。然后将该结果集插入变量 @ttaAllocs
.
有趣的是,然后 COMMIT
你在 INSERT
之前开始的 t运行saction对象。 Table 变量很可能最终被写入光盘,而不是存储在内存中,如果它变得足够大,那么您很可能会将 t运行saction 提交给 tempdb
.