2016-08-16 37 views
1

我有一個表學科SQL自定義函數調用的其他功能,顯示錯誤

DisciplineID |Discipline_Name 
1   | Aquatics 
2   | Archery 
3   | Athletics 

我AVE一個函數,它像(「游泳,射箭」)的字符串,並返回一個表項 游泳 射箭 (基本上分割字符串) 用於該函數如下

CREATE FUNCTION SplitString 
( 
@Input NVARCHAR(MAX) 
) 
RETURNS @Output TABLE (
    Item NVARCHAR(1000) 
) 
AS 
BEGIN 
    DECLARE @StartIndex INT, @EndIndex INT, @Character CHAR(1) 
    SET @StartIndex = 1 
    SET @Character =',' 
    IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character 
    BEGIN 
     SET @Input = @Input + @Character 
    END 
    WHILE CHARINDEX(@Character, @Input) > 0 
    BEGIN 
     SET @EndIndex = CHARINDEX(@Character, @Input) 
     INSERT INTO @Output(Item) 
     SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1) 
     SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input)) 
    END 
    RETURN 
END 
GO 

另外的其它功能:

create function getdisID 
(
@Inp varchar(1000) 
) 
RETURNS @Output TABLE (
    Itemid int 
    ) 
as 
begin 

SELECT DisciplineID 
FROM Disciplines 
inner JOIN 
dbo.SplitString(@Inp) as temp 
ON Disciplines.Discipline_Name=temp.Item; 
end 

在執行第二函數產生錯誤 「包括在函數內無法數據返回到客戶端選擇語句」。

+1

我認爲你想'應用'而不是'加入'功能 – HoneyBadger

+0

@ H oneyBadger你能更清楚一點 – praveenkrishp

+1

[INNER JOIN與表值函數不工作]的可能重複(http://stackoverflow.com/questions/23402316/inner-join-with-table-valued-function-not-工作) –

回答

1

的第二功能的腳本應該是這樣的:

create function getdisID 
(
@Inp varchar(1000) 
) 
RETURNS @Output TABLE (
    Itemid int 
    ) 
as 
begin 

insert into @Output 
select DisciplineID 
FROM Disciplines 
inner JOIN 
dbo.SplitString(@Inp) as temp 
ON Disciplines.Discipline_Name=temp.Item; 
return 
end 
+0

謝謝塔雷克,工作正常。 – praveenkrishp

6

這是否幫助

create function getdisID (@Inp varchar(1000)) 

Returns Table 
As 
Return (
    SELECT DisciplineID 
    FROM Disciplines 
    inner JOIN dbo.SplitString(@Inp) as temp 
    ON Disciplines.Discipline_Name=temp.Item; 
) 
+0

謝謝,它工作。 – praveenkrishp

2
create function getdisID 
(
    @Inp varchar(1000) 
) 
RETURNS @Output TABLE (
    Itemid int 
) 
as 
begin 

INSERT INTO @Output(Itemid) 
SELECT DisciplineID 
FROM Disciplines 
inner JOIN 
dbo.SplitString(@Inp) as temp 
ON Disciplines.Discipline_Name=temp.Item; 

end 

你需要可以使用存儲過程返回結果,或者你需要插入在輸出表中的參數數據

+0

我想你可能已經忘記了退貨聲明。但感謝它的工作.. – praveenkrishp

相關問題