Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 insert into with select statement

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-10-19 : 16:15:43
I have multiple dbs with same table structure,

how do i store dbname and no of record on that table in #table.


select [name] as DbName from sys.databases where [name] not in ('master','model','testdb')

insert into dbName.dob.tblName (Db_Name,No_Record) values (<databasename>,<no_of_record>)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-19 : 18:25:38
Just use the DB_NAME() function (I believe that's the name, not in front of BOL right now).

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-10-19 : 19:15:51
DB_name() always return same dbname,

e.g i have following dbs with same table structures
db01
db02
db03
db04
d05

i tried to instert the follwing script in loop,

insert into testdb.dbo.tblDbRecord (db_name,no_of_record) select db_name(),count(*) from <db_name_from_loop>.dbo.tbl01

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-10-19 : 20:21:29
Sorry, I misunderstood your question.

You'll need to use dynamic SQL or sp_msforeachdb to get the count(*) of your table. If you don't care about it being an accurate count then we can get it from the system tables all in one query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -