473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Syntax error or access violation

1
I have an ASP page in which there is embedded SQL. I am trying to use variables in the SQL but am getting the error 'syntax error or access violation'.
In the past, I have successfully used variables for table names and within WHERE clauses in SQL in an ASP page. This time, I am trying to use variables in the SELECT portion of an INSERT statement.

(1) The following code works in a SQL stored procedure:
DECLARE @BatchId uniqueidentifier
SELECT @BatchId = NEWID()

INSERT INTO sgCustomASPBatch
SELECT @BatchId,e.EngagementId,GETDATE()
FROM Engagement e
WHERE e.Name like '%ASP'

(2) Here is the same code in ASP which gets the syntax error:
dim BatchId

' get new id for batch
sSQL2 = "SELECT NEWID() 'NewId' "
set rst2 = oUtility2.Execute(Session("ReportsDSN"),sSQL2)
BatchId = rst2("NewId")

set rst2 = nothing

sSQL2 = "INSERT INTO sgCustomASPBatch " & _
"SELECT " & BatchId & _
" , EngagementId, GETDATE() " & _
"FROM Engagement e " & _
"WHERE e.EngagementId in ('" & pEngId & "') "
set rst2 = oUtility2.Execute(Session("ReportsDSN"),sSQL2)

(3) Without the variable for BatchId, this code works in ASP:
sSQL2 = "INSERT INTO sgCustomASPBatch " & _
"SELECT NEWID(), EngagementId, GETDATE() " & _
"FROM Engagement e " & _
"WHERE e.EngagementId in ('" & pEngId & "') "
set rst2 = oUtility2.Execute(Session("ReportsDSN"),sSQL2)


What do I need to change in the syntax regarding the variable BatchId to make example 2 above work in ASP?

Thanks.
Judy
Dec 4 '06 #1
2 4649
shweta123
692 Expert 512MB
write the sql as this
sSQL2 = "INSERT INTO sgCustomASPBatch " & _
"SELECT " & '" & BatchId & "' & _
" , EngagementId, GETDATE() " & _
"FROM Engagement e " & _
"WHERE e.EngagementId in ('" & pEngId & "') "
set rst2 = oUtility2.Execute(Session("ReportsDSN"),sSQL2)


just a thought

bye
Shweta
Dec 5 '06 #2
It looks like you are going to want to get into stored procedures. You can't use DECLARE @ANYTHING in a SQL statement from ASP. At least, I don't think so.

But if you have a stored procedure that is:

Expand|Select|Wrap|Line Numbers
  1. CREATE stored procedure sp_MySP
  2.   @name varchar,
  3.   @id int
  4. AS
  5.  
  6. UPDATE MyTable
  7. SET [Name] = @name
  8. WHERE ID = @id
  9.  
You could call it like this:

conn.execute("exec sp_MySP 'Michael', 10") and it would work...

Hope this helps,

Michael C. Gates
Dec 6 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
1
by: Rsa Myh | last post by:
Hi, I am running SQL Server 2000 SP3 on Windows Server 2003 and since recently have a strange problem executing shape queries from COM+ components using ADO. Until 4 days ago, they worked,...
8
by: DraguVaso | last post by:
Hi, I want my application do different actions depending on the exception it gets. For exemple: I have an SQL-table with a unique index. In case I try to Insert a record that's alreaddy in it I...
0
by: Mike Thomas | last post by:
This one must be very simple. I am trying to run an SQL Serv 7.0 stored proc from a VBA module in an Access 2000 app. This stored proc runs fine when I remove the parameters from the stored proc...
4
by: Dave | last post by:
Hi, I'm running Access '03, SQL SERVER 2k on Windows XP SP1. I've not got a ton of experience in writing UDF's for t-sql. In SQL Query Analyzer, I'm trying to write a user defined scalar...
27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
2
by: Nate | last post by:
I am trying to use the following IF statement, and whenever I use a single or double quote, I receive a syntax error. When using no quotes, the syntax is accepted, but the statement does not...
2
by: jthep | last post by:
I'm trying to get this piece of code I converted from C to work in C++ but I'm getting an access violation error. Problem occurs at line 61. Someone can help me with this? The function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.