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?
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?
John MillerAuthor Commented:
I tried this

DECLARE @Test varchar(10)
SELECT @Test = Database_ID from
WHERE Encryption_state = 2 and complete_percentage = 0
If (@test not NULL)

If i have multiple DB's which meets the about. It should PRINT multiple ALTER statements with DB Names.
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:
FROM sys.dm_database_encryption_keys  
WHERE Encryption_state = 2 and percent_complete = 0

Open in new window

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
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

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

Vitor MontalvãoMSSQL Senior EngineerCommented:
John, a feedback will be appreciated.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Recommendation to close this question by accepting the above comments as solutions.
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.