过程返回0而不是更高的数字

时间:2018-09-06 21:54:23

标签: sql sql-server

我有以下步骤,可以根据年份检索用户输入的某些数据。但是,我总是得到0。我对SQL还是很陌生,但这似乎应该可行

Create PROCEDURE [dbo].[Yearly]
@year int
AS
BEGIN
DECLARE @yearly Datetime
DECLARE @summ int   

SELECT @summ = SUM([dbo].[Out].[OutPcs]), @yearly = [dbo].[Out].[DateTime]
FROM [dbo].[Out]
WHERE YEAR(@yearly) = @year
GROUP BY [Out].[DateTime]
END;

我应该使用嵌套的select语句吗?我怀疑程序的那部分出了问题。

3 个答案:

答案 0 :(得分:2)

您有DECLARE @yearly Datetime

您尝试在SELECT ... @yearly = Out.Datetime FROM Out中进行设置,但是您有以下WHERE语句:YEAR(@yearly) = @year

由于@yearlyNULL调用时,YEAR()WHERE NULL = 2018,因此不会返回任何内容

这使该语句等效于WHERE

这将永远是不正确的。

要解决此问题,您需要设置年度,然后才能在YEAR(Dbo.Out.Datetime)子句中调用它或在其中使用其他内容。

您似乎想在那里使用SQL


由于您似乎是SQL的新手,所以我将添加一些额外的说明。这太简单了。

大多数编程语言自上而下运行。首先执行line1,第二,第二行,第三,等等。 SELECT Name FROM Employee WHERE EmpID = 1不会这样做。

命令First - FROM Employee --> Load the Employee table Second - WHERE EmpID = 1 --> Scan Employee for the records where EmpID = 1 Third - SELECT Name --> Display the `Name` field of the records I found. 按以下顺序运行。

SQL

compiler First - FROM dbo.Out --> Load Out table Second - WHERE YEAR(@yearly) = @year --> Scan for records that meet this req. Third - SELECT ... @yearly = dbo.Out.Datetime --> Set @yearly to the [Datetime] field associated to the record(s) I found.

中的命令如下所示
SQL

请注意,如果您的语句返回了多个记录,那么1-dimensional会尝试将2018-09-06 14:58:04,065 ERROR org.apache.flink.runtime.entrypoint.ClusterEntrypoint - Fatal error occurred in the cluster entrypoint. java.lang.RuntimeException: org.apache.flink.runtime.client.JobExecutionException: Could not set up JobManager at org.apache.flink.util.ExceptionUtils.rethrow(ExceptionUtils.java:199) at org.apache.flink.util.function.ConsumerWithException.accept(ConsumerWithException.java:40) at org.apache.flink.runtime.dispatcher.Dispatcher.lambda$waitForTerminatingJobManager$29(Dispatcher.java:820) at java.util.concurrent.CompletableFuture.uniRun(CompletableFuture.java:705) at java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:687) at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442) at org.apache.flink.runtime.rpc.akka.AkkaRpcActor.handleRunAsync(AkkaRpcActor.java:332) at org.apache.flink.runtime.rpc.akka.AkkaRpcActor.handleRpcMessage(AkkaRpcActor.java:158) at org.apache.flink.runtime.rpc.akka.FencedAkkaRpcActor.handleRpcMessage(FencedAkkaRpcActor.java:70) at org.apache.flink.runtime.rpc.akka.AkkaRpcActor.onReceive(AkkaRpcActor.java:142) at org.apache.flink.runtime.rpc.akka.FencedAkkaRpcActor.onReceive(FencedAkkaRpcActor.java:40) at akka.actor.UntypedActor$$anonfun$receive$1.applyOrElse(UntypedActor.scala:165) at akka.actor.Actor$class.aroundReceive(Actor.scala:502) at akka.actor.UntypedActor.aroundReceive(UntypedActor.scala:95) at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526) at akka.actor.ActorCell.invoke(ActorCell.scala:495) at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257) at akka.dispatch.Mailbox.run(Mailbox.scala:224) at akka.dispatch.Mailbox.exec(Mailbox.scala:234) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) Caused by: org.apache.flink.runtime.client.JobExecutionException: Could not set up JobManager at org.apache.flink.runtime.jobmaster.JobManagerRunner.<init>(JobManagerRunner.java:176) at org.apache.flink.runtime.dispatcher.Dispatcher$DefaultJobManagerRunnerFactory.createJobManagerRunner(Dispatcher.java:936) at org.apache.flink.runtime.dispatcher.Dispatcher.createJobManagerRunner(Dispatcher.java:291) at org.apache.flink.runtime.dispatcher.Dispatcher.runJob(Dispatcher.java:281) at org.apache.flink.util.function.ConsumerWithException.accept(ConsumerWithException.java:38) : ... 21 more Caused by: java.lang.Exception: Cannot set up the user code libraries: /hastorage/default/blob/job_e44fdee88a931200953fed45883ee3f1/blob_p-f655414c973995e93709acbd22c1c162c9c43a98-75bd4e71882f988a6c337222efadba7b (No such file or directory) at org.apache.flink.runtime.jobmaster.JobManagerRunner.<init>(JobManagerRunner.java:134) ... 25 more Caused by: java.io.FileNotFoundException: /hastorage/default/blob/job_e44fdee88a931200953fed45883ee3f1/blob_p-f655414c973995e93709acbd22c1c162c9c43a98-75bd4e71882f988a6c337222efadba7b (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at org.apache.flink.core.fs.local.LocalDataInputStream.<init>(LocalDataInputStream.java:50) at org.apache.flink.core.fs.local.LocalFileSystem.open(LocalFileSystem.java:142) at org.apache.flink.runtime.blob.FileSystemBlobStore.get(FileSystemBlobStore.java:102) at org.apache.flink.runtime.blob.FileSystemBlobStore.get(FileSystemBlobStore.java:84) at org.apache.flink.runtime.blob.BlobServer.getFileInternal(BlobServer.java:493) at org.apache.flink.runtime.blob.BlobServer.getFileInternal(BlobServer.java:444) at org.apache.flink.runtime.blob.BlobServer.getFile(BlobServer.java:417) at org.apache.flink.runtime.execution.librarycache.BlobLibraryCacheManager.registerTask(BlobLibraryCacheManager.java:120) at org.apache.flink.runtime.execution.librarycache.BlobLibraryCacheManager.registerJob(BlobLibraryCacheManager.java:91) at org.apache.flink.runtime.jobmaster.JobManagerRunner.<init>(JobManagerRunner.java:131) ... 25 more 2018-09-06 14:58:04,069 INFO org.apache.flink.runtime.blob.TransientBlobCache - Shutting down BLOB cache 变量设置为值数组。它将失败,并给您类似

  

返回的记录太多。让我只返回1条记录。

答案 1 :(得分:1)

@Edward

解释了为什么您的代码无法正常工作

这是一个有效的代码:

Create PROCEDURE [dbo].[Yearly]
    @year int
AS
BEGIN

    SELECT SUM([dbo].[Out].[OutPcs])
    FROM [dbo].[Out]
    WHERE YEAR([dbo].[Out].[DateTime]) = @year

END;

答案 2 :(得分:0)

您忘记返回“汇总”:

不需要@yearly var。

也不需要按年份分组。

Create PROCEDURE [dbo].[Yearly]
    @year int
AS
BEGIN
    DECLARE @summ int   

    SELECT @summ = SUM([dbo].[Out].[OutPcs])
    FROM [dbo].[Out]
    WHERE YEAR([dbo].[Out].[DateTime]) = @year

    Return @summ
END;