I have created a .Net assembly that returns a set of records. When I create a function in SQL Server, I get the message
CREATE FUNCTION for ...failed because T-SQL and CLR types for return value do not match.
I am a bit lost, does anyone have a clue what is going wrong? This is my assembly:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Net;
using System.IO;
using System.Data.SqlTypes;
using System.Net.Json;
namespace SQLHttpRequest
{
public partial class HTTPFunctions
{
public class mailBox
{
public bool Expanded { get; set; }
public string ID { get; set; }
public string Name { get; set; }
public string ParentID { get; set; }
public int ChildFolderCount { get; set; }
public int UnreadItemCount { get; set; }
public int TotalItemCount { get; set; }
}
public static void FillRow(object mailBoxObject
, out SqlBoolean Expanded
, out SqlString ID
, out SqlString Name
, out SqlString ParentID
, out SqlInt32 ChildFolderCount
, out SqlInt32 UnreadItemCount
, out SqlInt32 TotalItemCount
)
{
var mb = (mailBox)mailBoxObject;
Expanded = mb.Expanded;
ID = mb.ID;
Name = mb.Name;
ParentID = mb.ParentID;
ChildFolderCount = mb.ChildFolderCount;
UnreadItemCount = mb.UnreadItemCount;
TotalItemCount = mb.TotalItemCount;
}
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read
, TableDefinition = "Expanded BIT, ID NVARCHAR(100),Name NVARCHAR(max),ParentID NVARCHAR(max),ChildFolderCount INT, UnreadItemCount INT, TotalItemCount INT"
, FillRowMethodName = "FillRow")]
public static List<mailBox> GetMailFolders(string WellKnownFolderName, string FolderID, string UserID, string password)
{
List<mailBox> mbs = ReadFolder(WellKnownFolderName, UserID, password);
// Method ReadFolder Fills the mbs List
return mbs;
}
}
}
The assembly receives some parameters and returns a List of mail folders. The assembly is succesfully created in SQL Server:
CREATE ASSEMBLY SQLHttpRequest
FROM N'C:\...\SQLHttpRequest.dll'
WITH PERMISSION_SET=UNSAFE;
When I try to create the function
CREATE FUNCTION dbo.usp_GetMailFolders(
@WellKnownFolderName nvarchar(max),
@FolderID nvarchar(max),
@UserID nvarchar(max),
@password nvarchar(max)
)
RETURNS TABLE
(
Expanded BIT,
ID NVARCHAR(100),
Name NVARCHAR(100),
ParentID NVARCHAR(100),
ChildFolderCount int,
UnreadItemCount int,
TotalItemCount int
)
AS
EXTERNAL NAME SQLHttpRequest.[SQLHttpRequest.HTTPFunctions].GetMailFolders;
I receive the follwing error:
CREATE FUNCTION for "usp_GetMailFolders" failed because T-SQL and CLR types for return value do not match.
Can anyone spot what I am missing here?
Thanks in advance for your response!
Try to change signature of function GetMailFolders to IEnumerable.
public static IEnumerable GetMailFolders(string WellKnownFolderName, string FolderID, string UserID, string password)
I looked again, and it seems to me to try to put nvarchar instead of varchar in the code to create the tool.