I'm new to SQL Server and I'm using SQL Server Managment Studio 2012. I'm trying to do a very basic join, and I even copyed the syntax from an instruction video on PluralSight (using SQL Server 2008). Yet it does not excute.
This is the query:
USE [TestDB];
SELECT * FROM Cities JOIN Persons
This is the message:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Persons'.
The thing is when I use a "cross join" it seems to work fine with the expected results.
What am I doing wrong? And if I'm not doing anything wrong what could be the problem?
A join (other than a cross join) needs an on clause. Not knowing anything about your schema or how these two tables are related, perhaps you meant something like this:
SELECT * FROM dbo.Cities AS c
INNER JOIN dbo.Persons AS p
ON c.CityID = p.CityID;
Related
I'm new to Microsoft SQL Server 2014. I run this SQL code:
SELECT TOP(10) 'DBSG' as seek_entity, *
FROM DBSG..PM00200
and get this result:
Next, I want to find out total line items for that entity with code below.
WITH vw_pm00200_all AS
(
SELECT TOP(10)
'DBSG' as seek_entity, *
FROM
DBSG..PM00200
)
SELECT
seek_entity,
COUNT(*) AS total
FROM
vw_pm00200_all
GROUP BY
1
Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL?
It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count(*) will return the same result as Count(*) with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.
I am using Excel Connection to query customer contracts from DB2 for IBM i (AS400) through SQL Server connection and trying to join a SQL Server table to determine contract expiration date and sales team responsibility.
The AS400 query operates but I continue to receive an error on joining the SQL Server table ACCOUNT.dbo.CUSTOMER but can't find reference to alternate syntax on the join.
[select *
from openquery(
bpcsrpt_new,'
select s.SCID, s.SVER, s.CONTEXP, a.ACCTNAME, a.SALESTEAM
from AS400table1.contract c, AS400table1.subcontract s, ACCOUNT.dbo.CUSTOMER a
where c.cid=''Active''
and c.cid=s.scid
and c.cver=s.sver
and c.cid=a.acid')]
That's not going to work. When you use openquery, the statement gets sent to the remote machine. Obviously, ACCOUNT.dbo.CUSTOMER is not on the remote IBM i (aka AS400) machine.
You could use 4 part naming in the query directly
select s.SCID, s.SVER, s.CONTEXP, a.ACCTNAME, a.SALESTEAM
from IBMILNKNAM.IBMIDBNAM.IBMILIBNAM.contract c
, IBMILNKNAM.IBMIDBNAM.IBMILIBNAM.subcontract s
, ACCOUNT.dbo.CUSTOMER a
where c.cid='Active'
and c.cid=s.scid
and c.cver=s.sver
and c.cid=a.acid
Note however, the SQL Server will pull back the complete contract and subcontract tables to do the join locally.
Openquery is a better option if you're only interested in a few rows of a large table on the IBM i. If I recall correctly, something like so: (not tested)
select *
from (select * from Openquery(IBMIKNKNAM, 'select s.SCID, s.SVER, s.CONTEXP
from contract c
join subcontract s
on c.cid=s.scid
and c.cver=s.sver
where c.cid=''Active'')) as rmt
join ACCOUNT.dbo.CUSTOMER a on a.acid = rmt.cid
i'm having trouble figuring out why the following simple tsql update returned with an error
Incorrect syntax near '*='
SQL:
update unidb.dbo.result
set sra = 'A'
sra(char(1),null)
sra is not part of the primary key
If that UPDATE is the only statement that generate this error then you have triggers on dbo.result and one of these triggers contains this kind of join: *= which means LEFT OUTER JOIN (see section "Left outer join").
Solutions:
1) Set database compatibility level SQL Server 2000 (if you can; but starting from SQL Server 2012 - SELECT ##VERSION - the minimum database compatibility level is SQL Server 2005) or (better)
2) Replace the source code *= with LEFT OUTER JOIN.
I am trying to execute a very simple SQL query using "Microsoft Query". I can connect to the tables, select the columns I need but when I try to execute I get error msg saying "Incorrect Syntax near keyword 'IS'"
I got the SQL statement below through automated query but it just doesn't execute successfully. I think I know what the issue is. It is because my database catalog name is "IS". I tried executing same query on my other databases with different names and it works fine. Since I have access to several databases I need to specifiy which db I am accessing in my script and that's when it causes this issue. Is there a work around in my situation where I can avoid using database name and perhaps declare a variable?
SELECT Table1.id,
Table1.Name,
Table1.Status,
Table1.DateEntered
FROM IS.dbo.Table1 Table1
OR
SELECT * FROM IS.dbo.Table1 Table1 (Same error msg)
IS is a SQL reserved keyword, you have to wrap it with []
SELECT * FROM [IS].dbo.Table1 Table1 (Same error msg)
however, is a good practice - and error avoiding technique - to name tables without using reserved keywords or to always use brackets around tables name
I'd assume IS is a reserved keyword. Try wrapping it around square brackets:
SELECT Table1.id, Table1.Name, Table1.Status, Table1.DateEntered FROM [IS].dbo.Table1 Table1
I've just set up SQL Server 2008 SP2 on my workstation to run a local version of a database. The production database is SQL Server 2008 R2. A colleague is running SQL Server 2005 on his workstation.
My DB is throwing an error with the following code, while other instances of the same DB on the other servers run this query with no error.
WITH Posts AS (
SELECT TOP 10 *
FROM TBL_MSG_LATEST (NOLOCK)
WHERE TBL_MSG_LATEST.STATUS = 1
)
SELECT * FROM Posts (NOLOCK)
...throws this error:
Msg 215, Level 16, State 1, Line 6
Parameters supplied for object 'Posts' which is not a function. If the parameters are intended as a table hint, a WITH keyword is required.
Removing the (NOLOCK) after Posts makes my DB happy.
I'm not familiar with SQL Server, so I don't totally understand CTEs but I believe that this NOLOCK might not even be necessary here.
However, we aren't happy about making a change to the codebase just to satisfy my dev environment.
Is there a config difference with my newer DB environment? Is there a valid reason to remove the NOLOCK that I can't decipher from the error message?
From this page: http://msdn.microsoft.com/en-us/library/ms187373.aspx
Omitting the WITH keyword is a deprecated feature and will be removed
in a future version of Microsoft SQL Server.
So this will work.
WITH Posts AS (
SELECT TOP 10 *
FROM TBL_MSG_LATEST WITH (NOLOCK)
WHERE TBL_MSG_LATEST.STATUS = 1
)
SELECT * FROM Posts WITH (NOLOCK)
Don't know if it is a good idea or not or if it has any effect.
This appears to be specific to CTE syntax. Adding the "WITH" won't change how the NOLOCK works; it will just allow it to work.
With NOLOCK hints in CTEs, these both work:
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST NOLOCK
)
SELECT * FROM Posts NOLOCK;
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST WITH (NOLOCK)
)
SELECT * FROM Posts WITH (NOLOCK);
but this fails:
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST (NOLOCK)
)
SELECT * FROM Posts (NOLOCK);
These all work outside of a CTE:
SELECT ... FROM TBL_MSG_LATEST NOLOCK;
SELECT ... FROM TBL_MSG_LATEST (NOLOCK);
SELECT ... FROM TBL_MSG_LATEST WITH (NOLOCK);
The correct syntax is "WITH (NOLOCK)".
But ... if you're putting NOLOCK inside the CTE, why are you also putting it on the SELECT?
Note that the results are all the same in Denali CTP3.