简体   繁体   English

将Excel中的SQL代码导出到SQL

[英]Export SQL code from Excel into SQL

My skill level: basic user of VBA, know next to nothing about SQL (though I've recently learned how to execute code, etc). 我的技能水平:VBA的基本用户,对SQL几乎一无所知(尽管我最近已经学会了如何执行代码,等等)。

I've created a user interface in an Excel worksheet to allow user input of report variables (dates, etc). 我已经在Excel工作表中创建了一个用户界面,以允许用户输入报告变量(日期等)。 These pass into a second Excel worksheet that, through the use of various Excel statements, creates the necessary SQL code. 它们传递到第二个Excel工作表中,该工作表通过使用各种Excel语句来创建必要的SQL代码。 My macro then copies this worksheet and opens Microsoft SQL Server 2008 Rx via a shell command. 然后,我的宏将复制此工作表并通过shell命令打开Microsoft SQL Server 2008 Rx。 Currently, I then manually click “connect” on the resultant Microsoft SQL Server 2008 R2 connect-to-server window (which does not need a password, just requires me to click “connect”), I create a new query, I paste the code in and then execute in SQL. 当前,然后在生成的Microsoft SQL Server 2008 R2连接服务器窗口上手动单击“连接”(不需要密码,只需要单击“连接”),创建新查询,然后粘贴代码,然后在SQL中执行。 So it is these latter steps (opening SQL, click “connect”, copy/paste code, execute) that I'm trying to automate via VBA. 因此,正是这些我要通过VBA自动化的步骤(打开SQL,单击“连接”,复制/粘贴代码,执行)。

To offer more context, once the SQL code has run, I then have a second macro that uses the Excel Data Connection wizard to bring the resultant SQL data back into my worksheet. 为了提供更多的上下文,一旦运行了SQL代码,我便有了另一个宏,该宏使用Excel数据连接向导将生成的SQL数据带回到我的工作表中。 This is working well. 这运作良好。

I've researched this for several days on various boards. 我已经在各种板上研究了几天。 None seem to observe my approach of creating the code in Excel (probably because it is an inelegant approach). 似乎没有人遵循我在Excel中创建代码的方法(可能是因为这是一种不雅的方法)。 Or the post responses are beyond my skill level to understand. 或帖子回复超出了我的理解水平。 But I do fear that I'm asking a question that has been answered before (apologies in advance). 但是我确实担心我要问的是一个已经回答过的问题(事先道歉)。

Thanks for whatever advice/time can be offered. 感谢您提供的任何建议/时间。

I'm using: Excel/Office 2010 Microsoft SQL Server2008 R2 (is this the same as Microsoft SQL Server Management Studios? My SQL application shows both names) Microsoft ActiveX Data Objects 6.1 Library (this is NOT currently checked off. One post I saw referenced the need for this. I have multiple prior AciveX Library versions available as well.) 我正在使用:Excel / Office 2010 Microsoft SQL Server2008 R2(与Microsoft SQL Server Management Studios相同吗?我的SQL应用程序同时显示两个名称)Microsoft ActiveX Data Objects 6.1 Library(当前未选中)。引用了此需求。我也有多个先前的AciveX库版本可用。)

What you're doing is very ingenious, but it's probably not necessary. 您正在执行的操作非常巧妙,但这可能不是必需的。 Excel has a number of features for importing external data. Excel具有许多用于导入外部数据的功能。

I would look at the Data tab in Excel and the From Other Sources section of the ribbon. 我将查看Excel中的“ Data选项卡和功能区的“ From Other Sources部分。 You have at least three options: the From SQL Server , From Data Connection Wizard , and From Microsoft Query options. 您至少具有三个选项:“ From SQL Server ,“ From Data Connection Wizard ”和“ From Microsoft Query选项。

All of these will set up a connection to a database server and execute SQL against it. 所有这些都将建立与数据库服务器的连接,并对它执行SQL。 They return data to your spreadsheet in different ways. 它们以不同的方式将数据返回到您的电子表格。

Where VBA will come into play again is in automating the generation and parameterization of the queries these will execute. VBA再次发挥作用的地方是自动执行将要执行的查询并自动进行参数化。

But I would set up something that works, with constants for your parameter values, first, so you get an idea of the possibilities, and then read and research dynamically setting your SQL with VBA. 但是,我会先设置一个有效的参数值常量,这样您才能了解各种可能性,然后阅读并研究使用VBA动态设置SQL。

You might also investigate PowerPivot , by the way. 顺便说一下,您可能还会研究PowerPivot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM