Using Microsoft Query and SQL Procedure to return data in multiple rows - sql

I have a stored procedure in Microsoft SQL that I am calling from Excel. I'm passing a single parameter and a value is returned. When I set this up in Microsoft Query, I type in my query using the SQL dialog, set up the parameter to pull from (for example) cell B4 and return the data to D4. From a SQL, passing parameters, return, etc. perspective, this works great. What's incorrect is the way the return values are placed in the sheet, and how I get it to work for all of the rows. So what I get back looks like this:
Image of what I'm getting now
What I really want is for the 11.45 to be returned in D4 (which is what I specified in Microsoft Query), without the header in C4. Further, I need to 'copy' this from D2 to D40, as I'd like to process the same procedure against all of the data in column B. What am I missing here?
Thank you!

MS Query is not designed to be run for each row in excel. You would have as many queries as lines in excel and would spam the server with multiple queries.
Your procedure could accept a lower and upper date and return data to a new sheet, and the current sheet can link to it, or you could generate the whole sheet from the stored procedure.
In any case, i can't see how calling a SP for each row is going to work.

Related

Best Method to Query SQL using Variable Excel Values

There's an SQL database that I would like to query through excel without having to pull the entire SQL database into excel (5Million + rows). I have established the connection in excel. The values that I will be using to query the SQL Database are variable (typically around 150-200 cells).
End Result: The variable cells in excel are all in column A, I would like to query the Column A SQL values to retrieve the Column B SQL value and pull them back into excel. I know I could download the whole SQL database into excel and do a vlookup but my excel file will undoubtedly crash with all the SQL data.
Does anyone know where I should start? Would this best be resolved through VBA code or the advanced editor directly in excel?
Cheers,
Brandon M
You can include "?" in the query text of your connection. The first time you run the query, Excel will ask you what each of the "?" references. You can then change the values in those cells, and refresh the connection to use those new values.
Your situation is a bit unclear to me.
Do you want to perform "Select * from table where column in (Cell A)"? and then to print into Cell B?
If yes, you can use VBA code to build your SQL query and select the data.
If you don't want to use VBA, you can use some cell concatenation to build the query and can pass the query to SQL.

SSRS conditional execution of dataset

I have two different datasets in SSRS report which gives different number of output fields.I have used this two data set into two different tables in report.
1) One table will display output at a time in execution based on condition.
Actually while running the report two datasets are executing the SP and it takes more time to display the output.
Requirement:
I need to execute the one dataset SP at time based on condition.Other Dataset SP should not Execute.
Example:
Dataset1 executes Sp1
Dataset2 executes Sp2
Table1 uses Sp1
Table2 uses Sp2
Normally while executing report Table1 will display output(Based on default parameter selection)
But SP1 and SP2 are executing on same time.so report takes more time to display output.
I need to execute 1 SP at a time based on condition.so that other dataset SP will not execute.
Step1:
First Create Dummy SP for Dataset .That SP should have same input parameters and same output fields as original SP1 but gives zero output rows.Do the same for SP2 for Dataset 2
Step2:
In Dataset properties select StoredProcedure Icon and in Fx column add below code
=IIF(Parameters!ManagerID.Value= -1,"SP1","DummySP")
Note: DummySP created should be same like SP1
Do the same for Dataset 2..and this works.
I have seen this answer, it's nice, but to create two SP's, it is sometimes very cumbersome. Sometimes the report uses a SP's many. This means that all SP's must be duplicated. And if you double the reports many... This means that they need to be maintained and improved throughout their lives.
Instead I have another solution:
Step 1: I create one SP, and use one of the parameters that already exists anyway.
Step 2: In the mapping of the DS\Report's parameters, instead of sending to DS the parameter sent to the report as it is, you can write an expression, which says that if I want to display the data of this SP I send the parameter sent to the report, and if I do not want to display this data, I will send different parameter , Which will cause the SP not to return data.
All this of course only if it is a procedure with parameters that allow the above. It is also possible to create a special parameter for this.
The following is a picture and example of an expression:
and the expression:
=IIf(Parameters!Display_xxx.Value=1,Parameters!yyy.Value,0)
You must control the visibility of tablix depends on the received parameter, in additional you can create parameters on both queries to control where execution to avoid the execution, like
Where #Condition = 1 AND ( Your WHERE )
As for that sack of performance, you don't want to run other SP at all, in this case, as per my experience, the best way is to modify your stored procs to add a new parameter based on what you are deciding what data set to call (means what data to pull).
So that, if the parameter says that the specific SP is not required, don't run the code in SP using IF-ELSE combo and return blank single row.
And then, on top of that, you can hide or show your tablix.
Hope that makes sense?
And if the column count and data types are same, you can always use an expression to decide what stored-proc to call.

Change select statement in SQL from Excel - end user

I have a decent size SQL statement that I have connected to an Excel worksheet and it runs fine. The question I have is would it be possible to have the end user enter a list of values in an excel sheet or somewhere else and have those values added to a WHERE clause in my SQL to limit the results per the users needs without the user having to go into connection and alter the SQL etc.? Thank you.
Yes. I do it as part of a VB script. I'm not a VB expert and I didn't set it up- I just know enough to tweak some changes that the users require.
Conceptually- provide an area in th Excel template for users to enter the parameters, use VB to get the values of those parameters, then pass them to the SQL statement.
You can create a Data Connection in Excel that utilizes a parameter. Whether you are calling a Stored Procedure on the DB or sending raw SQL, you can replace part of the statement with a ?. Tie that parameter to a specific cell. Now all your user needs to do is enter/change the value in that cell and it will re-query the database using that value as the parameter.
(I don't have Excel in front of me, otherwise I'd walk you through the exact steps)

SQL Data Represented in Excell (Cells out of Order)

Currently we are using excel to display our stored procedures for reporting.
I have been coming across an issue where the excel columns, once filled with data, are not in the same order as they're in the SELECT statement.
The CONNECTION STRING in excel, points to our database and the COMMAND TEXT is an execute call for the SP.
Its my understanding that excel will display the SELECT statement items in order of appearance.
Could someone confirm this or tell me if there is something I am missing?
Cheers
Skitlz
Make your ORDER BY sequence match the SELECT sequence and it should work, don't just match one, match them all.
I used to use SQL as an ODBC link inside excel and it was fickle at times!
Use the command ORDER BY "something in your table" in the end of your statement COMMAND TEXT and see if you get the results in the order of the "order by".

Excel 2010 & SQL Server stored procedure

I need a macro that, for each cell in a column, executes an SQL Server stored procedure which gets a parameter for a different cell in a different column (but in the same row).
i.e. Fill column B with a result of a stored procedure which get the parameter from Column A..
Examples:
B2 is the result of stored procedure which use the A2 value as input
B3 is the result of stored procedure which use the A3 value as input
B4 is the result of stored procedure which use the A4 value as input
B5 is the result of stored procedure which use the A5 value as input
Is there a way to do this without VBA?
Yes, you should be able do this without VBA by defining a connection to the SQL Server database and importing the data into your spreadsheet that way. You can use the Data Connection Wizard to create a connection to a table or view in your database, or to enter your own SQL statement to query the database, and add that data to a new worksheet.
Then, in the original worksheet, you can use a lookup function like VLOOKUP to find a match.
=VLOOKUP(A2, Table_Named_Range, Column_of_Result, 0)
Let me know if you need a more detailed explanation.
I end it up getting the table into the sheet and then using the LOOKUP function to send multiple cells value to get a specific value in the table
Check here for how to use the LOOKUP Function