Linking the Sql Database to Excel using a query - sql

I am trying to bring data from SQL server using a Microsoft query the result is displayed correctly in the preview but once I close the Microsoft query window and apply it to the sheet I get the error: "problem obtaining data" if I pull it as a pivot table ...and it doesn't populate if I pull it as a table it just writes "Query from Data_Source" in the selected field and runs the query in the background and stays as is when that's finished instead of showing me the table result. 
pivot table error
table pull error
can anyone please help me figure out what I am doing wrong ?

I have made the Query into a Stored procedure, it is working well now, Thank for the help !

Related

Fetching Query Result Directly Into an Excel Cells

I am currently using Oracle Apex to write queries and copy and paste the results into an Excel file. I have also used mySQL Workbench in the past.
My question is it possible to link a database to Excel and produce the results? For example in cell A1 I would want to type "select sum(sales) from accounts" and when I click enter, the result would appear in that cell after fetching the query?
I may be over thinking it or expecting something that is not possible. Any help is appreciated
You can connect your excel with a database and pull the data to the sheet, but writing queries in an individual column still need to figure out. In between, you can take a look at the ODBC configuration to connect excel with the database, Hope this will give some insights.
https://www.devart.com/odbc/mysql/docs/excel.htm

Table missing in a database but query works

I have a table TBLT_GLDET in my SQL Server 2008 database. When I open a new query window and select the right database and execute following query
select *
from TBLT_GLDET;
the data appears. But in the tables list the table does not appear.
Also when I try to open the table for a Crystal Report using database expert in Visual Studio, it says the table is missing in the database.
What is the reason for this?
I did following things. I double checked whether it's the right database.
I refreshed the database.

SQL View displaying incorrect data while the same sql code run in a query window gives correct data

One of my SQL view when opened and run in a design window gives incorrect data while the same underlying SQL code is run in a new Query window gives correct data. To be more specific, this view is pointing to a table where data changes on a daily basis. Why am I getting stale data in the design view while the same (same view underlying sql) code run in a Query Window gives current data. This has never happened before. There is a excel sheet that connects to this view to pull data into reports and this is getting the stale data into the report.
What should I be looking at to fix this issue.
And the server is SQL Server 2008 R2 (SP1)
Any help is appreciated. Thanks in advance.
You probably use * in your view to select all columns from some table
select * from table
when you add a new column to that table you'll need to re-run the view, then it'll work... select *, especially in view is generally considered a bad practice. In the case of a * metadata is not automatically updated when the tables (or schema for that matter) used in view are modified, therefore you'll need to alter the view
another way of refreshing it is running system stored proc:
EXEC sp_RefreshView vw_ViewName

Add exsiting query to report

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.

Crystal Report Duplicating in details?

so I have a template in Crystal reports using vb.net, I changed my data source location and that source still contains the needed tables for my report, the problem is this... My original template displays the exact report but when I changed its data source it duplicates the data in the details section(e.g I made a query that will show 1 item only,but instead I get 4).
Here is a screenshot of my problem:
(original at http://tinypic.com/r/5by6ih/5)
I want to alter the template rather than to make another one
This is normally down to an issue with the underlying SQL query. If you open the the report in Crystal (or visual studio) you should be able to "Show SQL Query". Run the query in Query Analyser or SQL Server Management Studio and look at the result.
If the underlying reason isn't then immediately apparent please paste the resulting output.