Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi.

I have this query , using unpivot SQL statement. When I run the query on SQL server, it execute and gives out the result that I desired. But, when I want to use the same query on Gridview function on Visual Studio 2015, using VB , it said that " The Declare SQL construct or statement is not supported." So, How do I need to rearrange the unpivot SQL statement as it is not supported on gridview?

What I have tried:

SQL
 declare @MyTable table(
 
   [Total Interest] money,
  [Total Principal] money,
  [Total Penalty] money,
  [Total Charged] money

   )

  insert into @MyTable 
  
select (SELECT SUM([Total Interest Overdue(MYR)]) AS [Total Interest Overdue(MYR)] FROM (SELECT SUM(l.interestOverdue) * d.bidspotrate AS [Total Interest Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ),
(SELECT SUM([Total Principal Overdue(MYR)]) AS [Total Principal Overdue(MYR)] FROM (SELECT SUM(l.principalOverdue) * d.bidspotrate AS [Total Principal Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ),
(SELECT SUM([Total Penalty Overdue(MYR)]) AS [Total Penalty Overdue(MYR)] FROM (SELECT SUM(l.penaltyOverdue) * d.bidspotrate AS [Total Penalty Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode WHERE (l.source = 'Excel ') GROUP BY d.bidspotrate) AS [Total in MYR]),
(  select sum([Total Charged Overdue(MYR)])  as 
 [Total Charged Overdue(MYR)] from (select (SUM (l.chargedOverdue) * d.bidspotrate) as 
 [Total Charged Overdue(MYR)] from loan l, danadb d  where l.currency=d.ccycode   group by d.bidspotrate) as [Total in MYR])

    select [Type], [Total] from
 (select * from @MyTable) Src
  unpivot ([Total] for [Type] in ([Total Interest],[Total Principal],[Total Penalty],[Total Charged])) Res



The result of the query is

Type | Total
Total Interest | 10039176.9812
Total Principal | 92207576.6798
Total Penalty | 91959.7905
Total Charged | 21677.153


-------------------------------------------------------------------

The second query that I tried is

select [TOTAL]
from
(
select (SELECT SUM([Total Interest Overdue]) AS [Total Interest Overdue] FROM (SELECT SUM(l.interestOverdue) * d.bidspotrate AS [Total Interest Overdue] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ) as [Interest],
(SELECT SUM([Total Principal Overdue]) AS [Total Principal Overdue] FROM (SELECT SUM(l.principalOverdue) * d.bidspotrate AS [Total Principal Overdue] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ) [Principal],
(SELECT SUM([Total Penalty Overdue]) AS [Total Penalty Overdue] FROM (SELECT SUM(l.penaltyOverdue) * d.bidspotrate AS [Total Penalty Overdue] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode WHERE (l.source = 'Excel ') GROUP BY d.bidspotrate) AS [Total in MYR]) [Penalty],
( select sum([Total Charged Overdue]) as
[Total Charged Overdue] from (select (SUM (l.chargedOverdue) * d.bidspotrate) as
[Total Charged Overdue] from loan l, danadb d where l.currency=d.ccycode group by d.bidspotrate) as [Total in MYR]) [Charged] ) d

unpivot

(
[TOTAL] for amount in ([Interest], [Principal],[Penalty] , [Charged] )) pvt1

The result is as below

TOTAL
--------------
10039176.9812472
92207576.6797772
91959.790477519
21677.1530053711


I want another column which is TYPE to appear, but I did not know how to do it .
Posted
Updated 15-Dec-16 13:59pm
v6
Comments
Dave Kreskowiak 14-Dec-16 20:25pm    
This doesn't make sense. A GridView doesn't execute SQL so what code are you running to try to execute this SQL?
Member 12882545 14-Dec-16 20:33pm    
Oh, I'm using SQLDataSource to bind the SQL statement and the gridview. When I just using SQL statement like " select name,address from company" using SQLDataSource, the data can be viewed. But when I'm using the unpivot SQL statement, it shows the error.
Dave Kreskowiak 14-Dec-16 20:35pm    
Again, show the code. What you're saying isn't making sense.
Member 12882545 14-Dec-16 20:39pm    
This is the code I'm using

declare @MyTable table(

[Total Interest] money,
[Total Principal] money,
[Total Penalty] money,
[Total Charged] money

)

insert into @MyTable

select (SELECT SUM([Total Interest Overdue(MYR)]) AS [Total Interest Overdue(MYR)] FROM (SELECT SUM(l.interestOverdue) * d.bidspotrate AS [Total Interest Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ),
(SELECT SUM([Total Principal Overdue(MYR)]) AS [Total Principal Overdue(MYR)] FROM (SELECT SUM(l.principalOverdue) * d.bidspotrate AS [Total Principal Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode GROUP BY d.bidspotrate) AS [Total in MYR] ),
(SELECT SUM([Total Penalty Overdue(MYR)]) AS [Total Penalty Overdue(MYR)] FROM (SELECT SUM(l.penaltyOverdue) * d.bidspotrate AS [Total Penalty Overdue(MYR)] FROM Loan AS l INNER JOIN Danadb AS d ON l.currency = d.ccycode WHERE (l.source = 'Excel ') GROUP BY d.bidspotrate) AS [Total in MYR]),
( select sum([Total Charged Overdue(MYR)]) as
[Total Charged Overdue(MYR)] from (select (SUM (l.chargedOverdue) * d.bidspotrate) as
[Total Charged Overdue(MYR)] from loan l, danadb d where l.currency=d.ccycode group by d.bidspotrate) as [Total in MYR])

select [Type], [Total] from
(select * from @MyTable) Src
unpivot ([Total] for [Type] in ([Total Interest],[Total Principal],[Total Penalty],[Total Charged])) Res

When I run the code on SQL Server, it gives the output that I desired. But, When I want to use the same code on SQLDataSource , on gridview using Visual Studio 2015, it shows this error "The Declare SQL construct or statement is not supported".
Dave Kreskowiak 14-Dec-16 20:40pm    
NO! The code in your application that your running to execute this statement!

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