How do I find out which stored procedure sent an email? - sql-server

We have an email that is being sent to us from SQL Server that we want to stop. How do I find out which stored procedure is sending that email so I can modify/disable it?
What I've tried
Searching through all stored procedures via sys.all_sql_modules for words in the email subject and body text.
SELECT *
FROM sys.all_sql_modules AS asm
INNER JOIN sys.objects AS o ON o.object_id = asm.object_id
WHERE asm.definition LIKE '%*[subject/body words]*%'
I've looked through the sysmail_* tables and I have found my email being sent but I haven't found a way to trace it back to a stored procedure.
SELECT TOP 100 *
FROM msdb.dbo.sysmail_sentitems AS ss
WHERE ss.subject LIKE '%keyword%'

you could search sys.modules for the string
sp_send_dbmail
Then look at all the hits. OTOH how do you know it was sent from a PROC at all? It could be an ad-hoc query calling sp_send_dbmail, or an agent job, or something else.

I would simply create a server side trace and with filter for textdata like '%sp_send_dbmail%'
Then just run the trace for 1 day, and then you can figure out which SP is sending out db mail. This way, you can surely find out whether the email is sent via a stored proc or via an ad-hoc query (like inside a SSRS report?) or from a job step.

Related

How to audit who ran query on abcd database im Snowflake?

Have abcd database. one user has executed query on abcd database. Another user has executed another query on abcd database. Like this whenever different user has executed different user has executed query on abcd database. Need to capture user execution time and who has executed query..etc
This information is gathered from https://docs.snowflake.com/en/user-guide/access-history.html which is basically the access history details. Also, the same detail can be checked from Snowflake - Query History - Filters where you can use username or any other parameter to look up the details.
You can use Query_history view available in Snowflake.ACCOUNT_USAGE schema to get the complete information of the query within the last 365 days including user_name, execution time, etc
Please note Latency for the view may be up to 45 minutes.
https://docs.snowflake.com/en/sql-reference/account-usage/query_history.html#query-history-view
Also, you can use QUERY_HISTORY function available in information_schema to retrieve query information within the last 7 days and with no latency.
Please review the below documentation for more information.
https://docs.snowflake.com/en/sql-reference/functions/query_history.html#query-history-query-history-by
https://docs.snowflake.com/en/sql-reference/account-usage.html#differences-between-account-usage-and-information-schema
You can use below query(you can add the columns as you need):
select distinct QH.query_id, QH.USER_NAME,qh.database_name,Qh.start_time, qh.EXECUTION_TIME from
"SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY" QH
where
-- QH.query_id='' --If you know the query id,use it here
-- QH.user_name='USERNAME' -- You can filter by user id
QH.database_name='DBNAME' --you can filter by databasename
and qh.start_time > '2022-06-29 12:45:36.291'-- you can filter by date
;
If you want to track the IP address and application from where query was run, you can use below query as well:
select distinct QH.query_id,LH.client_ip, QH.USER_NAME,s.client_application_id,qh.database_name,Qh.start_time, qh.EXECUTION_TIME from snowflake.account_usage.login_history LH
inner join "SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY" QH
on QH.USER_NAME=LH.user_name
inner join "SNOWFLAKE"."ACCOUNT_USAGE"."SESSIONS" S on S.session_id=QH.session_id
and s.LOGIN_EVENT_ID=lh.EVENT_ID
where
-- QH.query_id='' --If you know the query id,use it here
-- QH.user_name='USERNAME' --If you know the user id,use it here
QH.database_name='DBNAME' --If you know the DB id,use it here
and qh.start_time > '2022-06-29 12:45:36.291'-- filter by date as required
;

How to restrict a SQL Server user to execute some stored procedures only?

The goal is to have a user who can execute some stored procedures and nothing else. For example I don't want to allow the user to query SELECT GETDATE() or SELECT 10 + 20 or anything else.
My main final goal is to prevent a malicious user to waste the power of server's CPU by calling a query in a loop. As I know this is somehow preventable in the code of stored procedures, for example by using a delay(WAITFOR) inside it but not for a query like SELECT 10 + 20.
How is this possible by SQL Server 2012?
Let me ask a better question:
Is there any way to limit the default "Public" user to execute only specified stored procedures and not query something like SELECT 10 + 20 ?

SQL Server send email when query result is not empty

I need to send an email from SQL Server when a query result set holds records. The query can be based on a lot of logic with joins between several tables.
Please send me in the right direction (views, triggers on views, SQL Server agent job..?).
using sp_send_DBmail as documented here(https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql?view=sql-server-2017) for all parameter options
declare #bodytext varchar(max)= '<b>Hey look I wrote something</b>'
if(Exists(select 1 from ....))
begin
EXEC msdb.dbo.sp_send_dbmail
#recipients='xyz#gmail.com',
#subject='ATTN! There are records',
#body=#bodytext,
#body_format='HTML',
#from_address='DBA <kl#domain.com>',
#reply_to='xyz#gmail.com'
end
take a look at Vsql-email app (you can search on google, I think I'm not allowed to post the direct link here), it has this option to not send the email if the query has 0 rows, and you can send the email as HTML formatted body and/or as excel attachment and no need to enable and configure Database Mail in SQL Server and write code for HTML formatting.

How to send email from execute sql task?

How can i send email if execute sql task get executed and loads a table. so, if table is loaded with any record send email, if not loaded no email.
Appreciate any help.
after that task add another task that checks if there are any rows in the table
something like
IF EXISTS (SELECT 1 FROM Yourtable)
BEGIN
EXEC msdb.dbo.sp_send_dbmail ---------
END
read up on sp_send_dbmail here: http://msdn.microsoft.com/en-us/library/ms190307.aspx
its cool i solved it. i used two execute sql tasks and first one for loading data into the table, second one counting the records and i put variable on green arrow #MyVariable > 0 and connected the send mail task.
Thanks to all.

Query to return internal details about stored function in SQL Server database

I have been given access to a SQL Server database that is currently used by 3rd party app. As such, I don't have any documentation on how that application stores the data or how it retrieves it.
I can figure a few things out based on the names of various tables and the parameters that the user-defined functions takes and returns, but I'm still getting errors at every other turn.
I was thinking that it would be really helpful if I could see what the stored functions were doing with the parameters given to return the output. Right now all I've been able to figure out is how to query for the input parameters and the output columns.
Is there any built-in information_schema table that will expose what the function is doing between input and output?
If you can execute a query against your database somehow, and if you have the necessary permissions to read the system catalog views, then you could run this query to get the name, the definition (SQL code) and a few more bits of information about your functions:
SELECT
obj.name ,
obj.type ,
obj.type_desc ,
obj.create_date ,
obj.modify_date ,
m.definition ,
m.is_schema_bound
FROM
sys.objects obj
INNER JOIN
sys.sql_modules m ON obj.object_id = m.object_id
WHERE
obj.type IN ('AF', 'FN', 'FS', 'FT', 'IF', 'TF')
Provided you have appropriate permissions, you can simply script out all Stored Procedures and Functions:
Right-click on your database in SSMS (SQL Server Management Studio), select Tasks –> Generate Scripts, ensure your database is highlighted and click next. Ensure the options to script out Stored Procedures and Functions are selected.
You can install SSMS (client Tools) without requiring a SQL Server license.
Another way is sp_helptext which will show you the source of the passed SP or UDF;
sp_helptext fnBlaDeBla

Resources