473,422 Members | 1,858 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,422 software developers and data experts.

(MSSQL 2005) - Incorrect syntax near the keyword 'AS'

NeoPa
32,556 Expert Mod 16PB
As the title implies, I am getting the following error message with some SQL I'm trying to use. I'm using MS Management Studio 2008 with a link to a 2005 server.

What confuses me is that when I change the first line from a DELETE to a SELECT, the SQL works exactly as I would expect it to.
Expand|Select|Wrap|Line Numbers
  1. DELETE
  2. FROM   [DBManagement].[dbo].[AnalysisRange] AS [tAR] INNER JOIN
  3.        [DBManagement].[dbo].[AnalysisRange] AS [tAR2]
  4.   ON   SUBSTRING([tAR].[AR_Analysis],2,3)=SUBSTRING([tAR2].[AR_Analysis],2,3)
  5.  AND   [tAR].[AR_Range]=[tAR2].[AR_Range]
  6. WHERE  SUBSTRING([tAR2].[AR_Analysis],1,1)='1'
  7. GO
Just to be crystal clear, I'm talking about replacing line 1 with :
Expand|Select|Wrap|Line Numbers
  1. SELECT [tAR].*, [tAR2].*
Background :

I have a table consisting of two fields, [AR_Analysis] (nvarchar(4)) and [AR_Range] (smallint). Each record is unique but neither field is. Valid values in [AR_Analysis] start with a '0' and are 4 characters long. I have the table populated currently with various matching pairs, but I have a new list of pairs within that list which should be deleted. To effect this I added the pairs to be deleted into the table but with a '1' as the first character of [AR_Analysis] but otherwise the same. I intend to clear away the original invalid items, as well as the new deletion items (starting with '1') with the above SQL. Let me see if I can make this easier with some example data :
Table = [AnalysisRange]
Expand|Select|Wrap|Line Numbers
  1. [AR_Analysis]  [AR_Range]
  2.     0401           21
  3.     0401           22
  4.     0401           23
  5.     0420           20
  6.     0420           22
  7.     0420           24
  8.     1401           22
  9.     1420           24
The valid data left after the process is complete should be :
Expand|Select|Wrap|Line Numbers
  1. [AR_Analysis]  [AR_Range]
  2.     0401           21
  3.     0401           23
  4.     0420           20
  5.     0420           22
The strange part, that completely confuses me, is that the results produced by what is essentially the same SQL when used as a SELECT query are exactly as I'd expect. When the DELETE query is attempted though, I get the message :
Expand|Select|Wrap|Line Numbers
  1. Msg 156, Level 15, State 1, Line 2
  2. Incorrect syntax near the keyword 'AS'.
I have checked in the Books Online help system, but wasn't able to decipher an answer in it. Maybe I need to get more used to the terminology, but for now it's too obscure for me.

PS. Any advice as to alternative ways of approaching my main problem is welcomed (I'm sure I'm not using the best approach) but I'm mainly interested in the specific question as to why what I'm attempting is causing this response.
Jul 26 '10 #1

✓ answered by NeoPa

Thanks for the link Oralloy. I'm afraid the idea wasn't quite right, but the link helped nevertheless.

If you look at the last example under Example D, you'll see aliases used. It does get a little complicated though. There seem to be two different FROM clauses (unless maybe the first, on the first line, is more of a predicate than a clause. I'm not sure I know). Anyway, this first FROM seems to have the job of specifying where the deletions should occur from. It's possible this has no facility to use an alias as you say. The second, which seems to be specifying the source of data that identifies the records in the first that need to be deleted, certainly seems to have the facility to use aliases. This query would not be possible to specify otherwise as the same table is used twice in two different ways.

All that said, I think I do have a much better understanding of the DELETE clause now. When I tried to look at the specification yesterday I missed the bit where there were two FROMs. I must have got confused by all the bracketing and assumed they were simply in different optional components (Most of my SQL experience is in Jet where the format of the DELETE clause is quite different).

The following SQL worked perfectly :)
Expand|Select|Wrap|Line Numbers
  1. USE [DBManagement]
  2. GO
  3.  
  4. DELETE [tAR]
  5. FROM   [dbo].[AnalysisRange] AS [tAR] INNER JOIN
  6.        [dbo].[AnalysisRange] AS [tAR2]
  7.   ON   SUBSTRING([tAR].[AR_Analysis],2,3)=SUBSTRING([tAR2].[AR_Analysis],2,3)
  8.  AND   [tAR].[AR_Range]=[tAR2].[AR_Range]
  9. WHERE  SUBSTRING([tAR2].[AR_Analysis],1,1)='1'
  10. GO
Thank you for making me look more deeply at the Help explanation. It was hard work but worth it :)

5 5461
Oralloy
988 Expert 512MB
Reading microsoft's documentation here, I see no reference to table aliases (the "AS") in their syntax diagram.

I didn't see anything similar for MySQL, either.

