Giter Site home page Giter Site logo

zzzprojects / eval-sql.net Goto Github PK

View Code? Open in Web Editor NEW
95.0 14.0 40.0 882 KB

SQL Eval Function | Dynamically Evaluate Expression in SQL Server using C# Syntax.

Home Page: https://eval-sql.net/

License: Other

C# 100.00%
sql sql-server eval evaluator evaluation regular-expression expression-evaluator splitting-text

eval-sql.net's Introduction

What's Eval-SQL.Net?

Eval SQL.NET is a library that allows evaluating C# expression dynamically directly in T-SQL.

It provides to your SQL Server all missing pieces like regular expression and dynamic arithmetic string evaluation.

-- SELECT 3
SELECT  SQLNET::New('x+y').ValueInt('x', 1).ValueInt('y', 2).EvalInt() as Result

Try it online

Find your solutions:

  • Dynamic Arithmetic Expression
  • Dynamic Pivot Table
  • Regular Expression
  • String Interpolation
  • Replace xp_cmdshell with DirectoryInfo & FileInfo

Performance & Scalability

Performance tuning is one of the most important tasks for a DBA. Don’t miss the chance to dramatically improve query performance by 300% for simple expression and more than 2000% for complex code over User-Defined Function (UDF) and Table-Valued Function (TVF).

Benchmark to split string with delimiters in SQL

Methods 1,000 rows 10,000 rows 100,000 rows 1,000,000 rows
Eval-SQL.NET 4 ms 13 ms 160 ms 1,650 ms
fn_split (TVF) 100 ms 625 ms 5,500 ms 55,000 ms

Download

Eval-SQL.NET-Install.sql

* PRO Version unlocked for the current month

Minimum Requirements:

  • SQL 2012 / SQL Azure v12
  • SAFE Permission (SQL CLR)

Evaluate dynamic arithmetic/math expression in SQL

Make the impossible now possible. Evaluate C# expression in SQL to overcome limitations.

  • Allow trusted users to create report field and filter
  • Consume Web Service
  • Replace text in the template with String Interpolation
-- CREATE test
DECLARE @table TABLE ( X INT, Y INT, Z INT )
INSERT  INTO @table VALUES  ( 2, 4, 6 ),  ( 3, 5, 7 ), ( 4, 6, 8 )

-- Result: 14, 22, 32
DECLARE @sqlnet SQLNET = SQLNET::New('x*y+z')
SELECT  @sqlnet.ValueInt('x', X)
               .ValueInt('y', Y)
               .ValueInt('z', Z)
               .EvalInt() as Result
FROM    @table

Try it online

Split text with delimiter

Improve performance and capability for splitting text with an easy to use split function and LINQ expression

  • Split text with multiple delimiters
  • Split text using a regular expression
  • Include row index
-- CREATE test
DECLARE @t TABLE (Id INT , Input VARCHAR(MAX))
INSERT  INTO @t VALUES  ( 1, '1, 2, 3; 4; 5' ), ( 2, '6;7,8;9,10' )

-- SPLIT with many delimiters: ',' and ';'
DECLARE @sqlnet SQLNET = SQLNET::New('Regex.Split(input, ",|;")')

SELECT  *
FROM    @t AS A
        CROSS APPLY ( SELECT    *
                      FROM      dbo.SQLNET_EvalTVF_1(@sqlnet.ValueString('input', Input))
                    ) AS B

Try it online

Use regular expression in SQL Server

Use Regex flexibility to overcome “LIKE” and “PATHINDEX” limitations.

  • IsMatch
  • Match
  • Matches
  • Replace
  • Split
DECLARE @customer TABLE ( Email VARCHAR(255) )

INSERT  INTO @customer
VALUES  ( '[email protected]' ),
        ( 'invalid.com' ),
        ( '[email protected]' )

DECLARE @valid_email SQLNET = SQLNET::New('Regex.IsMatch(email, 
@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")')

-- SELECT 'invalid.com'
SELECT * FROM @customer WHERE @valid_email.ValueString('email', Email).EvalBit() = 0

Try it online

Replace xp_cmdshell with restrictive alternative

Avoid enabling xp_cmdshell and compromising your SQL Server and use a more restrictive solution instead.

  • Impersonate Context
  • Improve maintainability
  • Improve readability
  • Improve security
-- REQUIRE EXTERNAL_ACCESS permission
DECLARE @sqlnet SQLNET = SQLNET::New('
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

var dir = new DirectoryInfo(path);
return dir.GetFiles("*.*")
          .Select(x => new { x.FullName, FileContent = File.ReadAllText(x.FullName) })
          .OrderBy(x => x.FullName)')
          .Impersonate()

-- SELECT FullName, FileContext FROM DesktopFiles ORDER BY Fullname
EXEC dbo.SQLNET_EvalResultSet @sqlnet

FREE vs PRO

Features PRO Version
Maximum Characters Unlimited
Commercial License Yes
Support & Upgrades (1 year) Yes

Learn more about the PRO Version

Contribute

The best way to contribute is by spreading the word about the library:

  • Blog it
  • Comment it
  • Star it
  • Share it

A HUGE THANKS for your help.

More Projects

To view all our free and paid projects, visit our website ZZZ Projects.

eval-sql.net's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eval-sql.net's Issues

Error while installing on SQL Server 2017

I'm getting the following error while running on SQL Server 2017:

[2018-02-13 14:32:02] [S0001] An error occured: CREATE or ALTER ASSEMBLY for assembly 'Z.Expressions.Compiler' with the SAFE or EXTERNAL_ACCESS option failed because the 'clr strict security' option of sp_configure is set to 1. Microsoft recommends that you sign the assembly with a certificate or asymmetric key that has a corresponding login with UNSAFE ASSEMBLY permission. Alternatively, you can trust the assembly using sp_add_trusted_assembly.

Here's the SQL Server info:
Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)
Aug 22 2017 17:04:49
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows 10 Pro 10.0 (Build 16299: )

Regex expressions didn't work

I've downloaded this powerful feature but was sad when in your exampes all functions are working except of Regex functions. I've downloaded this software only for solving this task. Can you please solve the case?

SQL Server 2019 Express - don't can load the assembly

When I call the statement ...where SQLNET::New('Regex.IsMatch(TTel, @"([^0-9\-\+ ()\.\/\[\]\|])")').ValueString('TTel', isnull(M.TTel, '')).EvalBit() = 1 I get the mistake

Msg 10314, Level 16, State 14, Line 6
An error occurred in the Microsoft .NET Framework while trying to load assembly id 65543. The server may be running out of resources, or the assembly may not be trusted. Run the query again, or check documentation to see how to solve the assembly trust issues. For more information about this error:
System.IO.FileLoadException: Could not load file or assembly 'z.expressions.sqlserver.eval, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An error relating to security occurred. (Exception from HRESULT: 0x8013150A)
System.IO.FileLoadException:
at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.Load(String assemblyString)

I made the installation to the first on an SQL Server 2019 Express. I don't get this mistake by SQL Server 2019 Standard.

How can I correct the mistake?

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.