August 20, 2021 at 11:25 am
USE [DatabaseA]
GO
CREATE DATABASE AUDIT SPECIFICATION [DatabaseAuditSpecification-DatabaseA]
FOR SERVER AUDIT [ServerAuditName]
ADD (INSERT ON DATABASE::[DatabaseA] BY [dbo]),
ADD (SELECT ON DATABASE::[DatabaseA] BY [dbo]),
ADD (DELETE ON DATABASE::[DatabaseA] BY [dbo])
WITH (STATE = OFF)
GO
I have 100+ databases and would like to generate a script which I can execute in one go instead of running this a 100 times by changing database names in the script every time I create a database audit specification for each database. Advise is appreciated?
USE [DatabaseA]
GO
CREATE DATABASE AUDIT SPECIFICATION [DatabaseAuditSpecification-DatabaseA]
FOR SERVER AUDIT [ServerAuditName]
ADD (INSERT ON DATABASE::[DatabaseA] BY [dbo]),
ADD (SELECT ON DATABASE::[DatabaseA] BY [dbo]),
ADD (DELETE ON DATABASE::[DatabaseA] BY [dbo])
WITH (STATE = OFF)
GO
I have 100+ databases and would like to generate a script which I can execute in one go instead of running this a 100 times by changing database names in the script every time I create a database audit specification for each database.
August 20, 2021 at 11:36 am
How about
/* generate DDL */
exec sp_msforeachdb '
Select ''
USE [?]
GO
CREATE DATABASE AUDIT SPECIFICATION [DatabaseAuditSpecification-DatabaseA]
FOR SERVER AUDIT [ServerAuditName]
ADD (INSERT ON DATABASE::[?] BY [dbo]),
ADD (SELECT ON DATABASE::[?] BY [dbo]),
ADD (DELETE ON DATABASE::[?] BY [dbo])
WITH (STATE = OFF)
GO
''
'
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
August 21, 2021 at 4:00 am
Thanks @Johan.
I settled with this...
CREATE TABLE #Results (
abc VARCHAR(MAX)
)
INSERT INTO #Results
exec sp_msforeachdb '
Select ''
USE [?]
GO
CREATE DATABASE AUDIT SPECIFICATION [DatabaseAuditSpecification ?]
FOR SERVER AUDIT [ServerAuditName]
ADD (SELECT ON DATABASE::[?] BY [dbo]),
ADD (INSERT ON DATABASE::[?] BY [dbo]),
ADD (DELETE ON DATABASE::[?] BY [dbo])
WITH (STATE = OFF)
GO
''
'
select * from #Results
August 24, 2021 at 6:37 pm
I also have the same case, thanks for this solution!
Uh-huh... Sounds like a precursor to spam.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy