Click here to Skip to main content
15,891,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'1%' select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'2%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'3%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'4%'" +
"select SUM(dr_amount)-SUM(cr_amount)" +
"from Gl_Transaction where Accountno like'5%'";
Posted
Updated 25-Jun-14 10:13am
v2
Comments
[no name] 25-Jun-14 14:31pm    
Use the spacebar where you need to so your string makes sense.
CPallini 25-Jun-14 15:29pm    
5.
Member 10690757 25-Jun-14 14:35pm    
how?
CPallini 25-Jun-14 15:32pm    
For instance, if you concatenate "select SUM(dr_amount)-SUM(cr_amount)" with "from ..." then you obtain "select SUM(dr_amount)-SUM(cr_amount)from ..." which is invalid because there is no blank between the closing brace ')' and 'from'.

Adding spacebar does not help ;( Sorry, collegues... Even when OP change the code, this doesn't help too, because He/She will get SQL error.

Do you know SQL? I'd suggest to strat with basics: SQL Tutorial[^]
 
Share this answer
 
Your example looks like a C# statement although it is syntactically incorrect since there is no operator in the statement. In my solution below, I added a declaration for a string named mySQLStatement and an assignment to that statement of the concatenated strings that you included in your question. The result is that mySQLStatement contains a syntactically correct SQL Statement. I know that is true because I created a table and ran the statement against it.
C#
string mySQLStatement = "select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'1%' select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'2%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'3%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'4%'" +
"select SUM(dramount)-SUM(cramount)" +
"from GlTransaction where Accountno like'5%'";
After executing the above statement the contents of mySQLStatement will be
SQL
"select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'1%' select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'2%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'3%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'4%'select SUM(dramount)-SUM(cramount)from GlTransaction where Accountno like'5%'"


Your SQL statement will create five result sets. While that will work, it may not be the simplest way to retrieve this data. I recommend that you modify your SQL statement to return just one result set with a separate column for each value rather than five result sets of one column each.

Maybe that's not what you really want to do.... You may want to clarify your question using the Improve Question button above and explain what you are trying to accomplish (using an example of what you want the result to look like).
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900