Performance issue: t-sql vs clr - sql-server

I am just wondering which will be faster t-sql function/procedure or clr version of one? A procedure works with database data and use cursors (t-sql version).
When should I use clr and when I should use t-sql to create procedures and functions?

Simple rule-of-thumb:
data manipulation (SELECT, UPDATE etc.) are best left to T-SQL (but without cursors!)
while anything that has to do with processing (string/regex matching, date arithmetic, calling external web services etc.) is a good match for SQL-CLR

Related

Is there a way to run Visual FoxPro code from a SQL Server CLR or CMD?

Apologies for the strange question here.
Long story short is that we have a Visual FoxPro .prg that will execute a query using SQLEXEC(), and then uses AFIELDS() to get Field Name, Field Type, Field Width (length), and Decimal Places of the resultset columns to store into a SQL table (even if selected from a temp table or dynamic SQL).
What I'm looking for it to be able to run this .prg from a SQL Server CLR, or even using xp_cmdshell without having to manually open a Visual FoxPro Application.
I've already attempted to recreate this functionality in a CLR alone but kept running into issues with nested INSERT INTO statements when using it as a CLR Stored Procedure.
Additionally, I've attempted to create as a CLR SQL Function, but it would attempt to execute our custom hooks and throw "Procedure does not exist" errors, even when surrounded with IF Object_ID() IS NOT NULL.
Also, as some queries may be results of a temp table, I am unable to use OPENQUERY or OPENROWSET.
The end goal here is to move away from our Visual FoxPro client entirely, but the ability to get the column metadata of anything thrown at it is holding us back.
(Not an answer really, writing here as in a comment it would be a mess)
If I understood you right, the table in question is a VFP table and have some stored procedure functions (maybe for insert\update\delete triggers, or validation check).
If that is the case, I am afraid you are out of luck. Your best bet might be having a VFP COM object in between or a VFP SP using SetResultSet() - that might fail if there are unsupported VFP commands.
On the other hand you are saying it starts with an SqlExec(), then likely it is not a VFP table. Then likely you could create a CLR function. Would it be possible for you to share more details, along with the VFP and C# codes.

MS-Access - Setting parameters for pass-through query

I have a pass-through query (for SQL Server) in Access which works with explicit values. For a non-pass-though query, I just use the visual editor to create parameters for the affected columns.
Is it possible to do the equivalent with a pass-through query or must I assemble the query with VBA (as mentioned in another post)?
TIA,
Paolo
Unfortunately, you cannot. It would be once possible with ODBCDirect workspace but that was removed since Access 2010. Therefore, for pass-through queries, you would have to concatenate SQL.
A possible alternative is to use ADO which allow you to create parameters and thus construct a command and execute it. Note that your project doesn't have to be limited to only DAO; you can use both DAO and ADO, leveraging what works best for your requirements.

CLR Stored Procedure v regular SQL Stored Procedure

I have been writing a CLR stored procedure that moves data from one database to another. I went with the CLR stored procedure because I like the .NET framework's ability to connect to remote servers better than I like linked servers, or openrowset, but I now find that my class is mostly embedded SQL strings. I was considering just using the CLR stored procedures to retrieve the data onto the local SQL Server, and then using a regular SQL stored procedure for the actual inserts and updates.
I'm not worried about pre-compilation of the procedure or performance, and I do like that the CLR procedure allows me to see all of the logic in one place, read from top to bottom.
Are there any reasons I should consider moving to a TSQL solution instead of CLR?
Thanks.
There are multiple reasons why you would stick to a regular stored procedure. I'll try to give you an overview of the ones that I know of:
Performance.
Memory issues. SQL Server only operates with its own max memory settings. CLR's go out of this bound. This could comprimise other applications (and the OS) running on this server.
Updatebility. You can update a Stored procedure with a simple script. CLR's are more complicated to update
Security. CLR's often require more security settings than regular t-sql.
As a general rule you only want to use CLR for:
interaction with the OS, such as reading from a file or dropping a message in MSMQ
performing complex calculations, especially when you already have the code written in a .NET language to do the calculation.

Compatible DDL (CREATE TABLE) across different SQL databases?

I'm working on a desktop application that must support (currently) MS Access and SQL Server back ends. The application is under constant development and changes are frequently being made to the database, mostly the addition of tables and views to support new features (but also some DROPs and ALTER TABLEs to add new columns).
I have a system that compiles the DDL into the executable, checks the database to see if the executable has any new DDL that has to be executed, and executes it. This works fine for a single database.
My immediate problem is that SQL Server and Access support wildly different names for data types so a CREATE TABLE statement that executes against Access will not execute against SQL Server (or worse, will execute but create a table with different datatypes).
Is there a method that can be used to create DDL (especially CREATE TABLE commands) that can be executed through ADO against both of these databases without having to craft separate DDL for each provider?
Since you are already using ADO, you should look into Microsoft ADOX
This allows you to manipulate structures in a data source using an ADO object model that is independent of the underlying data source type. i.e. without resorting to explicit DDL
Support for ADOX is not guaranteed by any given ADO Provider, and the level of ADOX support may vary even when it is available. But for MS Access and MS SQL Server I think you will find all the capability you require (and quite possibly more!)
This can be done using DBX in Delphi.
The following is links to sample code showing how this can be done.
http://cc.embarcadero.com/item/26210
I had the same problem.
I resolved it applying a C preproccessor to the SQL before executing it.
The preprocessor includes macros in order to handle the different dbs.
Stefano
Did you check
http://db.apache.org/ddlutils/
or
http://publib.boulder.ibm.com/infocenter/wtelecom/v6r1/index.jsp?topic=%2Fcom.ibm.twss.plan.doc%2Fdb_scripts.html

SQL Stored Proc : How to pass a collection of files to a stored proc?

My program has code that saves attachments, I want these attachments to be transferred to the database, and I am going to use a stored procedure to accomplish this.
I need to know what is the #param type to accept an array of binary files?
finally once I have this array, how to I insert this data into a SQL table?
I guess I am looking at using a byte[] for 1 file, but how do I pass from C# or .net a collection of byte arrays to the SP, and what should the param type be to accept this array of byte[]
Updated
Need a solution that will work in 2005 and 2008.
Update
I've decided to scrap the idea of having 1 large SP to process everything. Instead I am going to have smaller SPs, then handle the transaction in .net.
Do you think this would be a better solution, to handle the transaction in .net data objects?
If you were needing only to pass an array of values, I would suggest to format them as XML and pass them as the SQL xml datatype. You can perform select from XML with SQL just as easily as from a table.
It may also technically work if you pass binary data in XML, but I'm not sure it will be a good solution. Anyway, it is an option.
For SQL Server 2008, you can use the table-valued parameter feature.
This allows you to define a parameter of a table type, and then supply values to that parameter as a DataTable from your C# code.
See these blog posts and articles for more information:
SQL Server 2008: Table-valued parameters
Using Table-Valued Parameters in SQL Server 2008
This is a new feature of SQL Server 2008, so you won't have this in SQL Server 2000 or 2005.
UPDATE: if you need to support SQL Server 2005 as well, check out Erland Sommarskog's excellent article Arrays and Lists in SQL Server 2005. It offers a few ideas on how to accomplish this in 2005.
Marc
See Arrays and Lists in SQL 2005. This article aggregates pretty much every technique there is out there and discusses the advantages and problems with each approach.
I decided to create just one SP, and call it multiple times, one for each file I need uploaded, and keep the transaction managed in C# code.

Resources