2015-07-03 58 views
1

我目前有一個SQL作業,它執行特定軟件的所有用戶列表。SQL電子郵件作業修改後失敗

但是,我現在想要將名字和姓氏放在一起來創建一個完整的名稱。我創建了簡單的SQL來處理這樣的要求,但是當我試圖把它列入到SQL作業失敗:

故障原因:

[SQLSTATE 42000] (Error 102). The step failed. 

SQL作業多數民衆贊成失敗:

EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'test=email', 
    @recipients = '[email protected]', 
    @subject = 'Users Report', 
@body = '<b> Please follow the correct procedure (see other email) 
@body_format = 'HTML',  
    @query = 'SELECT [testDB].[dbo].[base_user].buser_userid AS [N4 User ID] 
      , [testDB].[dbo].[base_user].buser_firstName + ' ' + 
      [testDB].[dbo].[base_user].buser_lastName as [Users Name] 
      ,[testDB].[dbo].[sec_role].role_name as [Role] 
      ,[testDB].[dbo].[sec_role].role_description as [Description] 
      ,[testDB].[dbo].[base_user].buser_active AS [Active?] 
FROM  [testDB].[dbo].base_user 
INNER JOIN [testDB].[dbo].[sec_user_role_map] ON testDB.dbo.base_user.[buser_gkey] = testDB.dbo.[sec_user_role_map].[usrrolm_user_gkey] 
INNER JOIN [testDB].[dbo].[sec_role] ON testDB.dbo.[sec_user_role_map].[usrrolm_role_gkey] = testDB.[dbo].[sec_role].role_gkey 

', 
    @attach_query_result_as_file = 1; 

回答

0

您需要跳過CONCAT上的單引號

此外,您錯過了該行末尾的一個:@body

EXEC msdb.dbo.sp_send_dbmail 
    @profile_name = 'test=email', 
    @recipients = '[email protected]', 
    @subject = 'Users Report', 
@body = '<b> Please follow the correct procedure (see other email)', 
@body_format = 'HTML',  
    @query = 'SELECT [testDB].[dbo].[base_user].buser_userid AS [N4 User ID] 
      , [testDB].[dbo].[base_user].buser_firstName + '' '' + 
      [testDB].[dbo].[base_user].buser_lastName as [Users Name] 
      ,[testDB].[dbo].[sec_role].role_name as [Role] 
      ,[testDB].[dbo].[sec_role].role_description as [Description] 
      ,[testDB].[dbo].[base_user].buser_active AS [Active?] 
FROM  [testDB].[dbo].base_user 
INNER JOIN [testDB].[dbo].[sec_user_role_map] ON testDB.dbo.base_user.[buser_gkey] = testDB.dbo.[sec_user_role_map].[usrrolm_user_gkey] 
INNER JOIN [testDB].[dbo].[sec_role] ON testDB.dbo.[sec_user_role_map].[usrrolm_role_gkey] = testDB.[dbo].[sec_role].role_gkey 

', 
    @attach_query_result_as_file = 1; 
+1

謝謝馬特,那真的有幫助。你確定知道你的東西! – PAPERCLIPSANDPAPER