Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3937568
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:05:02+00:00 2026-05-20T00:05:02+00:00

I am trying to join two tables. One being the sys.databases table and the

  • 0

I am trying to join two tables. One being the sys.databases table and the other being a table defined in each database.

I am trying to gather the name of each database and the date it was created from the sys.databases table. [NAME] AND [CREATE_DATE]

The other column that I am trying to query on is from a different table called filesize, upon which I want to use the sum function to calculate the size of each database.

The problem that I am getting is that there is constant repetition. There should only be approximately 40 records to appear.

Here is my script:

SET NOCOUNT ON
DECLARE @DBNAME NVARCHAR(MAX)
DECLARE @DBNAME1 NVARCHAR(MAX)
DECLARE @varSQL NVARCHAR(MAX)

DROP TABLE TEMPIMGCOUNTERSERVER3 
CREATE TABLE TEMPIMGCOUNTERSERVER3 
(DBNAME NVARCHAR(MAX),
TOTAL_IMGSIZE_IN_MBS INT,
CREATE_DATE DATETIME)

DECLARE DBNAME CURSOR FAST_FORWARD FOR
select name from sys.databases where [NAME] LIKE 'Z%' AND create_date between GETDATE()-9 AND GETDATE()-3
ORDER BY [Name]

--SELECT * FROM SYS.DATABASES ORDER BY CREATE_DATE


OPEN DBNAME

FETCH NEXT FROM DBNAME INTO @DBname
WHILE (@@FETCH_STATUS=0)
BEGIN

--SET @DBNAME1=('USE ['+ @DBNAME +']')
--EXEC SP_EXECUTESQL  @DBNAME1

Set @varSQL='INSERT INTO TEMPIMGCOUNTERSERVER3 (DBNAME, TOTAL_IMGSIZE_IN_MBS, CREATE_DATE) 
SELECT ''['+@DBNAME+']'' AS DBNAME, SUM(FILESIZE/1048576) AS TOTAL_IMGSIZE_IN_MBS, CREATE_DATE
    FROM SYS.DATABASES, ['+@DBNAME+'].dbo.tbldoc WHERE CREATE_DATE BETWEEN GETDATE()-9 AND GETDATE()-3
    GROUP BY NAME, CREATE_DATE' 
EXEC SP_EXECUTESQL @varSQL

FETCH NEXT FROM DBNAME 
INTO @DBNAME
END

CLOSE DBNAME
DEALLOCATE DBNAME

INSERT TEMPIMGCOUNTERSERVER3 (DBNAME, TOTAL_IMGSIZE_IN_MBS)  
SELECT 'TOTAL_IMGSIZE_IN_MBS', SUM(TOTAL_IMGSIZE_IN_MBS) AS TOTAL_IMGSIZE_IN_MBS FROM TEMPIMGCOUNTERSERVER3 

SELECT DISTINCT DBNAME, SUM(TOTAL_IMGSIZE_IN_MBS) AS TOTAL_IMGSIZE_IN_MBS, convert(VARCHAR(10), CREATE_DATE, 101) AS CREATE_DATE FROM TEMPIMGCOUNTERSERVER3
GROUP BY DBNAME, convert(VARCHAR(10), CREATE_DATE, 101)
HAVING SUM(TOTAL_IMGSIZE_IN_MBS/1048576) IS NOT NULL
ORDER BY DBNAME
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:05:03+00:00Added an answer on May 20, 2026 at 12:05 am

    In your dynamic script you filter SYS.DATABASES on CREATE_DATE only.

    I think you should add filtering by NAME too, i.e. add to the WHERE clause something like this:

    '...AND NAME='''+@DBNAME+''' GROUP BY etc...'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to join two table (call them table1 and table2) but only return
I'm trying to LEFT JOIN two tables, to get a list of all rows
I'm trying to join two SQL tables, parent (which I have complete design control
I've been trying to perform a join on two tables in MySQL, and the
I have two tables one named Person , which contains columns ID and Name
I am doing a inner join between two tables where one is an association
I am trying to import data from a large database. I have two tables
Using extension syntax I'm trying to create a left-join using LINQ on two lists
I am trying to join together several audio files into one mp4/m4a file containing
I have a MySQL Left Join problem. I have three tables which I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.