1

Re: Stuck with INSERT syntax problem

I had been doing quite well migrating Access queries to XML over the last couple of days, but when I tried to do stuff with temporary tables for location hierarchy support I've become stuck with a simple syntax problem that is driving me nuts. Can anyone please show me the obvious error that I'm missing with the following cut-down code that is giving the error message "Incorrect syntax near the keyword INTO"?

<?xml version="1.0" ?>
<CustomReport menupath="BRMC" title="BRMC - Temp Table Debug" description="Syntax debug">
  <SQL>
    <Where keytype="Default">
    CREATE TABLE #Locations (Location_Key CHAR(16) COLLATE Database_Default PRIMARY KEY, Parent_Key CHAR(16) COLLATE Database_Default)
    INSERT INTO #Locations (Location_Key,Parent_Key)
    SELECT 
      LOCATION.LOCATION_KEY, LOCATION.LOCATION_KEY
    FROM
      LOCATION
    WHERE  
      LOCATION.LOCATION_KEY = 'SR000149000000Y2'
      
    DROP TABLE #Locations
    </Where>
  </SQL>
</CustomReport>

Thanks, Keith

2

Re: Stuck with INSERT syntax problem

CREATE TABLE should be a separate statement. It looks like the SQL engine is parsing INSERT as part of the CREATE statement because you've not put a semi-colon after it to terminate it. Try putting a semi-colon after the closing bracket of the CREATE statement to see if that helps. Here's an example:

http://msdn.microsoft.com/en-us/library/ms189872(SQL.90).aspx

However, you don't really need the CREATE TABLE statement at all:

http://msdn.microsoft.com/en-us/library/ms190750(v=SQL.90).aspx

Charles Roper
Digital Development Manager | Field Studies Council
http://www.field-studies-council.org | https://twitter.com/charlesroper | https://twitter.com/fsc_digital

3

Re: Stuck with INSERT syntax problem

Hi Charles,
I've tried the semi-colon and it made no difference. None of the XML report examples that I've looked at contain a semi-colon on this statement. (I even tried deleting the CREATE TABLE and got the same message). The mystery remains.
Thanks, Keith

4

Re: Stuck with INSERT syntax problem

As far as I am aware the semi-colon is only necessary in MS Access, other forms of SQL seem to manage without.

A couple of possible suggestions 1. In the insert into, there appears to be a space missing, between the comma and Parent_key (might make a difference) and 2. the select is selecting  the same field (location_key) twice (shouldn't matter, but worth pointing out anyway).

Charles is right though, if the above is all you need to do, then you don't need the temporary table, just the select statment.

Rob Large
Wildlife Sites Officer
Wiltshire & Swindon Biological Records Centre

5

Re: Stuck with INSERT syntax problem

Thanks for the suggestions Rob.
I've tried including a space and it made no difference, neither did changing the second item of the SELECT to be something else.

The code is cut down for debug from the code needed to process site hierarchies in Mike Weideli's help, which I presume works for him and others, but not for me. I have probably introduced a subtle error that I can't spot. Here's his complete code:

CREATE TABLE #Locations (Location_Key CHAR(16) COLLATE Database_Default PRIMARY KEY,
      Parent_Key CHAR(16)  COLLATE Database_Default)

INSERT INTO #Locations (LOcation_Key,Parent_Key) 
SELECT L.LOCATION_KEY, L.LOCATION_KEY  
FROM  
LOCATION L 
WHERE  
<Condition field= "L.Location_key" operator="equal" type="Location"  name="Select Locations" entrycount="-1" />
WHILE 1=1 BEGIN
 INSERT INTO #Locations
  SELECT L.Location_Key, Tinc.Parent_Key
  FROM Location L
  INNER JOIN #Locations Tinc on L.Parent_Key=Tinc.Location_Key
  LEFT JOIN #Locations Texc ON Texc.Location_Key=L.Location_Key
  WHERE Texc.Location_Key IS NULL
 IF @@ROWCOUNT=0
  BREAK
END

Regards, Keith

6

Re: Stuck with INSERT syntax problem

Hi Keith

What is actually wrong is that your query does not have any select statement in it to output some data. An XML report should always have the last SELECT statement output a table of data for the report. Recorder looks in the query to find this statement, and changes it to a SELECT * INTO ... statement to put your report's data into a temporary table. For example

SELECT *
FROM Taxon

would be converted by Recorder to

SELECT *
INTO #REPORT_OUTPUT
FROM Taxon

Having your query output in a temp table like this makes it a lot easier for Recorder to use the data. But in your case, the last SELECT statement is actually part of an INSERT INTO statement. So your statement becomes:

    CREATE TABLE #Locations (Location_Key CHAR(16) COLLATE Database_Default PRIMARY KEY, Parent_Key CHAR(16) COLLATE Database_Default)
    INSERT INTO #Locations (Location_Key,Parent_Key)
    SELECT 
      LOCATION.LOCATION_KEY, LOCATION.LOCATION_KEY
    INTO #REPORT_OUTPUT FROM
      LOCATION
    WHERE  
      LOCATION.LOCATION_KEY = 'SR000149000000Y2'
      
    DROP TABLE #Locations

This is no longer valid SQL. So the quick fix might be to do something like SELECT TOP 1 * FROM #Locations at the end.

A top tip, if you are using SQL Express and therefore don't have access to the SQL Profiler tool (which can log the actual SQL sent to the server and is brilliant for troubleshooting things like this), then try the AnjLab SQL Profiler, which is Open Source. You can find it at http://anjlab.com/en/projects/opensource/sqlprofiler.

John van Breda
Biodiverse IT

7

Re: Stuck with INSERT syntax problem

Thank you John. I would never have figured out that myself. I was assuming Recorder was executing the SQL that I was giving it instead of making up its own.

The AnjLab SQL Profiler you referred to looks very useful and showed the problem, and that your suggested workaround solved the problem.

I should be able to make progress again now...

Many thanks, Keith