4

I'm pretty new to SQL, but given the below example is there a way to condense the query? From my understanding SQL does not have arrays, but is it possible to store the tables somehow and do one SELECT statement, as maybe a variable (Select Count(*) From Table_Name) As Table_Name)?

SELECT
(SELECT COUNT(*) FROM ActivityLog) AS ActivityLog,
(SELECT COUNT(*) FROM ActivityLogin) AS ActivityLogin,
(SELECT COUNT(*) FROM ActivityPlayContent) AS ActivityPlayContent,
(SELECT COUNT(*) FROM ChangeLog) AS ChangeLog,
(SELECT COUNT(*) FROM ContentApprovalLog) AS ContentApprovalLog,
(SELECT COUNT(*) FROM ExceptionLog) AS ExceptionLog,
(SELECT COUNT(*) FROM State) AS State
2
  • You could define a stored procedure which dynamically constructs and then executes a SQL string query like the one you have above, but this is pretty much how SQL works. Dynamically constructing it as an SP is something akin to programming (with a language well not built for it) Commented Jun 9, 2014 at 15:38
  • Check this link, there are different ways to generate count for tables, you can pick one and modify it and write a function/stored procedure to work for the tables you want counts for. Commented Jun 9, 2014 at 15:46

1 Answer 1

2

If you need this query often and want to have it simpler, you can create a VIEW. You then have to query the view like you query a table.

SELECT * FROM MyView

Views are especially helpful when you have multiple complex queries and you want to reuse them in other queries (f.e. joining two views together or subquerying a view).

Create the view for your query as follows

CREATE VIEW MyView AS
SELECT
(SELECT COUNT(*) FROM ActivityLog) AS ActivityLog,
(SELECT COUNT(*) FROM ActivityLogin) AS ActivityLogin,
(SELECT COUNT(*) FROM ActivityPlayContent) AS ActivityPlayContent,
(SELECT COUNT(*) FROM ChangeLog) AS ChangeLog,
(SELECT COUNT(*) FROM ContentApprovalLog) AS ContentApprovalLog,
(SELECT COUNT(*) FROM ExceptionLog) AS ExceptionLog,
(SELECT COUNT(*) FROM State) AS State
Sign up to request clarification or add additional context in comments.

5 Comments

A good point, well made, but it doesn't really answer the question?
I agree that stored procedures would address this more directly, but I don't want to teach a SQL beginner to put everything in SP as this wouldn't be good. Given that his intention is to make the query more condensed, a view solves this problem in a smarter way.
The OP's question is asking how to write this without having to supply specific table names. In other words, he probably wants to write something dynamic based on sys.objects - as highlighted in the link in the comment from @rs.
I started researching into stored procedures and it seems like it would be similar to a C# method that can be called with parameters. I have to dive in a bit more.
Ok, I setup both a view and stored procedure. The stored procedure was a little tricky and it's not 100 percent yet, but I think I got it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.