I'd be willing to bet that you have to use the fully qualified table names in your query.
Jul 27 '10 #2
NeoPa
32,556 Expert Mod 16PB
Thanks for the link Oralloy. I'm afraid the idea wasn't quite right, but the link helped nevertheless.

If you look at the last example under Example D, you'll see aliases used. It does get a little complicated though. There seem to be two different FROM clauses (unless maybe the first, on the first line, is more of a predicate than a clause. I'm not sure I know). Anyway, this first FROM seems to have the job of specifying where the deletions should occur from. It's possible this has no facility to use an alias as you say. The second, which seems to be specifying the source of data that identifies the records in the first that need to be deleted, certainly seems to have the facility to use aliases. This query would not be possible to specify otherwise as the same table is used twice in two different ways.

All that said, I think I do have a much better understanding of the DELETE clause now. When I tried to look at the specification yesterday I missed the bit where there were two FROMs. I must have got confused by all the bracketing and assumed they were simply in different optional components (Most of my SQL experience is in Jet where the format of the DELETE clause is quite different).

The following SQL worked perfectly :)
Expand|Select|Wrap|Line Numbers
  1. USE [DBManagement]
  2. GO
  3.  
  4. DELETE [tAR]
  5. FROM   [dbo].[AnalysisRange] AS [tAR] INNER JOIN
  6.        [dbo].[AnalysisRange] AS [tAR2]
  7.   ON   SUBSTRING([tAR].[AR_Analysis],2,3)=SUBSTRING([tAR2].[AR_Analysis],2,3)
  8.  AND   [tAR].[AR_Range]=[tAR2].[AR_Range]
  9. WHERE  SUBSTRING([tAR2].[AR_Analysis],1,1)='1'
  10. GO
Thank you for making me look more deeply at the Help explanation. It was hard work but worth it :)
Jul 27 '10 #3
Oralloy
988 Expert 512MB
@NeoPa
Hey, I'm glad it helped. I learned something, too, which is why I participate here.

Other than convienence, is there a reason for the USE statement before your query?

Cheers,
Oralloy
Jul 27 '10 #4
NeoPa
32,556 Expert Mod 16PB
Oralloy: Other than convienence, is there a reason for the USE statement before your query?
Absolutely not. You're right on the money (at least that was certainly my motivation).

I've done T-SQL before, but I'm mainly an Access man at the moment. I'm hoping to move into SQL Server more though, as time goes on. Some of the ideas seem quite different though.
Jul 27 '10 #5
Oralloy
988 Expert 512MB
@NeoPa
God, Access reports just give me fits. Good luck making the transition to SQL Server. I've always found my problems were with reporting and inherited de-normal schemata, and not with new design.

I'm really not familiar with SQL Server as its self, just as another form of SQL server over ODBC. I've used it and written basic schemata against it using portable SQL, but nothing hard-core microsoft centric (if that makes sense?).

Most of my database has been through ODBC from C++ and Java, with no use of stored procedures. Basically what I do is create schemata, create applications that log tons of measurement information, retrieve data, compute fits, store final working calibration information. All-in-all, just basic, simple, generic SQL.
Jul 27 '10 #6

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

Similar topics

1
by: Jeff Magouirk | last post by:
Dear Group, I am trying to create a view and keep getting the Incorrect syntax near the keyword 'Declare'" error. Here is the code I am writing. Create view fixed_airs (sid, fad_a2, fad_a3)...
11
by: Mark Findlay | last post by:
Hello Experts! I am attempting to use the OleDbCommand.ExecuteScaler() function within my ASP.NET C# web page to perform a simple validation, but receive the following error: "Incorrect...
2
by: JMUApache | last post by:
Hi, I have got a error message while I run a sample OLEDB program. It says "Incorrect Syntax Near The Keyword Default Information". platform: Windows XP + Visual Studio 2005 Beta 2 + SQL...
2
by: Jon | last post by:
hi, i was trying to create a DB in a SQL Server. but when i try to connect it with : Dim conn As SqlConnection = New SqlConnection("Initial Catalog=master; " & _ "Data Source=SERVER-MACHINE;" &...
3
by: wallic | last post by:
Hello, This is my first post and I am a beginner with SQL code. The code below is supposed to update a new table (loctable) with a calculated value based on the original table (hra_data). ...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
1
by: itamar82 | last post by:
I am getting the following error: Microsoft OLE DB Provider for SQL Server error '80040e14' Incorrect syntax near the keyword 'WHERE'. for the sql below: SELECT TourId FROM (SELECT...
3
by: Skillman | last post by:
Hi Everybody, I'm beginer in SQL and was wonder if anybody could take a look at this and help me. I got this error, and not sure how to fix it. This is the error: Msg 156, Level 15, State 1, Line...
10
by: arial | last post by:
Hi, I am getting this error message: Incorrect syntax near the keyword 'where'. Description: An unhandled exception occurred during the execution of the current web request. Please review...
1
by: karenkksh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.