Passing Arrays to a Stored Procedure - sql-server-2005

What is the best, most preferable (and, if possible, efficient) way to pass an array of strings from a .NET application to a SQL Server 2005 stored procedure?
1) Pass the array of strings as a comma- or semicolon-delimited string and parse it into a temporary table?
2) Pass the array as XML and use SQL Server 2005 methods to parse it into a temporary table?
3) Write an ancilliary stored procedure to deal with each string individually and having .NET calling it for every element in the array?
4) Other... What?
I would say solution 2) is the most elegant one, but it is certainly not the most efficient... Is it?

Read this article.
Arrays and Lists in SQL Server 2005
The other related articles:
Arrays and Lists in SQL Server
Edit:
Sorry I did not realized that it is already mentioned in comments.

It depends upon your requirements.
In case I am importing then pass the XML and use SQL Server 2005 methods to parse it into a temporary table
If the records are not in bulk, I can use semicolon-delimited string.
If it is like you have some enumeration values like filtering the records on the basis of multiple status then I can use In
Good and Bad points about array passing
Hope this will help you. :)

Related

How can I split a string value in a SQL Server CE query?

I have a table containing a nvarchar column [AffectedNodes] that looks something like this when you take a peek at its contents (two variations shown):
"MID128; MID129; MID130"
"[1,3,2]; [3,1,2]"
We are working on a change which will move the AffectedNodes into its own table [AffectedNode] that has a nvarchar column [NodeId], which should store one of the nodes from the above string. I'm tasked with migrating the existing content to the new format.
As you can see the values are split using semi-column and a space.
To follow the database upgrade process they use in our project I have to write an SQL query in SQL Server CE. I'm wondering how I could do this in a neat way. Thanks!
SQL CE supports, CHARINDEX and SUBSTRING function, did you check that out or did you see any issue with them?

SSIS Variable SQL command for ADO.net source

While this seems like a basic problem, I've been ripping my hair out FOR DAYS trying to get an efficient solution to this.
I have a lookup table of values on a server that I read from and assemble into a string using a C# Script task. I write this string into a variable that I want to pass in as my WHERE parameters inside a large SQL query on a ADO.NET data source (from a different server which I only have read access to) in my data flow. For example, this string would just be something like
('Frank', 'John', 'Markus', 'Tom')
and I would append that as my WHERE clause.
I can't read from a variable directly for an ADO.NET data source AND I can't use the 'Expression' property to set my SQL either as my SQL query is over 4000 characters. I could use an Execute SQL Task to run my query, load the results into a recordset and I assume, then loop through the recordset but that's extremely inefficient.
What would be the best way to do this? My end goal is to put these results inside a table on the first server.
You could try to set up Script Component as source - variables and strings inside scripts can be longer than 4000 characters so you can fit your query inside.
Setup your component similar to this article: http://beyondrelational.com/modules/2/blogs/106/posts/11119/script-componentsource-part1.aspx
In this one you have example how to fetch data using ExecuteReader and put it to output of script component: http://beyondrelational.com/modules/2/blogs/106/posts/11124/ssis-script-component-as-source-adonet.aspx In this one you have instructions how to aquire connection properly: http://www.toadworld.com/platforms/sql-server/b/weblog/archive/2011/05/30/use-connections-properly-in-an-ssis-script-task
By joining this pieces of information you should be able to write your source Script Component which can fetch data using any length dynamically constructed query.
Good luck :)
You can do a simple select statement to return a list of values that will include ('Frank', 'John', 'Markus', 'Tom'). So your select would return :
Name
----------
Frank
John
Markus
Tom
Then, in SSIS, use a Merge Join Component (that will act as a INNER JOIN) instead of a where clause in your main query.
This is the cleaniest way to achieve what you want.

Create delimited string from a row in stored procedure with unknown number of elements

Using SQL Server 2000 and Microsoft SQL Server MS is there a way to create a delimited string based upon an unknown number of columns per row?
I'm pulling one row at a time from different tables and am going to store them in a column in another table.
A simple SQL query can't do anything like that. You need to specify the fields you are concatenating.
The only method that I'm aware of is to dynamincally build a query for each table.
I don't recall the structure of MSSQL2000, so I won't try to give an exact example, maybe someone else can. But there -are- system tables that contain table defintions. By parsing the contents of those system tables you can dynamically build the necessary query for each source data table.
TSQLthat writes TSQL, however, can be a bit tricky to debug and maintain :) So be careful how you structure everything...
Dems.
EDIT:
Or just do it in your client application.

Using an sql-function when reading and writing with NHibernate

I have the following problem.
I have a special column in my tables (a blob). In order to read and write to that column, I need to call an SQL function on its value - to convert to a string when I read, and to convert from a string to this blob when I write.
The read part is easy - I can use a formula to run the sql function against the column. But formulas are read only. Using IUserType also did not seem to assist - I can get the blob and write my own code to convert it to my own type, but I don't want to do that - I already have a database function that does this work for me.
Any ideas?
You can specify sql to insert and update, see the reference documentation, "Custom SQL for create, update and delete". Here is an example from Ayende which uses stored procedures (which is not the same, just to see how it works).
Or you could write a database trigger which does this transformation.

MySQL - explode/split input to stored procedure

I have problem, I need to explode my input to my stored procedure, but don't know how I can do it.
My stored procedure has a VARCHAR(256) input which I need to split and generate insert statements.
i what to explode this varchar "1,2,3,7,8,9" so I need to split that string on "," and iterate through the result
Yes, please see this forum thread on replicating the functionality in mysql that tsql provides.
That thread also discusses some of the downfalls of this method. Also, I think you want to be using VARCHAR(255) or just VARCHAR(MAX) because there is no benefit to a 256, it uses a 2 byte size prefix and yet only stores 256 characters.
Have you considered using xml and xpath syntax to extract the values instead? I think going forward this will become a more readable and maintainable method.
Here is a split_string() function
http://forge.mysql.com/tools/tool.php?id=4
Try my SQL Library: http://ondra.zizka.cz/stranky/programovani/sql/mysql_stored_procedures.texy