简体   繁体   English

将长 SQL 代码放入 VBA Excel 的最简单方法是什么?

[英]Whats the easiest way to put a long SQL code into VBA Excel?

We have a code that is quite long, that we want to put into VBA Excel.我们有一个很长的代码,我们想将它放入 VBA Excel 中。 For extracting the outstanding balance of all our customers account (we're a bank sort of).用于提取我们所有客户账户的未结余额(我们是一家银行)。 But To write the code takes to long because we have to reformat it etc etc..... So what do you suggest we use?但是编写代码需要很长时间,因为我们必须重新格式化它等等......那么你建议我们使用什么?

Im not sure wether I should put in what we've thought about, I don't want to steer your recommendations here.....But we've looked into FUNCTIONS, PROCEDURES and VIEWS.我不确定我是否应该输入我们的想法,我不想在这里指导您的建议......但我们已经研究了功能、程序和视图。 All for the purpose of finding lets call it a "container" which we can put our code in and then put it into VBA, and then make sure that it understands what is inside this container, so that it executes the code and gets us the outstanding balance + some more info.为了查找,我们将其称为“容器”,我们可以将代码放入其中,然后将其放入 VBA,然后确保它了解此容器中的内容,以便执行代码并获取我们未结余额+更多信息。 Thannks!谢谢!

Using a SQL VIEW / Stored Proc is a better idea than creating queries in Excel.使用 SQL VIEW / Stored Proc 比在 Excel 中创建查询更好。 It'll be pre-compiled so will run quicker, and it means you can call it from other places than Excel.它将被预编译,因此运行速度更快,这意味着您可以从 Excel 以外的其他地方调用它。

If you put all the definition for a query in Excel, then you'll need to use Excel to get that same info, or copy that Excel routine to somewhere else (and that's when mistakes start to happen).如果您将查询的所有定义放在 Excel 中,那么您将需要使用 Excel 来获取相同的信息,或者将 Excel 例程复制到其他地方。 Adhere to DRY (Don't Repeat Yourself).坚持干燥(不要重复自己)。

I would just create a stored SQL procedure or a VIEW, server-side and then just call it from Excel with whatever parameters you like to pull in the right data, keep it nice and simple on the Excel side.我将创建一个存储的 SQL 过程或服务器端的 VIEW,然后从 Excel 调用它,使用您喜欢的任何参数提取正确的数据,在 ZC1D81AF5835844B4EZD9D9DED86 上保持美观和简单。

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

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