您可以将多个列名称设置为SQL中的宏以进行查询吗?

时间:2020-05-22 14:37:37

标签: sql-server tsql

能否将SQL表中的多个列名设置为SQL中的宏以进行查询?

例如,我多次遇到多个列,是否可以使用宏或某种类型的引用来一次识别它们,以避免重复显示它们并使代码混乱?

当前代码有效,我只是在寻找一种更简洁/精简的选项。

当前代码:

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    ReactiveFormsModule,
    SharedModule,
    TranslateModule,
  ],
  declarations: [
    LoginComponent,
  ],
})
export class LoginModule {}

目标:

Failed: StaticInjectorError(DynamicTestModule)[NotificationEffects -> Actions]: 
      StaticInjectorError(Platform: core)[NotificationEffects -> Actions]: 
        NullInjectorError: No provider for Actions!
    error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'NotificationEffects', Function ] })
        at <Jasmine>
        at NullInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:855:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17514:1)
        at tryResolveToken (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17440:1)
        at StaticInjector.get (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:17266:1)
        at resolveNgModuleDep (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30393:1)
        at _createClass (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30470:1)
        at _createProviderInstance (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/fesm2015/core.js:30426:1)
    Error: Expected undefined to be truthy.
        at <Jasmine>
        at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/login/login.component.ngrx.spec.ts:67:23)
        at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
        at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)

这是继承的代码,看起来很笨拙。

谢谢

2 个答案:

答案 0 :(得分:0)

像这样的带编号的列几乎总是一个征兆,您应该有一个附加的表。因此,如果您现有的表结构是这样的:

Table1
Table1ID, OtherFields, Code1, Code2, Code3.... Code25

您真的想要更多类似这样的东西:

Table1
Table1ID, OtherFields

Table1Codes
Table1ID, Code

Table1中的每个条目在Table1Codes中将具有很多条目。然后,编写JOIN语句以在需要时并排显示两个集合。

FROM Table1 t
INNER JOIN Table1Codes tc1 ON tc.Table1ID = t.Table1ID AND tc.Code = 'ABC'
INNER JOIN Table1Codes tc2 ON tc.Table1ID = t.Table1ID AND tc.Code = 'CFS'

FROM Table1 t
INNER JOIN Table1Codes tc1 ON tc.Table1ID = t.Table1ID AND tc.Code IN ('ABC','FGS','g6s') 

答案 1 :(得分:0)

在通常情况下,如果您不能更改表的架构,则可以UNPIVOT。例如,假设CODE1 ... CODE25来自MyTable,则将UNPIVOT操作包装在CTE中:

;WITH
    cte AS 
    (
        SELECT      upvt.*
        FROM        MyTable
        UNPIVOT     (
                        CodeValue FOR CodeLabel IN ([CODE1], [CODE2], ..., [CODE25])
                    ) upvt
    )

SELECT      *
FROM        cte
WHERE       CodeValue IN ('ABC', 'DEF', ...)

不可撤销的操作不是免费的。取消对MyTable的过滤之前,请确保对其进行尽可能多的过滤。