感谢那些对这个问题感兴趣的人。全部通过以下答案解决。很抱歉让您对不清楚的问题感到困惑。英语不是我的母语,所以有时候很难详细解释。谢谢你们所有人。
我只想打印出结果,在表格中输出4个变量似乎不对。我该如何解决这个问题?
我尝试使用print @local变量,但我认为它在事务中正常工作。谢谢
我正在使用Microsoft SQL Server;值将是城市名称
CREATE PROCEDURE spFindAverageValue6
@Value VARCHAR(15)
AS
BEGIN
DECLARE @AverageWinSpeed FLOAT
DECLARE @AverageRainfall FLOAT
DECLARE @MaximumTemperature FLOAT
DECLARE @Minimumtemperature FLOAT
IF @VALUE IN (SELECT station FROM weather GROUP BY station)
BEGIN
SELECT @AverageWinSpeed = (SELECT AVG(ISNULL(WindSpeed, 0))
FROM WEATHER
WHERE Station = @Value)
SELECT @AverageRainfall = (SELECT AVG(ISNULL(Rain, 0))
FROM WEATHER
WHERE Station = @Value)
SELECT @MaximumTemperature = (SELECT MAX(ISNULL(Tmax, 0))
FROM WEATHER
WHERE Station = @Value)
SELECT @Minimumtemperature = (SELECT MIN(ISNULL(Tmin, 0))
FROM WEATHER
WHERE Station = @Value)
END
END
执行:
spFindAverageValue4 @VALUE = 'AUCKLAND'
答案 0 :(得分:3)
不完全确定您想要做什么 - 但假设您希望将这四个值发送给调用者(以便调用者可以将其打印出来),您可以大大简化你的存储过程是这样的:
CREATE PROCEDURE spFindAverageValue6
@Value VARCHAR(15)
AS
BEGIN
SELECT
AVG(ISNULL(WindSpeed, 0)) AS 'AverageWindSpeed',
AVG(ISNULL(Rain, 0)) AS 'AverageRainfall',
MAX(ISNULL(Tmax, 0)) AS 'MaximumTemperature',
MIN(ISNULL(Tmin, 0)) AS 'Minimumtemperature'
FROM
dbo.WEATHER
WHERE
Station = @Value
END
然后像这样调用它:
spFindAverageValue4 @VALUE = 'AUCKLAND'
然后你会得到一行的结果集,其中有四列对应于你在dbo.WEATHER
表中的程序中选择的每个值(这些值当然是完全弥补的 - 只是为了说明期待的内容):
AverageWindSpeed AverageRainfall MaximumTemperature MinimumTemperature
-------------------------------------------------------------------------------
23.76 106.91 31.07 -5.95