我正在使用java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
at org.springframework.test.context.web.socket.MockServerContainerContextCustomizer.customizeContext(MockServerContainerContextCustomizer.java:38)
at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:270)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:633)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:347)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 29 more
运算符,并且返回了不需要的数据类型。查询返回univoted数据的nvarchar数据类型。当连接到具有varchar数据类型的表时,我得到一个隐式类型转换。以下是将产生这些结果的示例。
有没有办法控制UNPIVOT
返回的输出数据类型?我希望UNPIVOT
返回varchar数据类型。
UNPIVOT
答案 0 :(得分:4)
UNPIVOT函数有点奇怪,因为它将从各个列(具有相同的数据类型和长度)获取数据并将其转换为行。
虽然它转换了数据,但它似乎确实做了一些假设。例如,您的Volume
列获取int的数据类型,因为这是您未分配的每个列的数据类型,但问题出在您的VolumeType
列上 - 引擎不知道是否您有一个varchar
或nvarchar
数据,因此当您执行unpivot时,它会提供nvarchar(256)
。
如果您不想进行隐式类型转换,那么我建议您使用CROSS APPLY
取消隐藏数据。它还有一些代码行,但如果需要,您可以包含显式数据类型。这是一个例子:
select *
from
(
select Facility, Dt, VolumeType, Volume
from #Data
cross apply
(
select 'VolumeType1', VolumeType1 union all
select 'VolumeType2', VolumeType2 union all
select 'VolumeType3', VolumeType3
) c (VolumeType, Volume)
) UNP
JOIN #Map
ON UNP.Facility = #Map.Facility
AND UNP.VolumeType = #Map.VolumeType;
See Demo。这应该删除隐式转换,但如果它没有,那么您可以在CROSS APPLY
内执行转换:
select *
from
(
select Facility, Dt, VolumeType, Volume
from #Data
cross apply
(
select cast('VolumeType1' as varchar(15)), VolumeType1 union all
select cast('VolumeType2' as varchar(15)), VolumeType2 union all
select cast('VolumeType3' as varchar(15)), VolumeType3
) c (VolumeType, Volume)
) UNP
JOIN #Map
ON UNP.Facility = #Map.Facility
AND UNP.VolumeType = #Map.VolumeType;
您的问题非常类似于我在DBA.SE上发布的问题,关于UNPIVOT要求所有数据类型长度相同的原因。