SQL Server fetch next x number of rows with where clause - sql-server

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

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.

prosgres sql of using limit and fetch

There are many solutions to the above questions from which i checked and tried but mine is always giving an error
I had some code converted from postgres to sql server but facing some issues
select * FROM errors OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
the above in sql server
converted from posgres as:
select * from errors offset #start# limit #total#; works
but not in sql server, what should i do there
In SQL Server, OFFSET...FETCH is part of the ORDER BY clause. Ordering is needed with OFFSET because result set order is undefined without ORDER BY; the same query could return different results even though the underlying data is the same.
Below is example syntax, guessing an an appropriate column name.
SELECT *
FROM Errors
ORDER BY ErrorTimeStamp
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

SQL server linked server to PostgreSQL query conversion of TOP clause is not converted

I created a SQL linked server to connect to PostgreSQL 11, and issue a query like this
SELECT TOP 1 *
FROM [LinkedServer_PostgreSQL].[DBname].[dbo].[TableName] a;
It is very slow and seems to take forever, I ended up killing the query every time. Upon investigation, I found the query on the PostgreSQL server is selecting all rows, the TOP N row clause on the PostgreSQL is not converted. That's why the query cannot finish, because the table has more than 10 million rows.
As I cannot replace TOP clause using LIMIT clause on SQL server. This really beats me. Help, anyone?

Invalid collation "utf8_general_ci" in SQL Server SELECT statement

I am trying to running the below query:
SELECT TOP 1
*, (Title COLLATE utf8_general_ci)
FROM
BibTitles
WHERE
TitlesID > '.$last_iterate_book_id.'
ORDER BY
TitlesID ASC;
in PHP script. The above query is to get data from a SQL Server view.
When I am running the script, it is throwing the below error:
Invalid collation 'utf8_general_ci'
I am using the collation because, my database contains data in different language and showing me those data as question mark (???) instead of actual data. I can't alter the database as well as table due to permission issue.
Please help me with this.

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