0
 SELECT
 (
     SELECT SUM(ISNULL(Volume,0))
       FROM Order_【a1.Login】
      WHERE Login = a1.Login
 ) AS SelfVolume
 FROM dbo.Account a1

I want the table name in the sub-select (【a1.Login】) to match the value a1.Login from the outer select statement (field Login of table Account). How can I get this result?

2
  • 2
    Sounds like you've got a really bad database schema if you store the same type of information in different tables for different accounts? Commented Dec 8, 2016 at 10:18
  • I also think this is a bad database schema, but now there is such a demand must be done, mssql statement there is no way to achieve like this function? I tried to use function, procduce, but I still can not afford to solve this problem Commented Dec 9, 2016 at 2:26

1 Answer 1

11

The technical answer is: By using dynamic SQL. It's complicated, error-prone and potentially dangerous (beware of Bobby Tables). Your SQLs will become unreadable and unmaintainable. You are entering a world of pain.

The correct answer is: You don't. Don't create a separate Orders table for every user. Create one Orders table with a foreign key to your Account table.


If you still want to go ahead and work with this broken database design (remember: You are entering a world of pain, and you are just getting started), you will somehow need to construct the following SQL dynamically:

 SELECT SUM(ISNULL(Login_Volume,0)) FROM
 (
     SELECT SUM(ISNULL(Volume,0)) AS Login_Volume FROM Order_SomeUser WHERE Login = 'SomeUser'
     UNION ALL
     SELECT SUM(ISNULL(Volume,0)) AS Login_Volume FROM Order_SomeOtherUser WHERE Login = 'SomeOtherUser'
     UNION ALL
     ...
 ) AS AllSums

You can do that in the language of your choice, either in your target language (C#, Java, PHP, etc.), which is probably the easiest and most maintainable solution, or directly in T-SQL, by using T-SQL cursors and loops (= the hard way). Whichever language you choose, the algorithm is straight-forward:

  • Loop through your Account table and get the Logins.
    • Sanitize the value and validate that the corresponding Order_ table exists.
    • Create one SQL statement for each account.
    • Join them with UNION ALL.
  • Wrap them in the outer SELECT as shown above.

Again: If there is any chance of fixing your broken DB design instead, do that, it will pay off in the long run.

Sign up to request clarification or add additional context in comments.

2 Comments

I also think this is a bad database schema, but now there is such a demand must be done, mssql statement there is no way to achieve like this function? I tried to use function, procduce, but I still can not afford to solve this problem
thank you for your answer sincerely. As you said, now only through the use of the cursor, CTE to achieve the results I want.

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.