Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 存储过程执行32次_Sql_Sql Server - Fatal编程技术网

Sql 存储过程执行32次

Sql 存储过程执行32次,sql,sql-server,Sql,Sql Server,我遇到了这个问题,我不确定是什么原因造成的。我的教授也同样感到困惑 create procedure getName @AuthorNum decimal(2,0) as begin select AUTHOR_LAST 'Last Name', AUTHOR_FIRST 'First Name' from AUTHOR where AUTHOR_NUM = @AuthorNum end go execute getName 9 这应该返回Collin

我遇到了这个问题,我不确定是什么原因造成的。我的教授也同样感到困惑

 create procedure getName
    @AuthorNum  decimal(2,0)
as
begin
    select AUTHOR_LAST 'Last Name', AUTHOR_FIRST 'First Name' 
    from AUTHOR 
    where AUTHOR_NUM = @AuthorNum
end
go

execute getName 9
这应该返回
Collins-Bradley
。相反,它执行32次。第一次返回预期结果,但其余31次中的每一次都返回表中第二个条目的条目(
Soloratoff Paul
,如果您感兴趣)


有什么线索可以解释为什么它执行的次数最多吗?

我通过查看存储过程文件夹中的条目找到了答案


显然,当我执行创建时,
executegetname2
已经潜入其中。我忘记在我的创建块和执行块之间放置一个
go
。这就是当您只执行一个查询的一部分时会发生的情况…

恭喜您自己找到了解决方案!只有一个提示:如果您只读取数据,那么存储过程不是执行此操作的最佳位置。您应该更喜欢内联表值函数(不带
BEGIN…END
),谢谢,很高兴知道。这是对一个赋值的回答,所以这个问题是专门为这个问题提出的另一个提示,当使用execute调用SP时,请使用命名参数。这样,如果参数的顺序在将来发生变化,代码就不会中断。