Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Cardinality Estimate and Computed ColumnsIs being a DBA still sexy? »
    comments

    Throughout the day, I probably write 5 to 15 short scripts to accomplish tasks that really, have no real purpose for future use.  That is, they aren't worth saving.  Typically this is because the script, like the one below, is easier to write than it is to search a folder to find.

    Just about the time I was going to hit delete I thought; why not post them on LTD's Wiki?

    Well, that is what I'm going to start attempting to do.  Don't get too excited.  There have been some good scripts that have gotten a lot of use, both from me and readers.  Scripts like the Orphaned fix script.  That script really came from the same situation.  I needed a quick script so I wrote it quickly.  Before throwing that one out, I realized it had some value given the steps that it resolves and the repeated need to fix orphaned users in databases.

    Another way to get them out there, blogging them.  On that note, today's short-throw-away script is grabbing the count of tables in a database and printing them out in SSMS with the table name.  This was for a quick reply in an email.  Hopefully it will help someone else out at some point.

    To add to all of this and the idea, I’m tagging anyone that reads this to do the same on your own blog.  All of those quick hacks in T-SQL or even C# and so on, post them up.  It’s a blog post, it’s sharing and it just might save someone a lot of time.

    Disclaimer: throw away scripts are just that. Untested and not run threw a ringer. Test them first :)

    1. set nocount on
    2. declare @int int = 1
    3. declare @count bigint
    4. declare @cmd nvarchar(1500)
    5. declare @tbl table (id int identity(1,1),name sysname)
    6. insert into @tbl select name from sys.tables where type = N'U'
    7.  
    8. while @int <= (Select count(*) from @tbl)
    9.  begin
    10.  
    11.    set @cmd = 'select @cnt=count(*) from ' + (select name from @tbl where id = @int)
    12.    execute sp_executesql @cmd, N'@cnt int OUTPUT', @cnt=@count OUTPUT
    13.    set @cmd = 'Table: ' + (select name from @tbl where id = @int) + ' has a row count of ' + CAST(@count AS VARCHAR(4000))
    14.    print @cmd
    15.   set @int += 1
    16.  end

    About the Author

    Ted Krueger is a SQL Server MVP and has been working in development and database administration for 13+ years. Specialties range from High Availability and Disaster / Recovery setup and testing methods down to custom assembly development for SQL Server Reporting Services. Ted blogs and is also one of the founders of LessThanDot.com technology community. Some of the articles focused on are Backup / Recovery, Security, SSIS and working on SQL Server and using all of the SQL Server features available to create stable and scalable database services. @onpnt Personal Blog over at http://onpnt.wordpress.com/
    Social SitingsTwitterLinkedInLTD RSS Feed
    3008 views
    InstapaperVote on HN

    6 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis Great idea..I will put up some stuff too as well

    For a quick count I just do a sp_spaceused instead because with big tables it might take a while to run

    Example

    SELECT 'exec sp_spaceused ''' + name + ''''
    FROM sys.tables
    where Type = 'U'


    That gives the following output and then I just paste that into a new query window and run it

    exec sp_spaceused 'Keys'
    exec sp_spaceused 'History'
    exec sp_spaceused 'DBUpgradeHistory'
    exec sp_spaceused 'ConfigurationInfo'
    exec sp_spaceused 'Catalog'
    exec sp_spaceused 'DataSets'
    exec sp_spaceused 'UpgradeInfo'
    exec sp_spaceused 'SubscriptionsBeingDeleted'
    exec sp_spaceused 'ModelDrill'
    exec sp_spaceused 'Segment'
    exec sp_spaceused 'ServerUpgradeHistory'
    exec sp_spaceused 'ChunkSegmentMapping'
    exec sp_spaceused 'ModelPerspective'
    exec sp_spaceused 'CachePolicy'
    exec sp_spaceused 'SegmentedChunk'
    exec sp_spaceused 'Users'
    exec sp_spaceused 'ExecutionLogStorage'
    exec sp_spaceused 'DataSource'
    exec sp_spaceused 'Policies'
    exec sp_spaceused 'ModelItemPolicy'
    exec sp_spaceused 'SecData'
    exec sp_spaceused 'Roles'
    exec sp_spaceused 'PolicyUserRole'
    exec sp_spaceused 'Event'
    exec sp_spaceused 'Subscriptions'
    exec sp_spaceused 'ActiveSubscriptions'
    exec sp_spaceused 'SnapshotData'
    exec sp_spaceused 'ChunkData'
    exec sp_spaceused 'Notifications'
    exec sp_spaceused 'Batch'
    exec sp_spaceused 'Schedule'
    exec sp_spaceused 'ReportSchedule'
    exec sp_spaceused 'RunningJobs'
    exec sp_spaceused 'ServerParametersInstance'
    11/15/12 @ 10:22
    Comment from: Tracy McKibben [Visitor] · http://www.realsqlguy.com
    Tracy McKibben There IS value in these scripts. I keep a shared Dropbox folder just for this purpose, shared with the rest of my DBA team. Everything from enabled CDC to changing the PK/clustering keys on a table (dropping all FK's and recreating). It's a great way to teach somebody how things are done, no matter how basic.
    11/15/12 @ 11:00
    Comment from: SDyckes [Member]
    SDyckes Because I have supported systems with unusual naming conventions or spaces in table names (SAP uses the @ in table naming, ie @Some_Table_Name, that are not table variables. Argh!), I adapted the script ever so slightly to accommodate that by adding the brackets around the table name. Here is the snippet I changed:

    set @cmd = 'select @cnt=count(*) from [' + (select name from @tbl where id = @int) + ']'
    11/15/12 @ 11:03
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky I have a record count of all tables in a database (few versions) in this blog post

    http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-to-get-information-about-all-databas
    11/15/12 @ 13:41
    Comment from: Frank Gill [Visitor] · http://skreebydba.com
    Frank Gill Ted, that is good advice. Lord knows I have a ton of them.
    11/15/12 @ 14:52
    Comment from: SQLDenis [Member] Email
    SQLDenis I added mine to the wiki page you started: List rowcounts for all Tables in Database
    11/15/12 @ 15:04

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)