65.9K
CodeProject is changing. Read more.
Home

Remove comment in SQL

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.72/5 (23 votes)

Feb 13, 2003

viewsIcon

68348

downloadIcon

464

Remove comment in SQL to make it easy to execute with MS Access.

Introduction

Your can write SQL which reads good, like this:

-- What the sql (et. stored procedure )writen for
-- and/or how to use it
/* Or block comment like this
   What the sql (et. stored procedure )writen for
   and/or how to use it
*/
create table tablename (
    fieldname text(255), -- comment for field 
    content text(255));

Comments in SQL statements sent through ADO will sometimes error out in ADO when ADO tries to parse it. By stripping out the comments, you have both documented SQL code and you don't get any errors when you try to use the commented code within your ADO application. Then use these SQL saved in files directly!

How to use it?

Declare the function in any .h file of your project:

int ClearSQLComment(LPCTSTR lpSQL,CString &rString);

and use it:

CString strText,strSQL;
//Read all text from file to a CString object ,write it yourselft
LoadTextFromFile(lpFileName,&strText);
ClearSQLComment(strText,strSQL);
//...Execute SQL 
Connect.Execute(strSQL,&affected,adCmdText);