Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 What is wrong with this statement

Author  Topic 

gaby_58
Starting Member

33 Posts

Posted - 2012-06-21 : 11:15:12
Hi all,

I have this SQL statement and gives an error like this
Incorrect syntax near '6335'
cmdS2 = new SqlCommand("SELECT TOP 1 SupplierId, Supplier,"
+ " SUM(TotalValue) FROM view_test WHERE YEAR(dDate)='"
+ pRefYear.ToString() + "' AND " + strR +
"Id IN(" + strId
+ ")AND SupplierId <> '"
+ strSupplierId[0]
+ "AND SupplierId <> '6335'"
+ "GROUP BY SupplierId, Supplier ORDER BY SUM(TotalValue) DESC", conS2);

Thanks for any info

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-21 : 11:48:16
Remove the single quote on the line indicated below:
cmdS2 = new SqlCommand("SELECT TOP 1 SupplierId, Supplier,"
+ " SUM(TotalValue) FROM view_test WHERE YEAR(dDate)='"
+ pRefYear.ToString() + "' AND " + strR +
"Id IN(" + strId
+ ")AND SupplierId <> '" -- <== Remove the single quote shown in red
+ strSupplierId[0]
+ "AND SupplierId <> '6335'"
+ "GROUP BY SupplierId, Supplier ORDER BY SUM(TotalValue) DESC", conS2);
Also, put spaces so that there is a space between the supplierId and the AND that follows it; similarly, before GROUP BY etc.
Go to Top of Page

gaby_58
Starting Member

33 Posts

Posted - 2012-06-21 : 11:59:19
Thanks a lot, it worked..
Go to Top of Page
   

- Advertisement -