Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am pretty new to SQL server. I have done coding but not database programming.

My question is: When users create a project in their application, it creates one database on SQL server. These databases have same table structure. In sys.databases, the database_id and database name gets created.

We created a view where we put the database ID and table structure and then union all to get data from a particular table from all the databases.

Currently we are hardcoding the database id which is coming from sys.databases in our view.

My question is: how do I automate this?

So when the new database gets created, my query can put that database id and the table structure automatically.

One of my colleague suggested:

(1)Create a trigger on the sys.databases table, so that when a new row is inserted, do a select for the new inserted row, find out the database_id.
(2)Used the database_id that was found out in (1), to add a new UNION in all of the views of concerns.

I don't know how to do that...any suggestions?

Thanks,
Ritesh

What I have tried:

I have to see how triggers work. As I said I am pretty new to database programming.
Posted
Updated 6-Dec-16 10:10am
v3
Comments
PIEBALDconsult 1-Dec-16 19:35pm    
Please do not ever add anything to a database that does not belong to you.
Member 12882034 1-Dec-16 19:42pm    
I am not. All I want is: Whenever new database gets added, I want to make sure my code knows about it so I can add database Id and database name in my view.

1 solution

Your code can run the following script to get all the databases on your server.

SQL
select dbid, name from master.dbo.sysdatabases


Just put it in a refresh button click_event.

If you are doing this in a view on the database,

Since this is just like any other table, just join it to whatever you need to.


Good Luck
Brent
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900