Incorrect syntax near '*='? - sql-server

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.

Related

Microsoft SQL Server: Error with Group By

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.

SQL Server fetch next x number of rows with where clause

Microsoft SQL Server 2008 R2
I am running a large SQL select query that may take hours to complete. So I try to break the query results into smaller sets.
e.g return results 1-10,000 first, then 10,001 - 20000, and so on
I used below code, but it gave me error
SELECT *
FROM PP_ConsolidatedSalesView
WHERE financial_period = '2018-11'
ORDER BY id
OFFSET 10000 ROWS
FETCH NEXT 10000 ROWS ONLY
I use a loop to dynamically change the offset and fetch next values.
The error message is:
Incorrect syntax near 'OFFSET'
Does anyone have an idea why? And is there an alternative solution?
Can you please confirm the database compatibility level. Offset is present in SQL Server 2012. If database is 2008 compatbility mode, then keyword isnt available.
You can check it like below:
USE AdventureWorks2012;
GO
SELECT compatibility_level
FROM sys.databases WHERE name = 'AdventureWorks2012';
GO
More info here: Incorrect syntax near OFFSET command

Azure SQL server IN statement not throwing error

When I write a sql statement containing an in clause, if the column in the in statement does not exist it does not throw an error, but just ignores the in statement and returns a resultset.
Query below will return all rows from T1
SELECT * FROM T1 WHERE Id IN(SELECT Id FROM T2)
Is that the correct behaviour for SQL Azure?
My local MS SQL server throws an error saying the column does not exist.
Frankly, what you are describing is impossible. If the column inside the IN statement doesn't exist, Azure will throw an exception. A result set cannot possibly be returned. Currently there is very little difference between the way SQL Server executes a query and the way Azure v12 does. Each may generate a different execution plan, but essentially there is very little difference between the two.
This is expected behavior, same as in Sql Server. Basically, if a column is referenced in a subquery that does not exist in the table referenced by the subquery's FROM clause, but exists in a table referenced by the outer query's FROM clause, the query executes without error. SQL Server implicitly qualifies the column in the subquery with the table name in the outer query.
This is documented here:
https://technet.microsoft.com/en-us/library/ms178050(v=sql.105).aspx

Is Join syntax different in SQL Server 2012?

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;

How to fix "domain error" in SQL Server 2005 when using LOG() function to get product of set

I have a inline select statement to calculate the product of the set of values.
Since SQL Server 2005 doesn't have a built in Product aggregate function, I am using LOG/EXP to get it.
My select statement is:
(select exp(sum(log(value))) from table where value > 0)
Unfortunately I keep getting the following error:
Msg 3623, Level 16, State 1, Line 1
A domain error occurred.
I've ensured that none of the values are zero or negative so I'm not really sure why this error is occurring. Does anyone have any ideas?
One of the features of the query planner introduced in SQL 2005 is that, in some circumstances where the table statistics indicate it will be more efficient, the WHERE clause of a statement will be processed after the SELECT clause.
(I can't find the Books On-Line reference for this right now).
I suspect this is what is happening here. You either need to exclude the rows where value = 0 before carrying out the calculation - the most reliable way being to store the rows you need in a temporary (#) table - or to modify your query to handle zero internally:
SELECT EXP(SUM(LOG(ISNULL(NULLIF(VALUE,0),1)))) AS result
FROM [table]
The NULLIF\ISNULL pair I have added to your query substitutes 1 for 0 - I think this will work, but you will need to test it on your data.

Resources