Excel cell value as SQL Server Connection parameter - sql

I'm trying to get SQL Server 2012 data into Excel 2013.
You can see my sample command text. If I write date parameter manually, like this:
set #dt = getdate()
it works well. But I need to pass it the Excel cell value, for example A1 value as shown in the picture. Is it possible? If yes, how?
Please note: I do NOT want to use vba macro connection, need to pass it via this form only.

Jeroen's answer applies to SQL Queries also, but it's not obvious how to implement them, since Excel doesn't appear to expose the option in advance, and the 'parameter' button is grayed out.
Write the query with question marks in place of the value to be used in the query:
SELECT * FROM dbo.TABLE AS T where t.Date = ?
Upon saving the query, a prompt appears that lets you select a cell or range to use as the value in place of the question mark.

Related

Excel multiline cell import in SQL Server via ADODB

I have a field in Excel with comments. People enter data with line breaks in a cell (ALT + ENTER).
Is there a way via Excel VBA to import this via ADODB to SQL Server 2016, so I can also retrieve it again as multiline from the database?
An example: in Excel I have a multiline cell with value:
TEST
TEST
TEST
When I retrieve it again via the database it will show TESTTESTTEST, I would like to show the same as in Excel.
I tried both
rs![comment] = cell value
and
t-sql (simplified): insert into comment_table (comment) VALUES cell value
In both cases when I retrieve the data, I only get one string like in the example.
Hope somebody has an idea to solve this.
Already found the solution.
Field has to be of type nvarchar. If I query the table in SQL Server, I only see one string.
TESTESTTEST
If I for example retrieve it in Excel in a pivot and set the field to wrap text, it will apply the line breaks again.

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.

Excel - Off Page Reference to Microsoft Query

I am utilizing Microsoft Query in Excel to tap into an ERP table structure like Crystal would do.
In writing the SQL, is there a way to have a filter pulled from the active Excel worksheet that is embedded in the SQL instead of prompting and editing the query?
My main problem is a Like [Prompt]% in the Excel GUI for the users to change like order numbers.
Is it possible to do an off page reference from MS Query to Excel?
If by "Microsoft Query", you're talking about the window that looks like it was coded for Windows 95, stop using it. This is provided for retro-compatibility.
Anyway, if you've displayed the criteria bar in MS query, you can type a name between brackets e.g. [Something] and MS query will prompt you to fill a value.
Not what you want yet but getting close. When you return to Excel and refresh the query, the prompt will now offer you the possibility to use a cell instead of a value you need to type every type.
In the more modern connection utility accessible via menu data > Connections (+ available even if you created your table via MS Query btw), you can achieve that by using question marks in the WHERE clause.
For instance, instead of SomeField = 'SomeValue', write SomeField = ?
Then, click on the Parameters button and you'll see all the parameters you've set, each of them can be attached to a cell's value.

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)

Excel SQL data import parameter issue

I tried to find the answer myself but not knowing how to word the question caused problems :).
I have an excel workbook that I use to pull data from SQL Server 2005 using a stored procedure that accepts a parameter. I am using Microsoft Query in Excel. I am trying to get Excel to grab the parameter from a cell so that the users will not have to edit the connection. If I were to do this as SQL, I would replace the value with a ? and point it to a cell without issue. Since this is a SP, I get a strange response.
This works:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] .07
This:
exec [GTI_mainframe].[proc_mf_forecast_authorizations] ?
Gives me the following message box:
[Microsoft][ODBC SQL Server Driver]Invalid parameter number
I control the SP and the excel workbook so can impliment what I need to. I had one person suggest a vba approach to reference the cell value directly. I could do that but my choice would be to not have to do it in macros. I would love to just be able to use the "refresh all" from the data tab in the ribbon bar. Any thoughts?
I recommend you XLReport; it allows you to connect to almost any database, create queries with parameters.
You can download the trial here.