为什么事件虽然声明给出错误必须在SQL函数中声明标量变量? [英] Why event though declaration giving error must declare the scalar variable in SQL function?

查看:133
本文介绍了为什么事件虽然声明给出错误必须在SQL函数中声明标量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在改变`function`,如:

I am altering `function` like:

ALTER FUNCTION [dbo].[fn_CalculateListing](@Listing TypeListingDatePrice READONLY)
RETURNS TypePreviousListingResult
AS
BEGIN
    DECLARE @tbl_ListingDateDetails TypePreviousListingResult
    RETURN @tbl_ListingDateDetails
END
GO



但是抛出错误:




But throwing error as:

Msg 137, Level 16, State 1, Procedure fn_CalculateListing, Line 6
   Must declare the scalar variable "@tbl_ListingDateDetails".



为什么这个给出错误并要求声明尽管我在使用那个变量之前声明了这个?


Why this giving error and asking for declaration eventhough I have declared that before using that variable?

推荐答案

这不是你的整个函数 - 如果没有别的东西它没有给变量赋值,所以RETURN有点无意义! :笑:

如果没有整个功能,很难确定(因为我们没有你的类型,我们无法准确测试你的代码),但最常见的原因是你的function包含一个或多个GO命令。这些破坏代码以及在GO之上声明的任何内容在下面不可用:

That isn't your whole function - if nothing else it doesn't give a value to the variable so the RETURN is a little pointless! :laugh:
Without the whole function it's difficult to be sure (and since we don't have your types, we can't test your code exactly) but the most common reason for this is that your function contains one or more GO commands. These "break" the code and anything declared above the GO is not available below:
DECLARE @X INT
SET @X = 3
SELECT @X
GO
SELECT @X

将给出您描述的错误消息。

Will give the error message you describe.


这篇关于为什么事件虽然声明给出错误必须在SQL函数中声明标量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