Cell format in SQL Data Connection within Excel Workbook - sql

My job has tools that require us to mass upload data through Excel workbooks, so I'm trying to embed my SQL results into my files to save me a step. I've created the data connection, but haven't quite figured out the code format in the 'Definition' box yet.
Right now I have:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username = '[Custcontnums$G2]'
But it's pulling back blank. (If I define the username criteria itself between single quotes, it does work) Eventually I would like to create an "in" statement for the username criteria, so something like:
select custcontnum from "CONTACT"."dbo"."Incont" where custcode='SNH' and username in ('[Custcontnums$G2]','[Custcontnums$G3]','[Custcontnums$G4]')
What is the correct format for an excel reference in the data connection feature for Excel?

Related

VBA to send a SQL string to a Tableau Connection

I have an excel work book that allows users to update multiple SQL statements at once based on data entered into cells.
Then the users copy the updated SQL and go into Tableau and paste it into the corresponding custom SQL data source in tableau and refresh it.
Is there a way to send the updated SQL code directly to the corresponding connection in Tableau?
If the table name is changing, that presents a challenge.
I have two suggestions.
Modify the twb xml.
A Tableau workbook file is simply an XML file. One part of the xml contains the connection information, including your custom SQL. VBA has some libraries for manipulating XML. You can write some custom VBA code to modify the XML that contains the custom SQL.
Use VBA to create/alter a view in teradata
Teradata allows database views. With VBA, you can connect to teradata and create/alter a view that changes based on your parameters from Excel. The key is to keep the view name constant, then Tableau will not need to be changed each time the underlying view definition is changed.
Of the two, my first choice would be the second option. It's cleaner and doesn't require distributing a new workbook file each time.

Excel spreadsheet as parameter for report

Has anyone created a report that could use an Excel spreadsheet attached by the user as a parameter for the report? An example would be a list of potential customers in Excel, and whether they exist or don't exist in the database.
I know something like this is easier to accomplish with a manual SSMS query. However, we have several query requests similar to this which are common requests.
Basically want the user to attach their spreadsheet and compare it with data in the database and then spit out a comparison report.
Use SSIS job to load data from spreadsheet to database and then compare data in usual way with queries.

How to use SQL Server to email an Excel workbook containing the results of a query

I need to send an email that has the results of a query formatted as an Excel spreadsheet using SQL Server.
For example, every Monday, run a query such as:
SELECT * FROM dbo.Table
Save the results in an xlsx Excel workbook and email it.
You have three choices:
Use SSIS to create the xlsx file and email it out.
Send a CSV File
as an attachment using sp_send_dbmail
(https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-send-dbmail-transact-sql)
Write a .Net based CLR procedure to create the xlsx file - for
example see this link where someone had issues and a solution with
this:
(Create an SQL CLR UDF with C#)
As it is, your question is way too broad for Stack Overflow and all of these solutions require not only a lot more input from yourself but will be subject to opinion as well.

Extract Data from SQL to Excel without overwriting the previous Data

I was able to use SQL Server Agent to create a job that extract Data from SQL Server 2008 to Excel Format Daily. However, is there anyway to create a job that keep all the extracts separately without overwrite the previous files? I would really much appreciated your help.
Thank you.
When using OLE DB/Jet data provider for Excel, there is a way to specify the target worksheet name. A worksheet in Excel is a rough equivalent of a database table. One option is to use a different worksheet name each time (say, based on a current date). Another option would be to append the data to the existing worksheet, if that's what you're after. If SQL Server Agent job does not allow you to do something like that, then you may want to create a small app instead.
While Copying from OLEDB to Excel, You can dynamically pass the file name through the expressions based on the Datetime.
Ex: Filepath+Date1_mmddyy_hhmmss
Every time the file generates, It will create a file with new file name and Excel file should be passed as an expression.

How to connect an Excel file to an Access Database

I have created a query in an Access database and exported the query result into an Excel file. Now, I want to connect that Excel file to the Access database (or to that query) so that whenever some fields are updated in database, those changes would be automatically updated in the exported Excel file (report). What would be the best way to do it?
Thanks
You can create a link between Excel and an Access table or query by going to the Data tab on Excel and clicking the "From Access" icon.
Alternatively if there is a trigger inside access that you can use to update data (or if you don't need real time updates just hourly or daily ones), you could have access programmatically re-export.
If you want to work with data in Access, but still maintain the data in Excel, you need to link to the data rather than import it. Follow these steps:
Create a blank database or open an existing file in Access.
Select File, Get External Data, Link Tables.
Select Microsoft Excel as the file type.
Select a worksheet or named range to import, and then
click Next. You can import only one worksheet or named range at a
time, and each one will become an Access table.
In the next dialog box, select or deselect the check box First Row
Contains Column Headings, depending on whether your worksheet has
headings. Then click Next.
Enter a name for the table (or accept the default name that Access
suggests), click Finish, and click OK.
Now you have an Access table that looks almost exactly like the imported table. The advantage is that it maintains a live link to the Excel worksheet and can be edited in either application.