0

The building of the following CLR function will get the following error /* Error: Unsupported type. */ in the automatically generated SQL. Which type caused the problem? The MSDN document https://msdn.microsoft.com/en-us/library/ms131103.aspx was used as an example to create the Clr function.

--------------------------------------------------------------------------------
--     This code was generated by a tool.
--
--     Changes to this file may cause incorrect behavior and will be lost if
--     the code is regenerated.
--------------------------------------------------------------------------------

CREATE FUNCTION [dbo].[InitMethod] (@path [nvarchar](MAX), @pattern [nvarchar](MAX), @recursive [bit])
RETURNS /* Error: Unsupported type. */
AS EXTERNAL NAME [ListFiles].[FileList].[InitMethod];

The following is the C# code.

public class FileInf
{
    public string Name { get; set; }
    public DateTime LastWriteTime { get; set; }
    public long Length { get; set; }
    public bool IsFile { get; set; }
};

[SqlFunction(FillRowMethodName = "FillRow")]
public static IEnumerable InitMethod(string path, string pattern, bool recursive)
{
    var dir = new DirectoryInfo(path);
    var files = dir.GetFiles(pattern, recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly);
    foreach (var f in files)
    {
        yield return new FileInf
        {
            Name = f.Name,
            LastWriteTime = f.LastWriteTime,
            Length = f.Length,
            IsFile = (f.Attributes & FileAttributes.Directory) == FileAttributes.Directory ? false : true
        };
    }
}

public static void FillRow(Object obj, 
  out SqlChars name, 
  out SqlDateTime lastWriteTime, 
  out SqlInt64 Length, 
  out SqlBoolean isFile)
{
    var fileInfo = (FileInf)obj;
    name = new SqlChars(fileInfo.Name);
    lastWriteTime = new SqlDateTime(fileInfo.LastWriteTime);
    Length = new SqlInt64(fileInfo.Length);
    isFile = new SqlBoolean(fileInfo.IsFile);
}
ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • Can you post the exact error you get? number, severity, state, message. – Remus Rusanu May 27 '15 at 20:37
  • The error is embedded in the generated code in visual studio (the first block of the code in the question). And then it has error under `AS` because there is no body. – ca9163d9 May 27 '15 at 20:44
  • It looks like the C# is giving back 4 outputs while the function is only set up for 3. Is that right? – SQLHound May 27 '15 at 21:13
  • Possible duplicate of [VS 2012 SSDT build CLR with IEnumerable failing on generated syntax](http://stackoverflow.com/questions/25001853/vs-2012-ssdt-build-clr-with-ienumerable-failing-on-generated-syntax) – Derreck Dean Sep 25 '16 at 03:49

1 Answers1

0

Change your attribute to [SqlFunction(FillRowMethodName = "FillRow", TableDefinition = "[name] VARCHAR(MAX),[lastWriteTime] DATETIME2(7),[Length] BIGINT, [isFile] BIT")].

See VS 2012 SSDT build CLR with IEnumerable failing on generated syntax

Community
  • 1
  • 1
Derreck Dean
  • 3,708
  • 1
  • 26
  • 45