473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Loop through all databases

Hi,

I have to run a script that fetches some value (say Column1) from a particulat table (say Table 1) from all the databases in SQL Server.
Is there any way i can parameterize (USE 'DATABSENAME') Command.

Can I use something like

Declare @StudyName char(20)
Set @StudyName ='Master'
Use '@StudyName'

Please let me know any solution
Mar 17 '09 #1
1 3951
This is a code I found and modified that will give you all tables and all columns, I have used this as a building block to do many things;

a cursor sounds like what you will need if you want a result set from all of the databases at once.

Expand|Select|Wrap|Line Numbers
  1. SET NOCOUNT ON
  2.  
  3. DECLARE @Return int
  4. DECLARE @Retain int
  5. DECLARE @Status int
  6. DECLARE @PCIntra varchar(100)
  7. DECLARE @PCUltra bit 
  8.  
  9. set @PCIntra = '%'
  10. set @PCUltra = 1
  11.  
  12. SET @Status = 0
  13.  
  14. DECLARE @TPre varchar(10)
  15.  
  16. DECLARE @TDo3 tinyint
  17. DECLARE @TDo4 tinyint
  18.  
  19. SET @TPre = ''
  20.  
  21. SET @TDo3 = LEN(@TPre)
  22. SET @TDo4 = LEN(@TPre) + 1
  23.  
  24. CREATE TABLE #DBAH (TName varchar(100),
  25.                     CName varchar(100),
  26.                     CList smallint,
  27.                     CKind varchar(20),
  28.                     CSize int,
  29.                     CWide smallint,
  30.                     CMore smallint)
  31.  
  32. INSERT #DBAH
  33. SELECT O.name
  34.      , C.name
  35.      , C.colid
  36.      , T.name
  37.      , C.length
  38.      , C.prec
  39.      , C.scale
  40.   FROM sysobjects AS O
  41.   JOIN syscolumns AS C
  42.     ON O.id = C.id
  43.   JOIN systypes AS T
  44.     ON C.xusertype = T.xusertype
  45.  WHERE ISNULL(OBJECTPROPERTY(O.id,'IsMSShipped'),1) = 0
  46.    AND RTRIM(O.type) = 'U'
  47.    AND LEFT(O.name,@TDo3) = @TPre
  48.    AND O.name NOT LIKE 'adt%'
  49.    AND O.name NOT LIKE '%dtproper%'
  50.    AND O.name NOT LIKE 'dt[_]%'
  51.  
  52.       AND (@PCIntra IS NULL OR               C.name     LIKE @PCIntra)
  53.  
  54. SET @Retain = @@ERROR IF @Status = 0 SET @Status = @Retain
  55.  
  56. SELECT CASE WHEN @PCUltra = 0 THEN SUBSTRING(TName,@TDo4,100) ELSE TName END AS TName, CName, CList, CKind, CSize, CWide, CMore FROM #DBAH ORDER BY TName, CList
  57.  
  58. DROP TABLE #DBAH
  59.  
  60. SET NOCOUNT OFF
  61.  
  62. SELECT (@Status)
  63.  
  64. GO
Mar 17 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Garry Clarke | last post by:
Hi, I've written a job to export user and database permissions for all d/b's on a server. As you can see below, the T-SQL commands are the same for each d/b. Can anyone assist with regard to...
0
by: Cara | last post by:
LAST CALL FOR VOTES (of 2) unmoderated group comp.databases.etl Newsgroups line: comp.databases.etl Extraction, transformation, loading issues. Votes must be received by 23:59:59 UTC, 12 Dec...
0
by: Cara Altman | last post by:
REQUEST FOR DISCUSSION (RFD) unmoderated group comp.databases.etl This is a formal Request For Discussion (RFD) for the creation of a world-wide unmoderated Usenet newsgroup...
2
by: CLarkou | last post by:
Is there any other way of looping in the fields of records in a Datasheet form, except from the following one (moving to each record) for checking the field values ? For I = 1 To var_end_R...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
7
by: itfetish | last post by:
Ok heres my problem, I have a database which has a table in it that has all the staff members who are currently signed out of hte office. That has their staff ID, and the time they are out till...
2
by: LostDeveloper via AccessMonster.com | last post by:
Relationship goes Certification can have multiple variations (any single variation satifies the certification), which require multiple course groups which require any one of the products assigned...
1
by: aconti74 | last post by:
Hello I am new to vba programming/coding. I am writing a program that goes through a directory of text files and imports them into the database. The problem is eventually the database gets to big...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.