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


error:- Unhandled Exception: System.Data.SqlClient.SqlException: Incorrect syntax near t he keyword...


error:- Unhandled Exception: System.Data.SqlClient.SqlException: Incorrect syntax near t he keyword 'as'.‌

Author
Message
Patrick.I
Patrick.I
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2647 Visits: 276
HI All,
I'm trying to use this sql query in my asp.net application but i keep getting error:-

Unhandled Exception: System.Data.SqlClient.SqlException: Incorrect syntax near t
he keyword 'as'.


SqlCommand cmd = new SqlCommand("select DATENAME(month, GETDATE()) AS [month],month(getdate()) as [monthnumber],t1.Actual,t2.Target from (cast(round(sum(amountmst)/1000000,2) as decimal(10,2)) as [Actual] from trans where month(transdate) =month(getdate()) and year(transdate)  = year(getdate()) and transtype=2 group by month(transdate)) as t1, 
(Select cast(round(sum(monthlybudget)/1000000,2) as decimal(10,2)) as [Target] from customer where month(date) = month(getdate()) and year(date)= year(getdate()) group by month(date)) as t2", con);


I know this should be a stor proc but don't have access.
Any ideas would be appreciated
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)

Group: General Forum Members
Points: 380516 Visits: 42209
Patrick.I - Saturday, February 3, 2018 6:07 AM
HI All,
I'm trying to use this sql query in my asp.net application but i keep getting error:-

Unhandled Exception: System.Data.SqlClient.SqlException: Incorrect syntax near t
he keyword 'as'.


SqlCommand cmd = new SqlCommand("select DATENAME(month, GETDATE()) AS [month],month(getdate()) as [monthnumber],t1.Actual,t2.Target from (cast(round(sum(amountmst)/1000000,2) as decimal(10,2)) as [Actual] from trans where month(transdate) =month(getdate()) and year(transdate)  = year(getdate()) and transtype=2 group by month(transdate)) as t1, 
(Select cast(round(sum(monthlybudget)/1000000,2) as decimal(10,2)) as [Target] from customer where month(date) = month(getdate()) and year(date)= year(getdate()) group by month(date)) as t2", con);


I know this should be a stor proc but don't have access.
Any ideas would be appreciated


Could be that your SQL isn't even syntactically correct.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Pearce
Sean Pearce
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14060 Visits: 3531
There is a missing select, and no join condition for t1 and t2.
SqlCommand cmd = new SqlCommand("
select
DATENAME(month, GETDATE()) AS [month],
month(getdate()) as [monthnumber],
t1.Actual,
t2.Target
from
( [NO SELECT HERE]
cast(round(sum(amountmst)/1000000,2) as decimal(10,2)) as [Actual]
from
trans
where
month(transdate) = month(getdate())
and year(transdate) = year(getdate())
and transtype=2
group by
month(transdate)) as t1,
(Select
cast(round(sum(monthlybudget)/1000000,2) as decimal(10,2)) as [Target]
from
customer
where
month(date) = month(getdate())
and year(date)= year(getdate())
group by
month(date)) as t2
[NO JOIN CONDITION HERE]
", con);





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)SSC Guru (380K reputation)

Group: General Forum Members
Points: 380516 Visits: 42209
Sean Pearce - Sunday, February 4, 2018 5:00 AM
There is a missing select, and no join condition for t1 and t2.
SqlCommand cmd = new SqlCommand("
select
DATENAME(month, GETDATE()) AS [month],
month(getdate()) as [monthnumber],
t1.Actual,
t2.Target
from
( [NO SELECT HERE]
cast(round(sum(amountmst)/1000000,2) as decimal(10,2)) as [Actual]
from
trans
where
month(transdate) = month(getdate())
and year(transdate) = year(getdate())
and transtype=2
group by
month(transdate)) as t1,
(Select
cast(round(sum(monthlybudget)/1000000,2) as decimal(10,2)) as [Target]
from
customer
where
month(date) = month(getdate())
and year(date)= year(getdate())
group by
month(date)) as t2
[NO JOIN CONDITION HERE]
", con);



Actually, it is a Cartesian product (or cross join).

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sean Pearce
Sean Pearce
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14060 Visits: 3531
I didn't say there must be a join condition, just pointing out that there is no condition, which the OP may or may not be aware of Cool



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
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