SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with sql statement


Help with sql statement

Author
Message
rony09830
rony09830
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 6
I am new to vb and sql . please bear with me
I have a database name database 1. I have column name "column A" and “column B" in all tables. I want to retrieve tables name and sum of (column A – column B) of all tables like below


table1 ( columnA - columnB)
table2 ( columnA - columnB)
table3 ( columnA - columnB)

if i have some manageable table I can use table names in sql statement
like bellow
SELECT "Table1" AS Table, SUM(a) - SUM(b) AS Result FROM table1
UNION
SELECT "Table2", SUM(a) - SUM(b) FROM table2
UNION
SELECT "Table3", SUM(a) - SUM(b) FROM table3



I would like to know is it possible to create a cursor to dynamically collect the data from all tables ?

I have tried the following code, When I run the sql statement I get the following error

Syntax error (missing operator) in query expression 'GROUP_CONCAT(COLUMN_NAME ORDER BY COLUMN_NAME SEPARATOR ' - ')'.

Dim CmdStr As String = "SELECT TABLE_NAME, GROUP_CONCAT(COLUMN_NAME ORDER BY COLUMN_NAME SEPARATOR ' - ')FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('a','b')AND TABLE_SCHEMA='database1'GROUP BY COLUMN_NAME;" 





or is there any way to do it in vb.net and ms access database? any bit of info would be helpful
Erland Sommarskog
Erland Sommarskog
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5432 Visits: 875
So here is the answer you did not expect: in a relational database a table is supposed to model a unique entity, and each column is supposed to model a unique attribute of that entity. Given this, the question you ask does not make much sense. Yes, it is possible to do, but it is not a good exercise for a beginner, because it will lead into paths where you should not wander at this point.

My assumption is that you do this as some kind of exercise to learn, but it is not a good exercise where you are now.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
rony09830
rony09830
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 6
Thanks for your response.
before giving up on it , I am still curious to know is there any way to fix the sql query?
Erland Sommarskog
Erland Sommarskog
SSCertifiable
SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)SSCertifiable (5.4K reputation)

Group: General Forum Members
Points: 5432 Visits: 875
The query you posted uses group_concat() which is not a function in SQL Server. That smells MySQL to me, but I could be wrong. Furthermore, the error message you posted is not from SQL Server. (And this is an SQL Server forum.)

Here is a way to write this query in SQL Server, but which will not work in any other product:

SELECT TABLE_NAME, substring(collist, 1, len(collist) - 2)
FROM INFORMATION_SCHEMA.TABLES T
CROSS APPLY (SELECT COLUMN_NAME + ' - '
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE T.TABLE_NAME = C.TABLE_NAME
AND C.COLUMN_NAME IN ('a', 'b')
FOR XML PATH('')) AS C (collist)
WHERE collist IS NOT NULL



Erland Sommarskog, SQL Server MVP, www.sommarskog.se
RatanDeep Saha
RatanDeep Saha
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1210 Visits: 693
Have you explored the option of CTE .
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search