SQL SERVER - Store Procedure

I have a SP in which i need to add one condition to check multiple databases by using sys.dm_database_encryption_keys table.

when encryption_state is 2 and percentage_completed is 0 in the results it should print the query i will provide.
John MillerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
So? Where are you stuck?

Have you tried something?
0
John MillerAuthor Commented:
I tried this

DECLARE @Test varchar(10)
SELECT @Test = Database_ID from
                             sys.dm_database_encryption_keys  
WHERE Encryption_state = 2 and complete_percentage = 0
If (@test not NULL)
Begin
PRINT("ALTER DB NAme")

If i have multiple DB's which meets the about. It should PRINT multiple ALTER statements with DB Names.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Mind that a variable can store only a single value so you'll never have this working for all databases.
Try this alternative solution:
SELECT 'ALTER DB ' + DB_NAME(Database_ID)
FROM sys.dm_database_encryption_keys  
WHERE Encryption_state = 2 and percent_complete = 0

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Nitin SontakkeDeveloperCommented:
Basically, I have no idea what you are up-to. The question doesn't shed much light on that. However, I can give you a technically correct answer.

You can do it as follows:

declare @databases nvarchar(max) = ''

SELECT @databases += N'ALTER DB ' + DB_NAME(Database_ID) + '
'
FROM sys.dm_database_encryption_keys  
WHERE Encryption_state = 2 and percent_complete = 0

select @databases

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
John, a feedback will be appreciated.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Recommendation to close this question by accepting the above comments as solutions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.