将我的存储过程查询转换为有效的T-SQL

时间:2016-07-25 12:31:06

标签: sql-server tsql

我有一个存储过程如下:

    ALTER PROCEDURE [dbo].[getReconciliationMarketMessageInCRM] --'02 jun 2014'

    @readDate datetime
AS

declare @newDate date

select a.New_MessageTypeCode as 'messageType', count(a.New_MessageTypeCode) as 'Received'
into #temp1
from New_marketmessagein as a
--where CreatedOn between '2016-7-18 04:00' and '2016-7-19 04:00'
where CreatedOn between @readDate + ' 04:00' and dateadd(day,1,@readDate) + ' 04:00'
group by a.New_MessageTypeCode

select  
sum(CASE WHEN #temp1.messageType = '014R' then #temp1.Received else 0 end) as '014R',
sum(CASE WHEN #temp1.messageType = '101' then #temp1.Received else 0 end) as '101',
sum(CASE WHEN #temp1.messageType = '101P' then #temp1.Received else 0 end) as '101P',
sum(CASE WHEN #temp1.messageType = '101R' then #temp1.Received else 0 end) as '101R',
sum(CASE WHEN #temp1.messageType = '102' then #temp1.Received else 0 end) as '102',
SUM(CASE WHEN #temp1.messageType = '102P' then #temp1.Received else 0 end) as '102P', 
SUM(CASE WHEN #temp1.messageType = '102R' then #temp1.Received else 0 end) as '102R',  
SUM(CASE WHEN #temp1.messageType = '105' then #temp1.Received else 0 end) as '105',
SUM(CASE WHEN #temp1.messageType = '105L' then #temp1.Received else 0 end) as '105L', 
SUM(CASE WHEN #temp1.messageType = '106D' then #temp1.Received else 0 end) as '106D', 
SUM(CASE WHEN #temp1.messageType = '106E' then #temp1.Received else 0 end) as '106E', 
SUM(CASE WHEN #temp1.messageType = '110' then #temp1.Received else 0 end) as '110',  
SUM(CASE WHEN #temp1.messageType = '111' then #temp1.Received else 0 end) as '111',
SUM(CASE WHEN #temp1.messageType = '111A' then #temp1.Received else 0 end) as '111A', 
SUM(CASE WHEN #temp1.messageType = '111L' then #temp1.Received else 0 end) as '111L', 
SUM(CASE WHEN #temp1.messageType = '111R' then #temp1.Received else 0 end) as '111R',
SUM(CASE WHEN #temp1.messageType = '112' then #temp1.Received else 0 end) as '112',  
SUM(CASE WHEN #temp1.messageType = '112R' then #temp1.Received else 0 end) as '112R',
SUM(CASE WHEN #temp1.messageType = '112W' then #temp1.Received else 0 end) as '112W', 
SUM(CASE WHEN #temp1.messageType = '114' then #temp1.Received else 0 end) as '114', 
SUM(CASE WHEN #temp1.messageType = '115' then #temp1.Received else 0 end) as '115',
SUM(CASE WHEN #temp1.messageType = '115R' then #temp1.Received else 0 end) as '115R',  
SUM(CASE WHEN #temp1.messageType = '116' then #temp1.Received else 0 end) as '116',
SUM(CASE WHEN #temp1.messageType = '116A' then #temp1.Received else 0 end) as '116A', 
SUM(CASE WHEN #temp1.messageType = '116N' then #temp1.Received else 0 end) as '116N', 
SUM(CASE WHEN #temp1.messageType = '116R' then #temp1.Received else 0 end) as '116R',
SUM(CASE WHEN #temp1.messageType = '117D' then #temp1.Received else 0 end) as '117D',  
SUM(CASE WHEN #temp1.messageType = '117R' then #temp1.Received else 0 end) as '117R',
SUM(CASE WHEN #temp1.messageType = '122' then #temp1.Received else 0 end) as '122', 
SUM(CASE WHEN #temp1.messageType = '122R' then #temp1.Received else 0 end) as '122R', 
SUM(CASE WHEN #temp1.messageType = '130D' then #temp1.Received else 0 end) as '130D',
SUM(CASE WHEN #temp1.messageType = '130R' then #temp1.Received else 0 end) as '130R',
SUM(CASE WHEN #temp1.messageType = '131' then #temp1.Received else 0 end) as '131',
SUM(CASE WHEN #temp1.messageType = '137R' then #temp1.Received else 0 end) as '137R', 
SUM(CASE WHEN #temp1.messageType = '261' then #temp1.Received else 0 end) as '261', 
SUM(CASE WHEN #temp1.messageType = '300' then #temp1.Received else 0 end) as '300', 
SUM(CASE WHEN #temp1.messageType = '300S' then #temp1.Received else 0 end) as '300S',
SUM(CASE WHEN #temp1.messageType = '300W' then #temp1.Received else 0 end) as '300W',  
SUM(CASE WHEN #temp1.messageType = '301' then #temp1.Received else 0 end) as '301',
SUM(CASE WHEN #temp1.messageType = '301N' then #temp1.Received else 0 end) as '301N', 
SUM(CASE WHEN #temp1.messageType = '303R' then #temp1.Received else 0 end) as '303R', 
SUM(CASE WHEN #temp1.messageType = '305' then #temp1.Received else 0 end) as '305',
SUM(CASE WHEN #temp1.messageType = '306' then #temp1.Received else 0 end) as '306',  
SUM(CASE WHEN #temp1.messageType = '306W' then #temp1.Received else 0 end) as '306W',
SUM(CASE WHEN #temp1.messageType = '307' then #temp1.Received else 0 end) as '307', 
SUM(CASE WHEN #temp1.messageType = '307W' then #temp1.Received else 0 end) as '307W', 
SUM(CASE WHEN #temp1.messageType = '308' then #temp1.Received else 0 end) as '308',
SUM(CASE WHEN #temp1.messageType = '310' then #temp1.Received else 0 end) as '310',
SUM(CASE WHEN #temp1.messageType = '310W' then #temp1.Received else 0 end) as '310W', 
SUM(CASE WHEN #temp1.messageType = '311' then #temp1.Received else 0 end) as '311', 
SUM(CASE WHEN #temp1.messageType = '320' then #temp1.Received else 0 end) as '320',
SUM(CASE WHEN #temp1.messageType = '320W' then #temp1.Received else 0 end) as '320W',
SUM(CASE WHEN #temp1.messageType = '321' then #temp1.Received else 0 end) as '321', 
SUM(CASE WHEN #temp1.messageType = '330' then #temp1.Received else 0 end) as '330', 
SUM(CASE WHEN #temp1.messageType = '331' then #temp1.Received else 0 end) as '331',
SUM(CASE WHEN #temp1.messageType = '332' then #temp1.Received else 0 end) as '332',
SUM(CASE WHEN #temp1.messageType = '332W' then #temp1.Received else 0 end) as '332W', 
SUM(CASE WHEN #temp1.messageType = '341' then #temp1.Received else 0 end) as '341', 
SUM(CASE WHEN #temp1.messageType = '342' then #temp1.Received else 0 end) as '342',
SUM(CASE WHEN #temp1.messageType = '352R' then #temp1.Received else 0 end) as '352R', 
SUM(CASE WHEN #temp1.messageType = '591' then #temp1.Received else 0 end) as '591',
SUM(CASE WHEN #temp1.messageType = '594' then #temp1.Received else 0 end) as '594',
SUM(CASE WHEN #temp1.messageType = '595' then #temp1.Received else 0 end) as '595', 
SUM(CASE WHEN #temp1.messageType = '596' then #temp1.Received else 0 end) as '596', 
SUM(CASE WHEN #temp1.messageType = '597' then #temp1.Received else 0 end) as '597',
SUM(CASE WHEN #temp1.messageType = '598' then #temp1.Received else 0 end) as '598',
SUM(CASE WHEN #temp1.messageType = '601' then #temp1.Received else 0 end) as '601', 
SUM(CASE WHEN #temp1.messageType = '602' then #temp1.Received else 0 end) as '602', 
SUM(CASE WHEN #temp1.messageType = '700' then #temp1.Received else 0 end) as '700',
SUM(CASE WHEN #temp1.messageType = '700W' then #temp1.Received else 0 end) as '700W',
SUM(CASE WHEN #temp1.messageType = '701' then #temp1.Received else 0 end) as '701', 
SUM(CASE WHEN #temp1.messageType = '701W' then #temp1.Received else 0 end) as '701W'
from #temp1

在答案的帮助下,我能够将其缩短如下:

SELECT *
FROM (
select a.New_MessageTypeCode
from New_marketmessagein as a
WHERE CreatedOn between '2016-7-20 04:00' and '2016-7-21 04:00'
) AS SRC
PIVOT (COUNT(New_MessageTypeCode ) FOR New_MessageTypeCode IN ([014R], [101], [101P], [101R], [102], [102P], [102R], [105],
[105L], [106D], [106E], [110], [111], [111A], [111L], [111R], [112], [112R], [112W], [114], [115], [115R], [116], [116A],
[116N], [116R],[117D],[117R], [122], [122R], [130D], [130R], [131], [137R], [261], [300], [300S], [300W], [301], [301N],
[303R], [305], [306], [306W], [307], [307W], [308], [310], [310W], [311], [320], [320W], [321], [330], [331], [332], [332W],
[341],[342], [352R], [591], [594], [595], [596], [597], [598], [601], [602], [700], [700W], [701], [701W])) As test

我现在正在尝试使用transact SQL设置SQL服务器作业,该SQL每天都会在附加的csv文件中通过电子邮件发送,如下所示:

DECLARE @query_attachment_filename VARCHAR(100) = 'Reconciliation-Count-Ending-' + CONVERT(VARCHAR(10), GETDATE(), 112) + '.csv';

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'support',
@recipients = 'johnpaul@energy.co.uk;martin@energy.co.uk',
@subject = 'Reconciliation Count',
@query = N'SET ANSI_WARNINGS OFF;SET NOCOUNT ON;SELECT *
FROM (
    select a.New_MessageTypeCode
    from New_marketmessagein as a
    WHERE CreatedOn between DATEADD(DAY, –1, GETDATE()) and GETDATE()
) AS SRC
PIVOT (COUNT(New_MessageTypeCode ) FOR New_MessageTypeCode IN ([014R], [101], [101P], [101R], [102], [102P], [102R], [105],
[105L], [106D], [106E], [110], [111], [111A], [111L], [111R], [112], [112R], [112W], [114], [115], [115R], [116], [116A],
[116N], [116R],[117D],[117R], [122], [122R], [130D], [130R], [131], [137R], [261], [300], [300S], [300W], [301], [301N],
[303R], [305], [306], [306W], [307], [307W], [308], [310], [310W], [311], [320], [320W], [321], [330], [331], [332], [332W],
[341],[342], [352R], [591], [594], [595], [596], [597], [598], [601], [602], [700], [700W], [701], [701W])) As test ;',
@attach_query_result_as_file = 1,
@query_attachment_filename = @query_attachment_filename,
@query_result_header =0,
@query_result_separator = ' '

虽然我已经替换了以前工作的作业的查询部分,但由于格式化查询错误导致错误,我在这里遗漏了什么吗?

1 个答案:

答案 0 :(得分:1)

使用pivot和避免临时表可以简化您的查询。尝试这样的事情:

SELECT *
FROM (
    select a.New_MessageTypeCode
    from New_marketmessagein as a
    WHERE CreatedOn between @readDate + ' 04:00' and dateadd(day,1,@readDate) + ' 04:00'
) AS SRC
PIVOT (COUNT(New_MessageTypeCode ) FOR New_MessageTypeCode IN (
    [014R], [101], [101P] -- etc...
) AS PVT

编辑:要更新您的更新问题:您提供的查询未在当前上下文中执行,您可能需要在引用表时添加到数据库名称。要进一步排除故障,请发送邮件以查看其正在使用的上下文。

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'support',
@recipients = '...',
@subject = 'TEST',
@query = N'SELECT SYSTEM_USER, DB_NAME()'