Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access thread: How to insert a sql text in the database?


Message #1 by npandey@h... on Fri, 23 Feb 2001 23:09:37
Hi Everybody,



I am working on a site where the a sql text is generated by a form. This 

sql text needs to be saved in the database so that if can retreived later 

by using the name used to save the sql text. Here is the code



----------CODE STARTS HERE---------------

<%

Queryname = Trim(Request.Form("Queryname"))

Sqlcode = Trim(Request.Form("Sqlcode"))



' function for avoiding error due to single quotes in sql text

Function SQLQuote(var)

	If InStr(var, "'") <> 0 Then

		var = Replace(var, "'", "''")

	End If

	SQLQuote = var

End Function



sDBName = "Provider=Microsoft.Jet.OLEDB.4.0; Data 

Source=E:\database\tcaoffers.mdb"

Set objDB = Server.CreateObject("ADODB.Connection")

objDB.Open sDBName



'Code to add a new record...

sql = "Insert Into queries ("

sql = sql & "Queryname,"

sql = sql & "Sqlcode"

sql = sql & ") "

sql = sql & "Values ("

sql = sql & "'" & SqlQuote(Queryname) & "',"

sql = sql & "'" & SqlQuote(Sqlcode) & "'"

sql = sql & ");"

	

Response.Write sql

ObjDB.Execute(sql)



objDB.Close

Set objDB = Nothing

%>

----------CODE ENDS HERE---------------



Somehow this is not working and I am getting following errors:



Insert Into queries (Queryname,Sqlcode) Values ('causers','SELECT * FROM 

eoffers WHERE State LIKE ''CA%'';'); 

Microsoft JET Database Engine error '80040e14' 



Syntax error in INSERT INTO statement. 



/tolladmin/insertquery.asp, line 28







I cannot find what is causing the error. Please help me.



Thanks in advance for your time and help



Regards to all

Nagendra Pandey
Message #2 by "Leif N. Eriksen" <Leif_N_Eriksen@y...> on Sat, 24 Feb 2001 19:59:45
Your SQLQuote is fine - but don't use it in an select statement. The 

select statement should be like ...



Insert Into queries (Queryname,Sqlcode) Values ('causers','SELECT * FROM 

eoffers WHERE State LIKE 'CA*'')



Leif



Message #3 by npandey@h... on Sun, 25 Feb 2001 09:28:08
Hi Lief,



Thanks for trying to help.



Finally I found the error and wanted others to know.



The error was using the reserved work Sqlcode. I changed it to Sql_Code 

and the code worked fine.



Cheers

Nagendra Pandey





> Your SQLQuote is fine - but don't use it in an select statement. The 

> select statement should be like ...

> 

> Insert Into queries (Queryname,Sqlcode) Values ('causers','SELECT * FROM 

> eoffers WHERE State LIKE 'CA*'')

> 

> Leif

> 


  Return to Index