Alternatives to SMO, i.e. DML APIs for SQL Server? - sql-server

I just learned I can't use SMO in ASP.NET without either SQL or a full SMO install on the application server. I'm not allowed to do that, so I can't use SMO.
What other libraries do the same thing as SMO but don't require an MSI installer or COM registrations?
I know I could send DDL to the server inside ADO.NET commands, but that is exactly what I was trying to avoid by using SMO.
What was nice about SMO:
Object oriented API for querying meta-data (columns, data types) that didn't rely on inconsistent COBOL-like DDL.
Didn't require querying undocumented stored procedures, system stored procedures or tables which break every few versions.
Off the top of my head I can think of ADOX and DMO, but both were COM based APIs.

SMO is running T-SQL under the covers. You could prototype in SMO and then watch in profiler to get the T-SQL.
It is probably an EULA violation, but you could redistrib the SMO assemblies side-by-side with your app, nothing to install in that case. I don't think their installer hits the registry. Pretty easy to bust open the SQLServerManagementObjects.msi and find out.

Related

SQL Server CLR vs Python vs R

Can somebody please explain the various scenarios that would make one choose SQLCLR vs Python vs R.
I understand that R is a language and a library specifically designed for statistical analysis and data mining so I understand leveraging that capability when appropriate, but can R (on SQL Server) do more and call additional external libraries like CLR assemblies can?
Is Python meant as an eventual replacement for C# SQLCLR? It seems to me, from what I've read, that Python can simply be embedded inside a stored procedure and then interpreted upon execution as opposed the compiled nature of CLR assemblies, but otherwise the capabilities are the same? Are they?
I'll try to answer your questions:
SQLCLR was introduced in SQL Server 2005, as a way to embed CLR (.NET) in the SQL Server engine. E.g with SQLCLR your .NET code is running in the same memory and process space as SQL Server itself. The way it works (simplified) is that you create an assembly and registers it with SQL Server (CREATE ASSEMBLY). You then create "wrapper" T-SQL stored procedures/functions/triggers etc., against your .NET methods, and it is these procs that you execute at runtime.
R was introduced in SQL Server 2016, and Python in SQL Server 2017 in order to give SQL Server machine learning capabilities. As opposed to .NET, neither R nor Python run embedded in SQL Server, but when you call R/Python code inside SQL Server, calls are made out to the R/Python engine sitting outside SQL Server's memory/process space. This is an important distinction between SQLCLR and R/Python:
SQLCLR code executes in-memory/in-process with SQL Server
R/Python executes outside of SQL Server.
As a side note; I have a series of blog-posts discussing the internals of SQL Server R Services (even if the posts talk about R, everything in there are applicable to Python as well).
As for capabilities; R/Python in SQL Server can do no more, no less than what "standalone" R/Python can do: as mentioned above the actual execution of R/Python happens outside of SQL Server as well.
Personally I do not think Python is a replacement for .NET in SQL Server, I see it as an additional tool in your toolbox. Where I work we use both SQLCLR as well as R/Python (in SQL Server). We have 100's of SQLCLR assemblies in our production databases, doing weird and wonderful things (sending messages to RabbitMQ etc., etc.), and IMHO it'd be very hard to replace that with Python, especially seeing that you'd immediately get a perf degradation - compiled code (SQLCLR) vs. interpreted code (R/Python).
Hope this helps.
Niels

Deploying a CLR Assembly to a different database than the rest of the objects

I am using Visual Studio and a SQL Server Database Project to maintain my database objects through TFS. I have implemented a CLR Assembly for using RegEx Pattern matching in my UnitTests. I want to deploy this CLR to a different database than where the rest of the objects in the database would be deployed to.
Database_1 - contains all of the database objects
Database_2 - contains the CLR Assembly and UDFs for RegEx Pattern Matching.
I would like to create the script to deploy a CLR Assembly and related UDFs to one database, while creating the script which will deploy the rest of the changes to another database. I would like to do this using PRE/POST scripts using sqlcmd as I think this is the easiest way to do this.
Is this possible?
Publishing to another Database requires a different Database Project. They should be able to be in the same solution, but having two different Database projects will allow for different publishing properties (e.g. Target Database, etc).
OR, a very quick and easy to not worry about any of that would be to use the SQL# SQLCLR library of functions (which I wrote). The free version of SQL# contains several RegEx functions as well as many others. You can just save the installation script (once the correct Database name has been edited into the USE statement) into TFS and be done with it :-).

What is a SSMS analogue for Oracle?

