简体   繁体   English

替代动态SQL和每个循环

[英]Alternative to dynamic sql and for each loop

I need to execute the same select statement against 5 different instances of the same source db - 1 per clinic - and union the result set together for further processing. 我需要对同一源数据库的5个不同实例执行相同的select语句-每个诊所1个-并将结果集合并在一起以进行进一步处理。 The existing script I've been tasked with maintaining executes a for each loop (1 iteration per instance) and substitutes the db name in the select statements for each iteration. 我负责维护的现有脚本为每个循环执行一个(每个实例1次迭代),并在每次迭代的select语句中替换数据库名称。 Currently the db names are obtained from selecting the dbs from master where name in 'db 1, db2, db3, db4, db5'. 当前,从“ master”中选择“ db 1,db2,db3,db4,db5”中的dbs来获得db名称。

I'd prefer to hold a reference table with each of the db names and a flag for whether they are included. 我希望保留一个带有每个数据库名称和一个是否包含它们的标志的引用表。 But is there an alternative to the for each loop? 但是,对于每个循环,还有替代方法吗?

So you have 5 different clinics and each one has a database with names: db1, db2, db3, db4, db5. 因此,您有5个不同的诊所,每个诊所都有一个名称为db1,db2,db3,db4,db5的数据库。

And the database names are all stored in a string variable as "db1, db2, db3, db4, db5" which you can then use as an array to sort through the database names. 数据库名称都存储在字符串变量“ db1,db2,db3,db4,db5”中,然后您可以将其用作数组来对数据库名称进行排序。

Of course it seems clunky having all these different databases to have to deal with but looks like that is how things were when you got it and may have some advantages in that at least someone hacking or otherwise mishandling one clinic can't mess things up in the other databases. 当然,要处理所有这些不同的数据库似乎很笨拙,但是当您获得它时,情况看起来是这样,并且可能具有一些优势,至少有人入侵或以其他方式处理一个诊所的情况不会使事情变得混乱其他数据库。

And at least the looping isn't using something simple iteration like this: 而且至少循环没有使用像这样的简单迭代:

For i = 1 to 5
    strDBName = "db" & i
Next

A problem with using i to iterate is that what if clinic 2 is no longer active and thus db2 should no longer be used. 使用i进行迭代的一个问题是,如果诊所2不再处于活动状态,那么应该不再使用db2,该怎么办。 And of course it there is a new clinic added then the "For i = 1 to 5" would need to be changed all over to be "For i = 1 to 6". 当然,这里增加了一个新诊所,然后需要将“对于i = 1至5”更改为“对于i = 1至6”。

So considering the above worse ways of doing things I kind of like the current way (DB names in a string variable that can be looped through as an array). 因此,考虑上述更糟糕的工作方式,我有点像当前的方式(字符串变量中的数据库名称可以作为数组循环通过)。 And the idea of possibly "hold a reference table with each of the db names and a flag for whether they are included" to me seems like a fancy way of doing what you're already doing and personally I'm all in favor of "keep it simple" and "if it ain't broke don't fix it." 对我来说,可能的想法是“持有一个带有每个数据库名称的参考表,并标记一个是否包含这些数据库的名称”,这似乎是做您已经在做的事情的一种奇特的方式,我个人完全赞成“保持简单”和“如果没有损坏,请勿修复。”

EDIT: 编辑:
But the more I think about it taking your "hold a reference table with each of the db names and a flag for whether they are included" idea and running with it a little bit the following does seem natural (but of course perhaps be sure only Admin users can see any fields about the specific database): 但是,我考虑得越多,就采用了“持有每个数据库名称和是否包含它们的标记的引用表”的想法,并对其进行一点操作,以下内容似乎很自然(但当然可以肯定管理员用户可以查看有关特定数据库的任何字段):

Clinics Table with Fields Such As: 带有以下字段的诊所表:
ClinicID ClinicID
ClinicName ClinicName
ClinicAddress ClinicAddress
ClinicDBName - Note could store the DB name here ClinicDBName-注意可以在此处存储数据库名称
ClinicIsInactive ClinicIsInactive

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

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