CREATE FUNCTION dbo.ta_OTMultiplierstest (@id int)
RETURNS nvarchar (100)
AS
BEGIN
DECLARE @text nvarchar(100)
set @text = ( SELECT CONVERT(nvarchar(5), b.Duration), CONVERT(nvarchar(5),b.Mon), CONVERT(nvarchar(5), b.Tue), CONVERT(nvarchar(5), b.Wed),
CONVERT(nvarchar(5), b.Thu), CONVERT(nvarchar(5), b.Fri), CONVERT(nvarchar(5), b.Sat), CONVERT(nvarchar(5), b.Sun),
CONVERT(nvarchar(5), b.DayOff), CONVERT(nvarchar(5), b.Holiday), CONVERT(nvarchar(5), b.Yearly),
CONVERT(nvarchar(5), b.Maternity), CONVERT(nvarchar(5), b.Other)
FROM ta_GenPolOTMultiplier b WHERE b.PolHistID = @id
Group By b.Duration)--, b.Mon, b.Tue, b.Wed, b.Thu, b.Fri, b.Sat, b.Sun, b.DayOff, b.Holiday, b.Yearly, b.Maternity, b.Other)
return @text
END
我收到此错误“当EXISTS未引入子查询时,只能在选择列表中指定一个表达式” 我知道我不能返回多个字段,我只需要知道如何解决这个问题? 我需要在存储过程中返回此函数并获取所有这些值的表
我感谢任何帮助
答案 0 :(得分:1)
您需要连接文本部分,如下所示:
set @text = ( SELECT CONVERT(nvarchar(5), b.Duration) + CONVERT(nvarchar(5),b.Mon) ...
答案 1 :(得分:1)
您可以使用“单个语句表值函数”执行此操作,如下所示:
CREATE FUNCTION dbo.ta_OTMultiplierstest (@id int)
RETURNS TABLE
AS
RETURN
SELECT
CONVERT(nvarchar(5), b.Duration)
, CONVERT(nvarchar(5),b.Mon)
, CONVERT(nvarchar(5), b.Tue)
, CONVERT(nvarchar(5), b.Wed)
, CONVERT(nvarchar(5), b.Thu)
, CONVERT(nvarchar(5), b.Fri)
, CONVERT(nvarchar(5), b.Sat)
, CONVERT(nvarchar(5), b.Sun)
, CONVERT(nvarchar(5), b.DayOff)
, CONVERT(nvarchar(5), b.Holiday)
, CONVERT(nvarchar(5), b.Yearly)
, CONVERT(nvarchar(5), b.Maternity)
, CONVERT(nvarchar(5), b.Other)
FROM dbo.ta_GenPolOTMultiplier b WHERE b.PolHistID = @id
Group By b.Duration
这将为您提供一个可以在您的程序中使用或加入的表格。
如果你需要一个连接的字符串,请参阅Ackroydd的答案。
答案 2 :(得分:0)
试试这个 -
CREATE FUNCTION dbo.ta_OTMultiplierstest (@id int)
RETURNS @Result TABLE
(
Duration nvarchar(5),
Mon nvarchar(5),
Tue nvarchar(5),
Wed nvarchar(5),
Thu nvarchar(5),
Fri nvarchar(5),
Sat nvarchar(5),
Sun nvarchar(5),
DayOff nvarchar(5),
Holiday nvarchar(5),
Yearly nvarchar(5),
Maternity nvarchar(5),
Other nvarchar(5)
)
AS
BEGIN
INSERT INTO @Result
(
Duration,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat,
Sun,
DayOff,
Holiday,
Yearly,
Maternity,
Other
)
SELECT
CONVERT(nvarchar(5), b.Duration)
, CONVERT(nvarchar(5),b.Mon)
, CONVERT(nvarchar(5), b.Tue)
, CONVERT(nvarchar(5), b.Wed)
, CONVERT(nvarchar(5), b.Thu)
, CONVERT(nvarchar(5), b.Fri)
, CONVERT(nvarchar(5), b.Sat)
, CONVERT(nvarchar(5), b.Sun)
, CONVERT(nvarchar(5), b.DayOff)
, CONVERT(nvarchar(5), b.Holiday)
, CONVERT(nvarchar(5), b.Yearly)
, CONVERT(nvarchar(5), b.Maternity)
, CONVERT(nvarchar(5), b.Other)
FROM dbo.ta_GenPolOTMultiplier b WHERE b.PolHistID = @id
Group By b.Duration
RETURN
END