Sql error on simple joins on table - sql

Below is my simple select statement with join which gives me error
Query :
select a.*,r.* from answers a join respondents r on r.id = a.respondentid
Error:
Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ' '.

I've had strange issues like this when copying and pasting queries from MS OneNote into SSMS 2014. The solution was to copy the query into notepad or outlook first and then back into SSMS. I'm sure long term my issue can be resolved by looking at the page encoding for where I store my queries.

Not sure, but probably what you can try is, close the current session and open new session and type your query manually instead of copy and paste from somewhere (try to write clean and clear query). Since, same query I was able to run
SQL Fiddle Example
==Update==
Looking at your error, which is pointing at Line 2. It seems there is some extra blank character, which probably causing this error.

Related

Error code BIG QUERY: Expected end of input but got keyword SELECT bigquery

Not sure why I'm getting this error code:
Expected end of input but got keyword SELECT bigquery
Screenshot of the query editor
I'm able to run the query but yet the error appears on the side which doesn't allow me to save view.
Any help would be appreciated, thanks!
The query editor tries to interpret the entire contents of the editor window as valid SQL. You don't have a terminator on the first SELECT statement, so the parser keeps going and throws an error when it sees the second SELECT keyword because, taken all together, the contents of the editor don't represent a single valid SQL statement. If you select the statement and run it, the parser only looks at the text you've selected. I suspect that's why you can run the first statement but the editor still shows that error.
Try putting a semicolon at the end of the first statement, as in:
SELECT * FROM covid-deaths-323215.covid_deaths.covid_deaths;. Note you probably also need a space between SELECT and *.
I got similar kind of error. The problem was that I was missing semicolon ';' between two 'SELECT' statements. When I put a semicolon at the end of first SELECT statement, the error was solved

ADODB connection refuses to connect due to missing operator

I'm opening an excel sheet via ADODB connection. The code has been working fine, until today, when it refused to connect to a new excel file.
The SQL string I am using is:
SELECT container_key,
price_catalogue,
portfolio_type,
portfolio_subtype,
yearly_fee,
yearly_fee_factor,
yearly_fee_2,
value_1,
advisory_agreement__advisory,
value_1_29,
3rd_party_key
FROM [temp0$]
Now I get an error message from VBA when i am on the line that is supposed to open the recordset:
Syntax error (missing operator) in query expression '3rd_party_key'
I have verified that the field exists and is named the same in the spreadsheet. The syntax looks correct to me as well. The spreadhseet is extremly large (360+ columns) and the field that is refrenced in the error message is the very last column. Might this have somethingto do with the issue I am encountering?
Can someone point out what the problem might be?
UPDATE:
I have tried putting all field names in parentheses. While this worked in not producing a run time error and it didn't work either. Finally I tried reducing the number of fields and went one by one. The following 3 fields cause the errors:
advisory_agreement__advisory,
value_1_29,
3rd_party_key
If i remove these fields, I can open the recordset without a problem. I suspect the name conventions of these fields causes a conflict somehow.
I'd try it column by column, with square brackets, to see which column is actually causing the problem. It might seem a bit tedious but this way you will know exactly where the problem lies.

Database backup taking issue

I am using Sql Server 2005.
I wanted to take back up of my database through simple sql command.
For this I referred THIS document.
Made command as follows:
backup database webbasednewsoft to disk 'e:\wb.bak'
but its giving me error as:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'e:\wb.bak'.
I dont understand what is syntax error in this?
I have made everything according to doccument.
I thought the issue was for escape sequence, hence i also tried,
backup database webbasednewsoft to disk 'e:\\wb.bak'
But the error was same.
Please guid me for this.
You need an = sign.
backup database webbasednewsoft to disk='e:\wb.bak'
See here for the MSDN for the BACKUP command
And if you look at your reference it too mentions using an = sign! but hey we're all guilty of eager reading I know I do it... :-)

Syntax error on executing a script in Sybase database

