sqlclr

stored procedure in SQL CLR

笑着哭i 提交于 2019-12-04 19:14:42
How do you write a stored procedure using C# in SQLCLR? Currently I am using SQL Server Management Studio and I write stored procedures using T-SQL such as create proc sp_item as .... . See: Building my first SQL CLR stored procedure CLR Assembly RegEx Functions for SQL Server by Example Choosing between CLR and T-SQL stored procedures: a simple benchmark Basically, there are Visual Studio templates which allow you to get started with SQL CLR projects. Fill in the blanks, write your actual code, and you can even deploy those CLR assemblies into SQL Server directly from within Visual Studio.

SqlDecimal issues in SQLCLR project

℡╲_俬逩灬. 提交于 2019-12-04 17:36:14
I'm having some unexpected results when working with SqlDecimals. In the end it seems to boil down to scale issues when dividing 2 SqlDecimals. c# example code looks like this : [Microsoft.SqlServer.Server.SqlFunction] [return: SqlFacet(Precision = 38, Scale = 8)] public static SqlDecimal fn_divide([SqlFacet(Precision = 38, Scale = 8)]SqlDecimal x, [SqlFacet(Precision = 38, Scale = 8)]SqlDecimal y) { var r = SqlDecimal.Divide(@x, @y); return r; } SQL test code looks like this : DECLARE @x numeric(38, 8), @y numeric(38, 8) SELECT @x = Replicate('1', 28) + '.12345678', @y = '0.25896314' SELECT

SQL CLR for Event Driven Communication

孤人 提交于 2019-12-04 12:27:27
Where I work they are using Long Polling techniques to detect events as they occur in the database. While it works...I think everyone would agree polling the database isn't optimal. I would rather attempt some kind of push-technology or technique. As such, I was thinking about using a table trigger to call a SQL CLR object that either drops the event into a queue or calls a web service. There are plenty of examples on how to do this: Call web service from SQL CLR? How to invoke webservice from SQL Server stored procedure Using Webservices and Xml Serialization in CLR Integration However, I am

Get the names of attributes from an element in a SQL XML column

允我心安 提交于 2019-12-04 09:53:21
问题 For this xml (in a SQL 2005 XML column): <doc> <a>1</a> <b ba="1" bb="2" bc="3" /> <c bd="3"/> <doc> I'd like to be able to retrieve the names of the attributes (ba, bb, bc, bd) rather than the values inside SQL Server 2005 . Well, XPath certainly allows this with name() but SQL doesn't support that. This is my chief complaint with using XML in SQL; you have to figure out which parts of the XML/Xpath/XQuery spec are in there. The only way I can think of to do this is to build a CLR proc that

HttpWebRequest runs slowly first time within SQLCLR

我的未来我决定 提交于 2019-12-04 09:39:53
问题 When making an HttpWebRequest within a CLR stored procedure (as per the code below), the first invocation after the Sql Server is (re-)started or after a given (but indeterminate) period of time waits for quite a length of time on the GetResponse() method call. Is there any way to resolve this that doesn't involve a "hack" such as having a Sql Server Agent job running every few minutes to try and ensure that the first "slow" call is made by the Agent and not "real" production code? function

SQLCLR Function and System.Runtime.Serialization In GAC

左心房为你撑大大i 提交于 2019-12-04 07:31:38
I've built a SQLCLR function in C# that will deserialize JSON and return a table. The problem I have is getting the correct assemblies within SQL Server 2012. In order to utilize Newtonsoft's deserializer I've had to add the following assemblies to SQL Server: System.ServiceModel.Internals.dll SMDiagnostics.dll System.Runtime.Serialization.dll Newtonsoft.Json.dll This has all gone as planned but when I try to run my function I get the following error: System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken

T-SQL (varchar(max) vs CLR (string, SqlString, SqlChars)?

梦想与她 提交于 2019-12-04 03:57:35
I have a problem, that I can't solve. I'm using SQL Server 2005, C# CLR for using outer dll. The problem is at length of parameter. I need to use as function parameter type varchar(max) . If at C# code I use string , SqlSring , I can't use T-SQL type varchar(max) , just varchar(4000) of nvarchar(4000) . I need to say, that can be situations, when I need to use more then 4000 symbols, so I need know, what C# type I need to use for varchar(max) . I have read a lot of articles, and several of them say, that for this I can use SqlChars . But! I have manipulations with strings. How can I have

TSQL: Is there a way to limit the rows returned and count the total that would have been returned without the limit (without adding it to every row)?

99封情书 提交于 2019-12-04 03:09:09
I'm working to update a stored procedure that current selects up to n rows, if the rows returned = n, does a select count without the limit, and then returns the original select and the total impacted rows. Kinda like: SELECT TOP (@rowsToReturn) A.data1, A.data2 FROM mytable A SET @maxRows = @@ROWCOUNT IF @rowsToReturn = @@ROWCOUNT BEGIN SET @maxRows = (SELECT COUNT(1) FROM mytableA) END I'm wanting reduce this to a single select statement. Based on this question , COUNT(*) OVER() allows this, but it is put on every single row instead of in an output parameter. Maybe something like FOUND_ROWS(

Any Best practice of doing table record insert with SQL CLR store procedure?

眉间皱痕 提交于 2019-12-03 22:47:32
Recently we turned a set of complicate C# based scheduling logic into SQL CLR stored procedure (running in SQL Server 2005). We believed that our code is a great SQL CLR candidate because: The logic involves tons of data from sQL Server. The logic is complicate and hard to be done using TSQL There is no threading or sychronization or accessing resources from outside of the sandbox. The result of our sp is pretty good so far. However, since the output of our logic is in form of several tables of data, we can't just return a single rowset as the result of the sp. Instead, in our code we have a

TSQL - Executing CLR Permission

隐身守侯 提交于 2019-12-03 16:22:38
I got a sql procedure from a CLR (.net Assembly) that when executed returns an error Msg 6522, Level 16, State 1, Procedure sp_HelloWorld, Line 0 A .NET Framework error occurred during execution of user defined routine or aggregate 'sp_HelloWorld': System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. System.Security.SecurityException: at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at