I am trying to create a trigger that emails the results of a query on insert. The table requires me to create json_value columns and I am struggling to get the syntax correct in the query section of the executable.
This is how I would normally write a query for json_value columns:
SELECT
json_value(property_dict,'$.level_1.level_2.description')
FROM [database].[dbo].[table]
When writing the query as above in the executable I get "Incorrect syntax near '$.'.
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MSSQLAlert',
@recipients = 'email@domain.com',
@subject = 'Test Email',
@query = 'SELECT TOP 1 json_value(jsoncolumn,'$.level_1.level_2.description')
FROM [database].[dbo].[table]',
@attach_query_result_as_file = 0
If I do not include the quotes outside of the $ and description I get "Failed to initialize sqlcmd library with error number -2147467259."
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'MSSQLAlert',
@recipients = 'email@domain.com',
@subject = 'Test Email',
@query = 'SELECT TOP 1 json_value(jsoncolumn,$.level_1.level_2.description)
FROM [database].[dbo].[table]',
@attach_query_result_as_file = 0
Is what I am trying to do possible? Thanks for any help!