Best Method to Query SQL using Variable Excel Values - sql

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.

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.

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

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.

find column name in sql query

I would like to use the GUI to find q column name in my sql query which has more than 300 columns
For example: I want to use GUI provided by SQL to find all the column names starting with VENDOR and hi-light them in order to make my operation easier..
I just want to use GUI
P.S: No query suggestions please, I am already aware of them (using like and all)
In MSQL Studio, use the Query Designer (Design Query In Editor...), paste your SQL, then use the name column in criteria pane to copy and paste in excel

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)

What could be SQL Query of a data source that is a spreadsheet, to be returned to a seperate spreadsheet? Including UDFs in the query?

I currently have a data source of a large table, sitting in workbook1. From workbook2, which is currently empty, I wish to set up a DSN connection to workbook1, so that I can query it from workbook 2.
In the SQL query result, I wish to display extra columns which are calculated using User-Defined VBA functions, the arguments of which will be other fields from the data source.
Example:
Workbook1 is Field1, F2, F3 and F4. I wish to query this and display all records, but additionally I wish to have F5=UDF(F3,F4).
I have been advised already that the solution to this is:
SELECT UDF(F3,F4) as F5
FROM \SourceWorkBookLocation\SourceWorkBook
IN ACCESS:
The problem I am having in access is not at the top of my list right now, relates to data types and trying to determine if a number in a string is <25. But the main problem is in MS Query:
IN EXCEL/MS QUERY:
The function is just not recognized; "undefined function"
I am not sure how to get it to see the function? My end goal here is to build a front end in excel, and have vba querying appropriately using user input variables passed to the queries. The querying will be done on a separately updated workbook.
Any ideas on how to get MS Query to see my UDF and accept what I am doing? Could it be a driver issue? There are a range of excel drivers to choose from.
Thanks
Looking at the info you have provided, you have tried to use two Excel workbooks as tables to query using Excel VBA UDF. Now I assume you are going to use these workbooks as your tables but in MS Access.
All most all databases is able to read standard SQL. See the thing is that each database is able to handle functions writting in their own space. In your case please write your UDF in Access VBA. Then try to execute to the same.
This is a common issue sometimes people do face, either tyring to access MS Access UDFs from Excel or vise versa. In a nutshell, when you're running MS Access, queries can call back into VBA. But when you're going through ODBC or ADO, the JET engine doesn't have the whole VBA model to draw on because it's simply not running.
You could try to do something like this:
Dim objExcl As Object
Set objExcl = CreateObject("Excel.Application")
objExcl.OpenCurrentDatabase "ExcelFileName/Path"
objExcl.Run ("UDFName")
objExcl.CloseCurrentDatabase
Set objExcl = Nothing
Frankly I prefer moving the UDF in to Acces..
References:
Create Access UDF
http://www.sqlexamples.info/SQL/inlineudf.htm