What is (if there is) the analogue of SQL Server Management Studio for Oracle databases?
Question seems easy but Google didn't know a thing.
I've never worked with Oracle database, now I will have my first encounter soon so I'd like to be a little prepared.
It depends. There are a number of applications that could be used depending on what specifically you are trying to do.
If you are trying to administer the database, you would probably want to use Enterprise Manager. This is a web-based application that lets you monitor and administer the database. It can either be configured to run just on a particular database server (Enterprise Manager Database Control) or it can be configured to allow you to access all the databases and a variety of non-database products running in the organization (Enterprise Manager Grid Control). Grid Control is, obviously, a much more involved install. The Database Control should be installed when you install the database (though some DBAs will turn it off because they don't want to run a HTTP server on their database server).
If you are trying to write and debug PL/SQL code (packages, procedures, functions, triggers, etc.) or to just run some ad-hoc SQL statements, Oracle provides a free tool SQL Developer that can be used. There are a variety of other PL/SQL IDEs out there as well-- Toad from Quest and PL/SQL Developer from AllAroundAutomations are two of the more common ones.
Oracle also has a basic command-line SQL tool SQL*Plus that will exist pretty much wherever you are (in much the same way that vi will be available on just about any Unix machine you log in to). There are lots of DBAs (and a decent number of developers) that prefer to use SQL*Plus rather than using the various GUIs. At a minimum, in most large organizations, DBAs will use SQL*Plus to execute scripts built by the developers as part of the code promotion process so you'll want to have a basic familiarity with SQL*Plus.
You can use Oracle SQL Developer which is free from oracle.
Toad is also available.

Write a CLR stored procedure

I would like to use existing Delphi code to write SQL Server stored procedures.
In the past I used extended stored procedures, somehow a dll compiled by Delphi wrapped by a SQL Server extended stored procedure.
Extended stored procedures are now deprecated, so somehow I wonder if there is a solution, even in the "trick domain", like some sample CLR code that wraps a normal dll or something like that.
Just to give you an example:
I insert in the db some documents by encrypting them and I would like to create a kind of API based on SQL Server functions / procedures for inserting or reading documents, so other people accessing sql server can call those functions.
Of course an alternative solution is to use webservices but I would like to try the SQL Server way first.
Note: I don't want to use Delphi Prism, my code is for XE2.
Unsafe SQLCLR assemblies can p-invoke native dlls. A better way would be to expose the native DLL services as a COM interface and use COM interop from SQLCLR, or even call the COM APIs directly from SQL via OLE Automation Procedures. An even better way would be to rewrite the Delphi code as CLR code and invoke it directly as SQLCLR procedure. And the best way would be to use SQL Server native encryption features.
Not to mention the fact that CLR in SQL Server is a guaranteed deep performance hit. Keep to the standard CRUD operators and you should be fine. The other way to do it is to use the file system as your encryption mechanism. If you are only trying to prevent casual access to docs this is a fine way to go. Otherwise it might be time to rethink your access protocol.
CLR in SQL Server is a convenient bad idea. Use it sparingly if at all.

Stored Procedure Versioning

How do you manage revisions of stored procedures?
We have a BI solution on SQL Server 2005 with hundreds of stored procedures.
What would be a good way to get these into Subversion? What are your recommended tools to script stored procedures to files?
There are doubtless a bunch of off-the-shelf products you could buy (I think a few RedGate tools might come in handy here), as well as Visual Studio Team Suite - Database Edition.
In light of purchasing something, why not consider using SQL Management Objects (SMO)?
I've written a couple of utilities which generate T-SQL scripts (using the Scripter class) which produces the same scripts you get from generating scripts through the SQL Server Management Studio (it uses the same functionality).
You could integrate such a utility into a build script/build process which would allow you to generate scripts and then version & check them into a source repository. Plus, you can batch the scripts into a single file (if desired) which beats maintaining hundreds of individual files.
I wrote a blog entry about this approach a while back.
Check out more on the SMO class Scripter
Here's a few more entries which might be useful:
http://www.sqlteam.com/article/scripting-database-objects-using-smo-updated
http://sqlblog.com/blogs/ben_miller/archive/2007/10/03/table-scripting-with-smo-part-1.aspx
See here and here for a start.
Please check out here What is the best way to version control my SQL server stored procedures?. Might help you identify couple of solutions to this issue.
I have previously used a Visual Studio Database Project to manage create table scripts, stored procedure scripts etc. I'm fairly sure you could then use subversion to manage these files in the same way as any Visual Studio project.
I used the built in functionality for scripting the procs, but i'm sure Redgate would have some tasty tools for that.

Resources