Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL exception while selecting the columns with alias names

My SQL query is

cmd = new OleDbCommand("select vchr_No as voucher No, vchr_Date as Date, 
                        vchr_Acnthd as Debit, vchr_Prtynm as Paid to 
                        from cshvchrs  
                        where vchr_No like '%" + vchno + "%' ", con);

When I try to retrieve the data I am getting an exception:

The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.

Please anybody help me

like image 332
Learner Avatar asked Jun 06 '26 21:06

Learner


2 Answers

You have invalid SQL - the aliases that contain spaces need to be escaped, as do aliases that are keywords.

cmd = new OleDbCommand("select vchr_No as [voucher No] ,vchr_Date as [Date] ,vchr_Acnthd as Debit ,vchr_Prtynm as Paid to from cshvchrs  where vchr_No like '%" + vchno + "%' ", con);
like image 119
Oded Avatar answered Jun 09 '26 11:06

Oded


On vchr_Date as Date part,

Date is a reserved keyword on Transact-SQL.

Reserved keywords are part of the grammar of the Transact-SQL language that is used by SQL Server to parse and understand Transact-SQL statements and batches.

You can use it with square brackets [] like;

vchr_Date as [Date]

Use your full query as;

cmd = new OleDbCommand("select vchr_No as [voucher No] ,vchr_Date as [Date] ,vchr_Acnthd as Debit ,vchr_Prtynm as Paid to from cshvchrs  where vchr_No like '%" + vchno + "%' ", con);
like image 30
Soner Gönül Avatar answered Jun 09 '26 11:06

Soner Gönül



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!