{"id":998,"date":"2010-01-27T10:24:59","date_gmt":"2010-01-27T09:24:59","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=998"},"modified":"2010-01-27T10:24:59","modified_gmt":"2010-01-27T09:24:59","slug":"sqlclr-deplyment-hiba","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2010\/01\/27\/sqlclr-deplyment-hiba\/","title":{"rendered":"SQLCLR deplyment hiba"},"content":{"rendered":"<p>Error: Incorrect syntax near valami.<br \/>\nAkkor j\u00f6n el\u0151, ha az SQLCLR assemblyt \u00e9s benne a f\u00fcggv\u00e9nyeket akarja az VS deployolni. T\u00f6bb oka lehet, most az volt, hogy egy .NET oldalon double-t visszaad\u00f3 f\u00fcggv\u00e9ny v\u00e9letlen\u00fcl \u00edgy lett deklar\u00e1lva:<\/p>\n<p>[SqlFunction(&#8230;, TableDefinition = &#8220;Datum datetime, Szazalek double&#8221;)]<\/p>\n<p>Mi a hiba benne? SQL Serverben nincs double, csak real \u00e9s float. R\u00e1ad\u00e1sul a C# float az az SQL real \u00e9s a C# double az SQL Server float (kb.). :)<\/p>\n<p>Az el\u0151bbi helyesen:<\/p>\n<p>[SqlFunction(&#8230;, TableDefinition = &#8220;Datum datetime, Szazalek float&#8221;)]<\/p>\n<p>Mi\u00e9rt kellett SQLCLR f\u00fcggv\u00e9nyt \u00edrni? A fut\u00f3 aggreg\u00e1l\u00e1sok (\u00e9n legal\u00e1bbis nem tudok jobbat kurzor n\u00e9lk\u00fcl) o(n2)-es algoritmusok, ezt CLR-ben k\u00f6nnyen meg lehet \u00edrni o(n)-re. Pl:<\/p>\n<p>\u00ef\u00bb\u00bfusing System;<br \/>\nusing System.Collections;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.Data;<br \/>\nusing System.Data.SqlClient;<br \/>\nusing System.Data.SqlTypes;<br \/>\nusing Microsoft.SqlServer.Server;<\/p>\n<p>public class UserDefinedFunctions<br \/>\n{<br \/>\n    internal class Result<br \/>\n    {<br \/>\n        public long RowId;<br \/>\n        public double CumDollarGain;<br \/>\n        public double TopDollarGain;<br \/>\n        public double DollarDrawDown;<br \/>\n    }<\/p>\n<p>    \/\/Input table: create table #trades(RowId long, DollarGain money, other columns possible)<br \/>\n    [SqlFunction(FillRowMethodName = &#8220;FillRow&#8221;, DataAccess = DataAccessKind.Read,<br \/>\n        TableDefinition = &#8220;RowId bigint, CumDollarGain float, TopDollarGain float, DollarDrawDown float&#8221;)]<br \/>\n    public static IEnumerable Cumul()<br \/>\n    {<br \/>\n        using (var conn = new SqlConnection(&#8220;Context connection=true&#8221;))<br \/>\n        {<br \/>\n            using (var cmd = new SqlCommand(&#8220;select RowID, DollarGain from #trades&#8221;, conn))<br \/>\n            {<br \/>\n                var res = new List<Result>();<br \/>\n                conn.Open();<br \/>\n                double cumulPrice = 0, topPrice = 0, drawDawn = 0;<\/p>\n<p>                using (SqlDataReader r = cmd.ExecuteReader())<br \/>\n                {<br \/>\n                    int idCol = r.GetOrdinal(&#8220;RowID&#8221;);<br \/>\n                    int gainCol = r.GetOrdinal(&#8220;DollarGain&#8221;);<\/p>\n<p>                    while (r.Read())<br \/>\n                    {<br \/>\n                        var price = r.GetDouble(gainCol);<\/p>\n<p>                        cumulPrice += price;<br \/>\n                        topPrice = Math.Max(price, topPrice);<\/p>\n<p>                        drawDawn += price;<br \/>\n                        drawDawn = Math.Min(drawDawn, 0);<\/p>\n<p>                        res.Add(new Result<br \/>\n                                    {<br \/>\n                                        RowId = r.GetInt64(idCol),<br \/>\n                                        CumDollarGain = cumulPrice,<br \/>\n                                        TopDollarGain = topPrice,<br \/>\n                                        DollarDrawDown = drawDawn<br \/>\n                                    });<br \/>\n                    }<br \/>\n                    return res;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    public static void FillRow(object obj,<br \/>\n    out long id,<br \/>\n    out double cumDollarGain,<br \/>\n    out double topDollarGain,<br \/>\n    out double dollarDrawDown)<br \/>\n    {<br \/>\n        var r = (Result)obj;<br \/>\n        id = r.RowId;<br \/>\n        cumDollarGain = r.CumDollarGain;<br \/>\n        topDollarGain = r.TopDollarGain;<br \/>\n        dollarDrawDown = r.DollarDrawDown;<br \/>\n    }<br \/>\n};<\/p>\n<p>Sajnos nem lehet \u00e1tpasszolni a megnyitott SqlDataReadert a k\u00e9t met\u00f3dus k\u00f6z\u00f6tt, ez\u00e9rt k\u00e9nytelen az ember let\u00e1rolni az eredm\u00e9nyhalmazt. Persze p\u00e1r ezer sorn\u00e1l ez nem gond.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Error: Incorrect syntax near valami. Akkor j\u00f6n el\u0151, ha az SQLCLR assemblyt \u00e9s benne a f\u00fcggv\u00e9nyeket akarja az VS deployolni. T\u00f6bb oka&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,25,6,4,21,58],"tags":[],"class_list":["post-998","post","type-post","status-publish","format-standard","hentry","category-net","category-adonet","category-adatbazisok","category-szakmai-elet","category-sql-server-2005","category-sql-server-2008"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":1,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"predecessor-version":[{"id":999,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/998\/revisions\/999"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}