Good day guys I have one question here and hope this is possible.
I would like to know if it is possible to run a query that would switch to a different database.
I know that if select the command use db GO; db being the database I want to use would go to that database but in my case it is a little different.
Here is what I want to do.
1. restore a database
2. Find the name of the database
3. from the name I want my script to select that database and run some code.
I.e. Restore database test.
If I use the following query I can get the name of the recent database I just restored.
SELECT NAME FROM MASTER..SYSDATABASES WHERE CRDATE = (SELECT MAX(CRDATE) FROM MASTER..SYSDATABASES)
AND NAME <> 'TEMPDB'
I would like to use the result and create a command using the result get my code to execute to that database I just restored without having to manually change the database from management studio.
Is this possible?
I would like to know if it is possible to run a query that would switch to a different database.
I know that if select the command use db GO; db being the database I want to use would go to that database but in my case it is a little different.
Here is what I want to do.
1. restore a database
2. Find the name of the database
3. from the name I want my script to select that database and run some code.
I.e. Restore database test.
If I use the following query I can get the name of the recent database I just restored.
SELECT NAME FROM MASTER..SYSDATABASES WHERE CRDATE = (SELECT MAX(CRDATE) FROM MASTER..SYSDATABASES)
AND NAME <> 'TEMPDB'
I would like to use the result and create a command using the result get my code to execute to that database I just restored without having to manually change the database from management studio.
Is this possible?