I am using Sybase and it complains the following has a syntax error:
DELETE
*
FROM
table1 INNER JOIN table2 ON table1.some_col = table2.some_col
The specific error is: Incorrect syntax near the keyword 'inner join'.
I've checked online and this is how people go about it, but mine refuses to accept this.
Could someone please tell me what is wrong?
assuming you intend to delete from table1 based on a join with table2 the following
DELETE table1
FROM
table1 INNER JOIN table2 ON table1.some_col = table2.some_col
According to Sybase's Manual, it should be like this:
DELETE
FROM table1
FROM table1, table2
WHERE
table1.some_col = table2.some_col
Related
I have 2 tables in SQL Server that I am trying to make a left join from so that all records from table1 are shown and any data from table2 is shown if it exists. They are as follows
Table1
id Customername Jobid
--------------------------------
2754444 Jones 123
2854233 Smith 234
Table2
key Location
-----------------------------
FD#2754444 London
FEC#2854233 Liverpool
I can get an inner join query to work as below - but I obviously get only matching records, (which I dont want - I want all records from table1 and any matching values from table2)
This works:
$query = "select distinct table1.id, table1.customername, table1.jobid, table2.location, table2.[key]
from table1
inner join table2
on table1.id= RIGHT([table2].[key],7)"
So changing it to a left join:
This does not work:
$query = "select distinct table1.id, table1.customername, table1.jobid, table2.location, table2.[key]
from table1
left join table2
on table1.id = RIGHT([table2].[key],7)"
It does not return any of the table2 data. Any advice on what I am doing wrong would be very welcome.
Thanks in advance.
I put together a SQL Fiddle to show that your query should work (based on a guess about datatypes). Given that you've wrapped your queries as strings, that raises the question of whether your problem is actually with SQL, or if the ODBC (or whatever) connection is actually returning a parser error rather than a result set. Have you looked at what the db is providing in return? Have you ensured that there is whitespace between each word, even for line breaks (copying your text as-is shows CRs and LFs, but check your code); otherwise, it's quite possible that you're sending SQL Server something like "SELECT * FROMTABLEWHERETHING" rather than "SELECT * FROM TABLE WHERE".
Thank you all for your input. For some reason the RIGHT was not returning anything so I managed to resolve with :
left JOIN [table2] ON [table1].id= substring([key],(CHARINDEX ('#',[key] , 1)+1),7)
Thanks for all your responses.
Jim
I have to convert an Access query to Sql Server query.
In my FROM clause I have something like this:
FROM
(table1 INNER JOIN table2 ON table1.afield = table2.afield)
I'm think that my FROM clause in Sql Server should remain unchanged but I'm not sure about that.
Can you please give me confirm about that?
You can use what you have, I'd take out the parenthesis though;
FROM table1
INNER JOIN table2
ON table1.afield = table2.afield
It would also be a good idea to get into the habit of using table aliases too, something like this;
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t1.afield = t2.afield
As this will help the readability of the code.
I'm new here so if I do something that breaks convention please let me know.
I am trying to create a simple query that includes a single outer join before I move onto a larger and more complicated query. When I run my query before adding the join it works as expected. When I add the join the query returns an error message. I cannot determine the cause of the error. I am working in Microsoft SQL Server 2008 R2.
This query runs successfully and produces the expected results:
use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,
table2
where table1.id_num = table2.id_num
This query does not run successfully:
use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,
table2
where table1 left outer join table2 on table1.id_num = table2.id_num
The error message I get is:
Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near 'left'.
I also tried "outer join" instead of "left outer join", but this produced a similar error message (the only difference was "near 'outer'" instead of "near 'left'").
I do know what boolean means but I am not sure why it's an issue here. The boolean operator comes later in the same line. My code looks like it follows the same format as other code I have seen. Any assistance would be appreciated.
your join needs to be after the FROM not in the WHERE clause
use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1
left outer join table2 on table1.id_num = table2.id_num
You are using deprecated syntax, listing out tables and defining their relationship in the WHERE clause has been replaced by explicit JOIN statements
FROM Table1 t1
JOIN Table2 t2
ON t1.col1 = t2.col1
LEFT JOIN Table3 t3
ON t1.col2 = t3.col1
Old style joins do allow for outer joins:
use MyDatabase
select table1.ID_NUM,
table1.LAST_NAME
From table1,table2
where table1.id_num *= table2.id_num
This old syntax should be avoided, but helpful to know what they are if you come across them.
I've got an Access application with an update query with the following syntax:
UPDATE TABLE1, TABLE2 SET
TABLE2.VALUE1 = TABLE1.VALUE1,
TABLE2.VALUE2 = TABLE1.VALUE2,
TABLE2.VALUE3 = TABLE1.VALUE3,
TABLE2.VALUE4 = TABLE1.VALUE4
The query is working but I do not understand what's going on here.
I'm trying to convert this query to SQL Server.
Can somebody please explain what this query does? My guess is that it's a special Access syntax.
Thanks,
Sven
It uses the older implicit JOIN syntax, although SQL Server should understand that syntax too.
It's INNER JOINing table1 and table2, then moving the values from table1 to table2. Because of the lack of JOIN conditions, if table1 has more than 1 row it may have unpredictable results.
Essentially it is:
UPDATE Table1 INNER JOIN Table2 <<ON Missing Conditions Here>>
SET Table2.Value1 = Table1.Value1
Table2.Value2 = Table1.Value2
Table2.Value3 = Table1.Value3
Table2.Value4 = Table1.Value4
You can convert this to SQL Server with something like this:
UPDATE Table2
SET Table2.Value1 = Table1.Value1
Table2.Value2 = Table1.Value2
Table2.Value3 = Table1.Value3
Table2.Value4 = Table1.Value4
FROM Table1 INNER JOIN Table2 <<ON Missing Conditions Here>>
Every field from TABLE2 will override corresponded field from TABLE1 with records from TABLE1 one by one. Result will be TABLE2 with all replaced records by last row from TABLE1. If TABLE1 has no records - no changes happens.
Sorry for my english.
And... it is SQL.
Try to avoid the "UPDATE with join" syntax in SQL Server. It is completely non-standard SQL but more seriously it gives unpredictable results without any error or warning if the joining criteria is not correct. Use the MERGE statement instead or use the standard version of the UPDATE statement (with a subquery) if you can.
SELECT * FROM table1, table2
WHERE table1.user_id = table2.id
AND table1.content = news
AND table1.content_id = 1
that wont work. cant u have two "AND" in a sql statement??
//Tomek
you probably want to quote 'news' as a string...
You also probably want to use an inner join instead (much more efficient)
SELECT * FROM table1
INNER JOIN table2 ON table1.user_id = table2.id
WHERE table1.content = 'news'
AND table1.content_id = 1
news is what? Some sort of parameter? Value of the field? A word which should occur in the field? Most likely your syntax is wrong, see W3Schools WHERE and W3Schools AND/OR operators pages for more information.
let me rewrite that for you with a JOIN statement since it is not 1995 anymore, you also need quotes around news
SELECT * FROM table1 t1
inner join table2 t2 on t1.user_id = t2.id
AND t1.content = 'news'
AND t1.content_id = 1
First let's start with getting rid of that old-style join so that the join and where clauses are clearly separated to make understanding much easier.
SELECT * FROM table1 t1.
JOIN table2 t2
ON t1.user_id = t2.id
WHERE t1.content = news
AND t1.content_id = 1
Now let's discuss what the problem might be. First what error are you receiving as this could be perfectly acceptable syntax? Could the problem be that you have not noted which table news is to come from? If it is in both tables you could be getting an error there. If news is meant to be the value of the column you would want it to be 'news' instead of news. It could also simply be that no records meet your conditions.
Further it is a bad practice to ever use select * and it should never be used in a join as you are returning at least one field twice and that is wasteful of both database and network resources. Always specify only the columns you need. This wastes lots of resources every day when lazy programmers do this in every query.
What do table1 and table2 look like? Can you paste a create script?
I would STRONGLY recommend to use the proper JOIN syntax:
select * from table1
inner join table2 on table1.user_id = table2.id
......
what datatypes is "table1.content" ?
You can most definitely have a lot more than just 2 AND statements in a SQL query - any database that really support any of the SQL-xx standards will support this...
I liked cmartin's response. Also, you can use more than one AND, if needed.