I have the following code:
select order_number,received_date
from order_head
where order_head.order_number not in (select order_number from csa_log group by order_number)
and ordernature in ('02','03')
and received_date > '01.01.2010'
and buyer_code = 'GAP'
group by order_number,received_date
order by received_date desc
When run in crystal reports does not pull the same data as sql server express. It is pulling only one record as opposed to 7. Anybody have some insight? Thank you in advance. (I have a suspicion it has something to do with the nested statement).
Several possibilites come to mind.
First are you absolutely sure that Crystal Reports and the database you are querying are the same one. Amazing how often one is querying dev and another prod when you have these kinds of issues. Even if you don't believe this to be the case, check this anyway.
Permissions could be another reason for the discrepancy.
Finally confirm that the two queries are exactly the same. Use Profiler if possible to confirm this.
Usually I use SQL server profiler to check what SQL Statements Crystal is running behind the scenes. In your case is not possible because you are running SQL Server Express.
In Crystal you have the option of checking the actual SQL statement that Crystal is running by selecting "Database" and "Show SQL Statement" in the top menu.
Make sure that under Reports menu you are not putting any conditions in the Select Records.
Check that you are showing the fields in the detail and not in the header or footer. That is a common mistake.
Related
I need to find out which of our Crystal Reports users are actually running, so we can get rid of those that are no longer being used. I can do some kind of query of the latest jobs run on the SQL server as described here:
How to get the last run job details in SQL
However I'm not sure how I could tie that back to the actual reports. I've tried opening a Crystal Report in Crystal Reports 2008 while running a trace on the SQL server using SQL Profiler, however I don't see any database calls in the tracethat would allow me to determine the name of the report being run.
How could I find out which Crystal Reports are actually in use?
I usually embed a unique identifier (the report's name or ID) in either a sql-expression field or the command's query.
If the DBA finds an issue with a given query (e.g. non-performant), this approach easily identifies the source.
SQL-expression:
//{%report_name}
(
'Daily Obstetrics Review [OB003]'
)
In the command:
SELECT 'Daily Obstetrics Review [OB003]' AS REPORT_NAME
...
FROM ...
I have had an odd error I cannot explain. Basically, I am running a query to my SQL database using excel and am having non-existent data pop up when it comes to a very particular order in my database.
Here is a simple query surrounding this order:
select * from OR200100 where OR200100.OR20001='0000793605'
Here is the output in EXCEL
And here is the same output in SQL
what is happening here? How could the same query generate 2 different results?
Run SQL Server Profiler against the database if you can, then compare the output to the sql query that you are running in ssms.
OK, so it's SQL Server then, that's important because different SQL products can have very different idiosyncrasies and controls.
The next things to check are these:
Is OR200100 a Table or a View? If it's a view then post it's code.
Are you using the same Login/account from both Excel and SSMS?
Are you sure that you are connecting to the same Server and Database? SSMS tells you what you are connected to, but client apps like Excel do not and it is very common for this type of problem to be caused by the app connecting to a Dev or QA version of the database. See here for some of the different ways that this can happen:
So I had a very similar problem, my query was grouping by week numbers. What I found was that one of the queries had set datefirst 5 set whilst the other didn't. I guess the key thing here is make sure, if you are using any SET operations in your ssms queries, these are identical to those in the Excel query string.
My report calculates the stock of inks in my stores. I built a SQL statement in VB.NET and got the correct results. How can I display these results in my Crystal Report? Or design a report such that same results will be retrieved?
I tried to use SQL Expression builder but failed.
Usually you will link Data Tables to your report using Database Expert. Do the same in Database Expert, select the Database and you can see Add Command. Select that and click the > button. You will get a Window, write your SQL Query there and press OK.
After finishing this you can see Command in Database Fields which contains all your records got from the SQL Query as result.
In Database Manager choose Add Command and then type your SQL Statement there.
I am trying to update a #temp table from a linked server using a top clause but am getting a very odd result - it seems to be ignoring the order by in my code.
I can fix the specific problem programatically but would like to know if this is a one off issue or there is a general problem with the way i have linked the servers.
The query is being run on a 2005 SQL Server (9.0.3042) linked to a 2008 SQL Server R2 (10.50.279) linked via the Microsoft OLE DB Provider for SQL Server.
The query looks like this - i have already created #TempTable which has the columns Id, Date and PrevDate and inserted data into the Id and Date columns.
update #TempTable
set PrevDate =
(select top 1
d.Date
from
linkedserver.DB.dbo.Date as d
where
d.Id = #TempTable.Id
and d.Date < #TempTable.Date
order by
d.Date desc)
The select is not picking the top 1, it appears to be picking the first date entered into the table for the specific Id and ignoring the order by clause.
When I just do a select, it works fine
When I put in the PrevDate via the inital insert, it works fine.
When I use a Max rather than a Top, it works fine.
When I run the exact same query/scenario on the 2008 SQL Server via a link (same provider) back to the 2005 SQL Server, it works fine
I am not looking for a fix for this specific query but would like to know if this is an isolated incedent or some fundamental problem with 2005 to 2008 linking that is going to manifest itself in very hard to find ways.
Thanks,
Tim
I have some bad news. I have replicated this issue here. It looks to me to be an issue with SQL Server 2005's query plan (I tested with a later version too, v 9.0.4035):
Image: Query plan for update statement
(sorry I can't embed images here, not enough reputation)
The details for that remote query:
Image: Remote Query details
There is no ORDER BY statement sent to the remote server, so the results that come back are indeed just the TOP (1) in an arbitrary order, which is likely to be in the order of the primary key. It seems to me that SQL Server should have emitted an ORDER BY Date desc there.
Therefore, I have to conclude that other queries like this, in this environment, could easily have the same issue (so, not an isolated incident).
By the way, there is a Microsoft Connect issue already raised for this one, from 2009: connect.microsoft.com/SQLServer/feedback/details/446017/missing-order-by-in-remote-query-with-update-statement
i have a view that is using linked server to retrieve data from a remote server in SQL Server. On each time viewing the view, the results returned are vary. For example, 1st time execution may return 100 rows of records but on 2nd time of execution, rows returned are 120 rows. Any ideas what is the cause?
I have witnessed odd linked-server results that are a product of non-determinism written into the SQL itself, I.e. a TOP query written without an ORDER BY clause.
This problem, for example, where the chap had multiple non-unique foreign keys coming from a table source on the left hand side of a linked-server INNER JOIN, and wanted 10 rows from a remote sub-query to the right, where the end result was restricted to 10 rows itself, when it should have been greater than 10 rows.
Should definitely give your SQL a quick eye for such curiosities.
The data on the linked server changed between executions?
Is your SQL Server fully patched? SQL Server 2008 and 2005 both have bug fixes out related to incorrect query results from linked servers.
Here is one example:
969997 FIX: You receive an incorrect result when you query data from a linked server that is created by using an index OLE DB provider in SQL Server 2005 or in SQL Server 2008
Is the linked server also a SQL Server? If not, perhaps a buggy driver? I've seen odd results, for example, due to an old Informix ODBC driver. Are you able to run something akin to SQL Profiler on the linked server to see what command it's receiving?
I'm not sure what the answer is, but (assuming that your counts of 100 and 120 are accurate) can you not capture the data from the two runs and compare it? That might give you some clues as to what's going on. For example, is it completely different datat, or is it duplicate rows (in the 120 row batch).