从外部文件加载T-Sql时,我无法编译此代码。当T-Sql直接插入F#中时,代码可以工作:
type GetRecentMedia =
SqlCommandProvider<"DECLARE @MedId NVARCHAR(128);
DECLARE @TopN INT;
select Top(@TopN) * from media.mediaInfo
where MediaId = @MedId",
Admin.connectionString, ConfigFile = "Web.config">
但是当我尝试从sql文件加载T-Sql时出现此错误:
执行需要0参数,但这里给出2。
T-SQL:
DECLARE @MedId NVARCHAR(128);
DECLARE @TopN INT;
select Top(@TopN) * from media.mediaInfo
where MediaId = @MedId
F#:
module internal FeedRecentMedia =
type GetRecentMedia =
SqlCommandProvider<"Social\InstagramFeed_Select_GetRecentMedia.sql",
Admin.connectionString, ConfigFile = "Web.config">
module MediaTool =
// get current media
let rM = new FeedRecentMedia.GetRecentMedia()
let curMedia = rM.Execute(MedId = "Id", TopN = 1) |> Seq.head
我在管理工作室测试了t-sql,它在那里工作。我只得到上面提到的错误。我在这里缺少什么?
答案 0 :(得分:1)
这是SqlClient类型提供程序的documented限制。从F#代码传递给T-SQL的参数必须是
幸运的是,文档中提供的相同解决方法解决了这两个问题:使用外部变量作为参数声明变量。这也允许您明确注释类型:
type FizzOrBuzz = SqlCommandProvider<"
DECLARE @x AS INT = @xVal
SELECT
CASE
WHEN @x % 3 = 0 AND @x % 5 = 0 THEN 'FizzBuzz'
WHEN @x % 3 = 0 THEN 'Fizz'
WHEN @x % 5 = 0 THEN 'Buzz'
ELSE CONCAT(@x, '') --use concat to avoid nullable column
END", connectionString>
let fizzOrBuzz = new FizzOrBuzz()
printfn "Answer on interview:\n%A" [ for i = 1 to 100 do yield! fizzOrBuzz.Execute(i) ]