I created a Sybase database emp_details using SQL Anywhere and Sybase Central. I had given emp/emp as dba username/password while creating.
The db got created and the files were generated in the given folder.
When I tried running the below script using Ineractive SQL:
use master
go
if exists (select 1 from master..sysdatabases where name='emp_details')
checkpoint emp_details
go
It threw the following exception
Could not execute statement.
Syntax error near 'checkpoint' on line 2
SQLCODE=-131, ODBC 3 State="42000"
Line 4, column 1
Haven't been able to figure out what exactly the syntax issue is and have been stuck up with this for a while.
Any ideas?
First of all, you may want to think about posting your SQL Anywhere questions to the http://sqlanywhere-forum.sap.com/ forum. It's a forum dedicated to the SQL Anywhere product line.
Is there any possibility that the two periods together might be causing your syntax issue?
Normally you're not going to get an exact area where the error is coming from. See if that helps. Also check out the other forum as well.

Creating Stored Procedure Syntax, relating to use of GO

Does anyone know why.
CREATE PROCEDURE My_Procedure
(#Company varchar(50))
AS
SELECT PRD_DATE
FROM WM_PROPERTY_DATES
WITH (NOLOCK)
WHERE PRD_COMPANY = #Company
GO
Gives me an error message in SQL management studio:
Msg 102, Level 15, State 1, Procedure My_Procedure, Line 1
Incorrect syntax near 'GO'.
Now, this is the last statement of a batch, maybe the last statement should not have a GO ?
If you go to "Save As...", click on the little down-arrow on the Save button and select "Save with Encoding..." then you can set your Line endings to Windows (CR LF). That seems to have resolved the issue for me...
There was a bug released in SQL Server that parses the GO statement incorrectly. I believe it was introduced when you could do GO X, and execute the batch X multiple times.
Sometimes I've had to add a comments section ("--") to force the parser to terminate rather than produce a syntax error. This has been seen when I've had 400,000 lines in a batch of code.
Example:
PRINT "This is a test."
GO --
PRINT "This is not a test."
GO 5 --
The sql you currently have in the question will work properly. The unformatted sql you had before Kev edited the post won't. The reason is that you had the GO on the same line as the sql. It needs to be on a separate line.
If you copy-paste text from text editor with Unix/Mac EOLs (e.g. Notepad++ supports this) the GO is interpreted as being on the same line as the last TSQL statement (yet on the screen you can see newlines normally). Converting EOLs to Windows (CRLF) in the text editor fixed the problem. Very tricky though.
Error for this sql
ALTER PROCEDURE My_Procedure
(#Company varchar(50))
AS
SELECT PRD_DATE
FROM WM_PROPERTY_DATES
WITH (NOLOCK)
WHERE PRD_COMPANY = #Company GO
is
Msg 102, Level 15, State 1, Procedure My_Procedure, Line 7
Incorrect syntax near 'GO'.
note the Line 7, original question has Line 1.
If I put the GO on its own line SQL works fine.
Given that your error message says Line 1, it would appear that for some reason there isnt a correct CR/LF happening in your sql.
You can certainly have GO at the end of your batch. I see nothing wrong with this code per se. Put a semicolon after #Company.
I tried this SQL on my 2008 server by creating a table WM_PROPERTY_DATES and adding 2 columns, PRD_DATE and PRD_COMPANY.
Work just fine and creates the proc. Maybe you can try putting your code in a BEGIN...END block and see if the issue persists.
Raj
You said
Now, this is the last statement of a
batch, maybe the last statement should
not have a GO ?
This implies that these lines are all part of the same batch submitted to SQL. The thing is, a CREATE PROCEDURE (or CREATE FUNCTION or CREATE VIEW) statement must be the first statement in the batch. So, put a "GO" line in front of that CREATE statement, and see what happens.
Philip
In my case I had copied part of the code from a webpage and it seems that saved the page with different encoding, I tried SaveAs from SMS with different encoding, but didn't work.
To fix my issue I copy the code into NodePad, then save it in ANSI format and re-open the query
No serious company could possibly pretend to add GO after each statement. Perhaps after each batch.
GO is not a Transact-SQL statement. Is a delimiter understood by tools like ISQLW (aka. Query analizer), osql, sqlcmd and SSMS (Management Studio). These tools split the SQL file into batches, delimited by GO (or whatever is the 'batch delimiter' set, to to be accurate, but is usually GO) and then send to the server one batch at a time. The server never sees the GO, and if it would see it then it would report an error 102, incorrect syntax, as you already seen.