I want to set my @query field within a stored procedure to the following:
sp_msforeachdb 'select "?" AS databases, create_date from [?].sys.tables where DATEDIFF(day, create_date, GETDATE())<7'
My basic understanding of SQL may be way off, but can I do the following? (for sp_send_dbmail in case anyone is wondering) Or does this not work, since sp_msforeachdb is also a procedure itself?
@query = 'sp_msforeachdb 'select "?" AS databases, create_date from [?].sys.tables where DATEDIFF(day, create_date, GETDATE())<7''
If this is legal,how can I escape the single and double ticks within the string because I keep getting errors, such as Incorrect syntax near ''' or something simmilar. Thanks so much!