【发布时间】:2021-11-20 13:09:55
【问题描述】:
我正在整理一个存储过程,它将一些查询结果作为 CSV 附件发送到电子邮件中。我正在测试程序并收到此错误:
Msg 22050, Level 16, State 1, Line 2 格式查询错误,可能 无效参数 Msg 14661,级别 16,状态 1,程序 sp_send_dbmail,第 517 行 [Batch Start Line 2] 查询执行失败: 消息 102,级别 15,状态 1,服务器 NRWOGMSQL6ST\SQLSTD2012,第 27 行 'tbl' 附近的语法不正确。
我知道在 CSV 中产生结果的查询有效,因为我已经在 SP 之外对其进行了测试。我相信所有的引用都是正确的,因为我打印了查询并仔细检查了转义引号。我尝试使用@query_result_no_padding = 1,但它在附加的 CSV 中给了我同样的错误。我也知道 SP 的电子邮件部分有效,因为我一直在使用测试查询来测试事物(请参阅以“SELECT TOP 100”开头的注释掉查询)。
我相信错误是指 tblAPDTracker 表。我尝试注释掉这一行,但下一个错误与“Well”表相同。我不能注释掉井表,因为它是我使用的第一张表。有什么方法可以获取有关问题所在以及如何解决问题的更多信息?
USE [UTRBDMSNET]
GO
/****** Object: StoredProcedure [dbo].[TestProcedure] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[HiebingTestProcedure]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @message VARCHAR(1000);
DECLARE @subject VARCHAR(100);
DECLARE @query VARCHAR(1000);
DECLARE @tab char(1) = CHAR(9);
DECLARE @query_attachment_filename VARCHAR(520);
DECLARE @CRLF char(2);
SELECT @CRLF = CHAR(13)+CHAR(10)
SELECT @subject = 'Well Information Report'
SELECT @message = N'Dear Zeke, '
+@CRLF+ ''
+@CRLF+ N'Please refer to the attached spread sheet for the results of last month''s information report.'
+@CRLF+ ''
+@CRLF+ 'Thanks,'
+@CRLF+ 'Matt';
--SELECT @query =
-- 'SET NOCOUNT ON;
-- SELECT TOP 100 WellID, Operator, WellStatus, ModifyUser
-- FROM UTRBDMSNET.dbo.Well';
SELECT @query =
'
SET NOCOUNT ON;
DECLARE @SearchYear AS VARCHAR(4) = 2020
DECLARE @SearchMonth AS VARCHAR(2) = 7
SELECT
API14,
[Entity Number],
[First Prod Date],
[Spacing Rule],
TPI AS ''Top Producing Interval Location'',
BH AS ''Bottom Hole Location'',
[Well History Comments],
[Well History Modify Date] AS ''Last Modified Date''
FROM
(
SELECT
dbo.BuildAPI14(Well.WellID, Construct.SideTrack, Construct.Completion) AS ''API14'',
CAST(ConstructDate.EventDate AS DATE) AS ''First Prod Date'',
Loc.LocType AS ''Location Type'',
CONCAT(''Township '',LocExt.Township,LocExt.TownshipDir,'' '',''Range '',LocExt.Range,LocExt.RangeDir,'' Section '',LocExt.Sec,'' '',RefCounty.CountyName,'' County'') AS ''Location'',
tblAPDTracker.SpacingRule AS ''Spacing Rule'',
Lease.Number AS ''Entity Number'',
WellHistory.WHComments AS ''Well History Comments'',
WellHistory.ModifyDate AS ''Well History Modify Date''
FROM UTRBDMSNET.dbo.Well
LEFT JOIN UTRBDMSNET.dbo.tblAPDTracker ON LEFT(tblAPDTracker.APINO,10) = Well.WellID
LEFT JOIN UTRBDMSNET.dbo.Construct ON Construct.WellKey = Well.PKey
LEFT JOIN UTRBDMSNET.dbo.ConstructReservoir ON ConstructReservoir.ConstructKey = Construct.PKey
LEFT JOIN UTRBDMSNET.dbo.Lease ON Lease.Pkey = ConstructReservoir.LeaseKey
LEFT JOIN UTRBDMSNET.dbo.WellHistory ON WellHistory.WellKey = Construct.WellKey
LEFT JOIN UTRBDMSNET.dbo.ConstructDate ON ConstructDate.ConstructKey = Construct.PKey AND ConstructDate.Event = ''FirstProduction''
LEFT JOIN UTRBDMSNET.dbo.Loc ON loc.ConstructKey = Construct.PKey AND Loc.LocType IN (''BH'',''TPI'')
LEFT JOIN UTRBDMSNET.dbo.LocExt ON LocExt.LocKey = Loc.PKey
LEFT JOIN UTRBDMSNET.dbo.RefCounty ON RefCounty.PKey = LocExt.County
WHERE
WellHistory.WorkType = ''ENTITY''
AND WellHistory.ModifyUser = ''UTAH\rachelmedina''
AND YEAR(WellHistory.ModifyDate) = @SearchYear
AND MONTH(WellHistory.ModifyDate) = @SearchMonth
GROUP BY
Well.WellID,
Construct.SideTrack,
Construct.Completion,
ConstructDate.EventDate,
Loc.LocType,
LocExt.Township,
LocExt.TownshipDir,
LocExt.Range,
LocExt.RangeDir,
LocExt.Sec,
RefCounty.CountyName,
tblAPDTracker.SpacingRule,
Lease.Number,
WellHistory.WHComments,
WellHistory.ModifyDate
) AS BasicQuery
PIVOT
(
MIN(BasicQuery.Location) FOR [Location Type] IN ([TPI], [BH])
) AS PivotedQuery
ORDER BY
API14,
[Well History Modify Date];
'
SELECT @query_attachment_filename = 'TestingEmailAttachment.csv';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'OilGasEmail',
@from_address = 'mhiebing@utah.gov',
@recipients = 'mhiebing@utah.gov ',
@body = @message,
@query = @query,
@query_attachment_filename = @query_attachment_filename,
@attach_query_result_as_file = 1,
@query_result_header = 1,
@query_result_width = 32767,
@query_result_separator = @tab,
@append_query_error = 0
--@query_result_no_padding = 1;
END
下面是我希望在电子邮件 CSV 中看到的几行结果。
| API14 | Entity Number | First Prod Date | Spacing Rule | Top Producing Interval Location | Bottom Hole Location | Well History Comments | Last Modified Date |
|---|---|---|---|---|---|---|---|
| 43013534820000 | 100260 | 2019-01-09 | 139-134 | Township 3S Range 1W Section 22 DUCHESNE County | Township 3S Range 1W Section 15 DUCHESNE County | Moved to CTB entity 100401 | 2020-07-15 17:27:00 |
| 43013534820000 | 100401 | 2019-01-09 | 139-134 | Township 3S Range 1W Section 22 DUCHESNE County | Township 3S Range 1W Section 15 DUCHESNE County | Moved to CTB entity 100401 | 2020-07-15 17:27:00 |
| 43013534860000 | 100246 | 2019-01-09 | 139-134 | Township 3S Range 1W Section 22 DUCHESNE County | Township 3S Range 1W Section 15 DUCHESNE County | Moved to CTB entity 100401. | 2020-07-15 17:28:00 |
【问题讨论】:
-
DECLARE "@"SearchYear AS VARCHAR(4) = 2020 DECLARE "@"SearchMonth AS VARCHAR(2) = 7 为什么要在字符串类型的变量中使用数字?
-
如果省略 3 部分命名,而只使用 2 部分命名,然后使用
@execute_query_database参数定义需要在其中运行查询的数据库,会发生什么情况? -
@SQLpro 最后,我希望这个 SP 在本月 1 日运行。我将用动态数字替换这两个变量,这些数字将调用当前年份的上个月。我尝试删除它们并将数字直接放入查询中,但没有任何区别。
-
那是 1 部分命名,@Hiebs915 ... 始终符合架构条件
-
@Hiebs915 这意味着始终为模式绑定对象指定模式名称。因此,请使用
dbo.tblAPDTracker而不仅仅是tblAPDTracker。您的查询已经这样做了,Larnu 只是指您询问您是否应该只使用tblAPDTracker而不是dbo.tblAPDTracker当您误解使用 2 部分命名(即dbo.tblAPDTracker)而不是 3 部分命名的原始建议时,其中包括数据库名称(例如UTRBDMSNET.dbo.Lease)。 Larnu 建议删除第一部分——数据库名称——然后通过@execute_query_database参数传递数据库名称。
标签: sql-server tsql dynamic-sql truncation dbmail