无法初始化错误编号为-2147467259的sqlcmd库。当我删除计数并按查询分组时,效果很好。我通过上面的错误添加计数和分组。
'''
DECLARE @sub VARCHAR(100);
DECLARE @qry VARCHAR(1000);
DECLARE @msg VARCHAR(250);
DECLARE @query_ath NVARCHAR(1000);
DECLARE @query_attachment_filename NVARCHAR(1000);
DECLARE @tab char(1) = CHAR(9);
SELECT @sub = 'Daily Absence Report';
SELECT @msg = 'Please refer to the attached spread sheet for the report.'
SELECT @query_ath = 'select a.SCS_STUDENT,
concat(d.FIRST_NAME,'' '',d.LAST_NAME) as Student_Name,
c.STA_OTHER_COHORT_GROUPS,
b.STC_COURSE_NAME,
b.STUDENT_ACAD_CRED_ID,
e.SCS_ABSENT_DATES,
count(case e.SCS_ATTENDANCE_TYPES when ''A'' then 1 end) as
Number_of_Absences
from STUDENT_COURSE_SEC a, STUDENT_ACAD_CRED b, STA_OTHER_COHORTS c, PERSON
d, SCS_ATTENDANCE e
where a.SCS_STUDENT_ACAD_CRED = b.STUDENT_ACAD_CRED_ID
and b.STC_REPORTING_TERM = ''2019FA''
and b.STC_PERSON_ID = left(c.STUDENT_ACAD_LEVELS_ID, 7)
and b.STC_PERSON_ID = d.ID
and c.STA_OTHER_COHORT_GROUPS like ''AT%''
and c.STA_OTHER_COHORT_GROUPS <> ''ATCHR''
and a.STUDENT_COURSE_SEC_ID = e.STUDENT_COURSE_SEC_ID
and e.SCS_ATTENDANCE_TYPES = ''A''
and CONVERT(date, getdate()) = e.SCS_ABSENT_DATES
group by a.SCS_STUDENT,
concat(d.FIRST_NAME,'' '',d.LAST_NAME),
c.STA_OTHER_COHORT_GROUPS,
b.STC_COURSE_NAME,
b.STUDENT_ACAD_CRED_ID,
e.SCS_ABSENT_DATES';
SELECT @query_attachment_filename = 'ath_absences.csv';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'elearning no shows',
@recipients = 'email', --'ajenkins@eastms.edu',
@copy_recipients = 'rboles@eastms.edu',
@body = @msg,
@subject = @sub,
@execute_query_database = 'db_server',
@query = @query_ath,
@query_attachment_filename = 'ath_absences.csv',
@attach_query_result_as_file = 1,
@query_result_header = 1,
@query_result_width = 256,
@query_result_separator=@tab,
@query_result_no_padding = 1;
'''
答案 0 :(得分:0)
根据我们在评论中的讨论,请增加@query_ath的字符数。