1

Testing my first Table Valued Function, I get no data, even while the SELECT works.
Not returning anything:

select * from GetMvtHistory('C1505 BLACK')

Returning correct data:

select row_number() OVER (order by DocId) as Id, detailId, Quant, 0 as Cumul
    FROM vwDetailsHist
    WHERE refer = 'C1505 BLACK'
    ORDER BY DocId;

TVF code:

ALTER FUNCTION [dbo].[GetMvtHistory]
(@Ref char(10))
RETURNS @MvtHist table
(
Id int,
[DetailId] int NULL, 
[Quant] int NULL, 
[Cumul] int NULL
)
WITH EXEC AS CALLER
AS
BEGIN
INSERT into @MvtHist
    select row_number() OVER (order by DocId) as Id, detailId, Quant, 0 as Cumul
        FROM vwDetailsHist
        WHERE refer = @Ref
        ORDER BY DocId;

RETURN 

What am I missing ?? Thanks !

2
  • What type is vwDetailsHist.refer? Commented Oct 30, 2013 at 11:16
  • 1
    Also this doesn't need to be a multi statement TVF. Inline TVFs generally perform better. Commented Oct 30, 2013 at 11:18

2 Answers 2

2

You've made the parameter a char(10). I cannot imagine that is the data type you need. You should probably use the same data type that the column has (which should probably be an nvarchar).

'C1505 BLACK' is 11 chars. Doesn't fit.

Sign up to request clarification or add additional context in comments.

2 Comments

Oooops. I feel stupid now. You're perfectly right. Thanks. @Ref is indeed Varchar(15)
Be sure to fix the string data types to be longer nvarchar types. That gets rid of issues like this and others.
0

Set @Ref parameter to 50 or higher. Example:

@Ref varchar(500)

This will solve your problem.

1 Comment

Though if ref is indexed and is actually varchar rather than nvarchar using this datatype can prevent an index seek.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.