Get all values from one column of a CSV string using Stored Procedure - sql

Within a stored procedure, I need to take a whole CSV file as a string, then pick out all the values in one "column" to do a further query on the database.
I cannot use a saved doc - so i think that rules out openrowset, and the whole thing has to be done within a stored procedure.
Have spent hours googling and trying, but can find a good answer. One possible was http://www.tainyan.com/articles/entry-32/converting-csv-to-sql-data-table-with-stored-procedure.html but it doesnt work and i can find the error.
How should this be done please?

I don't really like this but it will work, provided your csv column remains at the same column index. I'd be wary of the performance of this but it might work.
See Fiddle here: http://sqlfiddle.com/#!3/336b7/1
Basically convert your csv file to xml, cast to an xml type, then perform queries on the xml.

Related

Fill SQL temp table from results AFTER execution of SPROC

Is there a way to fill a temp table/other SQL variable after a SPROC has been executed?
I have the SQL results sitting in the SSMS results window, but I don't want to re-run the SPROC to fill a temp table because it takes over an hour.
I can export to CSV and re-import (using OPENROWSET, which is always difficult), but I was curious if there are any more elegant solutions?
I've run into this several times and have not found anything simple..
There's no way.
Also, watch out exporting from SSMS to CSV because
a)It formats things (especially dates and numbers) and
b)It truncates columns (43679 chars max, which might sound a lot but gets used up quickly if XML).
So your export might not be a true representation of results.

How to remove Nulls from Save As in SQL Server Management Studio

I have created a variable that is type table inside a stored procedure. At the end of the procedure I am selecting all the rows in the table and displaying them. When I right click on the headers and select "Save As" it allows me to change the type to All Files and save the file as a text file. This works fine except that the columns that have NULLS in them saves as NULLS. I want it to fill NULLs in with spaces.
I've been trying to find a way to create a file using a stored procedure but most things indicate to use SSIS but I can't figure out how to use SSIS with a variable that is a table instead of using an actual table.
If I could either replace nulls with spaces or use a stored procedure to do the same thing it would be great. I can not use tab or comma delimited as the final product has to be a flat file that each column uses the same amount of characters as is declared in the column headers. Padded with spaces.
Thanks for any help you are able to offer.
Cheers
P.S. I am using SQL Server 2012 Management Studio
The easy way to do this would be to convert the NULLs to spaces in your SELECT statement.
SELECT COALESCE(yourcolumn, '')
Put the COALESCE clause around every column that has NULLs in it.
Using COALESCE article link
If the last thing you do in the stored procedure is Select * From TempTable then you can use that SP in an OleDb source component. Change from Table or View to Sql Command and use the Exec (sp_SomeName) syntax. This will create a pipe that you can connect to a destination component, such as flat file.
I have seen many issues over the years doing Save Results As... I will only use this for informal 'quick check' files and not for anything considered 'live' or 'production' data.
Here is a good blog that also shows how to use parameters.
http://geekswithblogs.net/stun/archive/2009/03/05/mapping-stored-procedure-parameters-in-ssis-ole-db-source-editor.aspx

retrieve value from sql server and passing it to string to write it into txt file using VB .NET

I am retrieving some data from sql server and trying to write it to a text file, I am getting error in retrieving and passing it to variable Could you please help me in this.
You don't give a lot of detail regarding the error. Assuming your query is working and returning data to the table, Streamwriter.Write(dt) won't return the data in the table, it will return the name of the table or something like that. to get all the data you need to either loop through the rows and columns and print each one as you like or use dt.WriteXML(myIOStream), obWRiter.(myIOstream). You can also try streamwriter.write(dt.writexml()). I haven't tried anything out so you'll need to work on the code.

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.