Skip to content

Instantly share code, notes, and snippets.

DECLARE @text VARCHAR(50) = 'a;d;c;b;e';
-- Split (with ';' delimiter)
DECLARE @sqlnet SQLNET = SQLNET::New('Regex.Split(input, ";")')
SELECT *
FROM dbo.SQLNET_EvalTVF_1(@sqlnet.ValueString('input', @text))
-- Split && Distinct
SET @sqlnet = @sqlnet.Code('Regex.Split(input, ";").Distinct()')
CREATE FUNCTION [dbo].[fn_Split](
@sInputList VARCHAR(8000),
@sDelimiter VARCHAR(8000) = ','
)
RETURNS @List TABLE ( item VARCHAR(8000) )
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter, @sInputList, 0) <> 0
BEGIN
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// The first call perform a database round trip
var countries1 = ctx.Countries.FromCache().ToList();
// Subsequent calls will take the value from the memory instead
var countries2 = ctx.Countries.FromCache().ToList();
// Example from Entity Framework Extensions Library
// BulkInsert extension methods added automatically
using (var ctx = new EntitiesContext())
{
// Do not use the ChangeTracker or require to add the list in the DbSet
ctx.BulkInsert(list);
}
DECLARE @sqlnet SQLNET = SQLNET::New('Regex.Split(input, "/")')
SELECT *
FROM SourceTable
CROSS APPLY dbo.SQLNET_EvalTVF_1(@sqlnet.ValueString('input', DelimitedString))
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// Oops! All customers are cached instead of the customer count.
var count = ctx.Customers.FromCache().Count();
// The count is deferred and cached.
var count = ctx.Customers.DeferredCount().FromCache();
// using Z.EntityFramework.Plus; // Don't forget to include this.
var ctx = new EntitiesContext();
// Cache queries with related tags
var states = ctx.States.FromCache("countries", "states");
var stateCount = ctx.States.DeferredCount().FromCache("countries", "states", "stats");
// Expire all cache entries using the "countries" tag
QueryCacheManager.ExpireTag("countries");
DECLARE @tableFormula TABLE (Formula VARCHAR(255), X INT, Y INT, Z INT)
INSERT INTO @tableFormula
VALUES ( 'x+y*z', 1, 2, 3 ),
( '(x+y)*z', 1, 2, 3 )
-- Select_0: 7
-- Select_1: 9
SELECT SQLNET::New(Formula).ValueInt('x', X).ValueInt('y', Y).ValueInt('z', Z).EvalInt()
FROM @tableFormula
DECLARE @items TABLE (Quantity INT, Price MONEY)
INSERT INTO @items
VALUES ( 2, 10 ),
( 9, 6 ),
( 15, 2 ),
( 6, 0 ),
( 84, 5 )
DECLARE @customColumn SQLNET = SQLNET::New('(quantity * price).ToString("$#.00")')
CREATE PROCEDURE [dbo].[Select_Switch] @x INT, @y INT, @z INT
AS
BEGIN
DECLARE @result INT
SET @result = SQLNET::New('
switch(x)
{
case 1: return y + z;
case 2: return y - z;