Read contents of a text file into a varchar WITHOUT using BULK - sql

I've got a problem whereby I need to read data from a .txt file into a variable in SQL Server. The read needs to be performed programmatically, as it's going to form part of a stored procedure, and it needs not to utilise the BULK method, as I don't have permissions to use the BULK method on the database in question. Is this possible?
Thanks in advance :)

Can you get them to allow Ad Hoc Distributed Queries? Then you can use OpenRowset or OpenDatasource.
SELECT *
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=c:\users\graham\desktop;',
'SELECT * FROM [data.txt];'
Here's the recofiguring code, if you need it:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
go
This is a laborious technique, though -- you sure you can't use client code? Even, I dunno, VBA in Excel or something?
g.

There is no other way to read the contents of a file without these permissions in SQL Server, or without setting up a link to the file. Otherwise, you have to do this through SSIS or using programming.
The solution is to get the permissions to solve your issue.

Related

Activate FileStream on SQL Server 2012 only with code (query)

I'm desperately trying to activate FileStream only with queries. By following the MSDN procedure everything is ok, but I would like to enable it without using SQL Server Configuration Manager or SQL Server Management Studio.
On several website I read that this query was enough to activate this feature (this query is done in the MSDN procedure but after some configuration in SQL Server Configuration Manager ) :
EXEC sp_configure 'filestream_access_level', 2;
GO
RECONFIGURE
GO
The message L option de configuration filestream access level est passée de 2 à 2. Pour installer, exécutez l'instruction RECONFIGURE." is gave but if I check the service's proprieties Filestream is steal disabled, even if I execute RECONFIGURE again. If I try to open a connection to a base featuring Filestream columns i get a message confirming Filestream is not enabled.
I also tried this query :
USE master
GO
EXEC sp_configure 'show advanced options'
GO
EXEC sp_configure filestream_access_level, 3
GO
EXEC sp_filestream_configure
#enable_level = 3
, #share_name = N'FS';
GO
RECONFIGURE WITH OVERRIDE
GO
Without succes, althougth the sp_filestream_configure seems to not exist.
This website tell the issue could be the default language of my server, but after change the default language to English nothing was better.
https://stackoverflow.com/questions/1...tream-access-l
I hope someone will be able to help me. Thanks. Sorry for my bad english, I'm french ;)
You have to do both. One is enabling the feature in the configuration manager. The other is setting the access level.
Here's the MSDN guide to walk through:
https://msdn.microsoft.com/en-us/library/cc645923.aspx
You're only doing step 11, you need to do the first 10 steps as well.

Import XML file into SQL Server without BULK

I'm trying to import data from a XML file into a database. I've made a test script using BULK INSERT, but when I've tested on the live database, permissions for BULK are disabled.
Attempt #1:
BULK INSERT XMLTable FROM 'C:\Radu\test.xml' WITH (ROWTERMINATOR = '>');
So, I have continued research to find a way to avoid using BULK and found other options, such as OPENROWSET and OPENDATASOURCE. But unfortunately, rights for these operations are also revoked.
Attempt #2:
SELECT *
FROM OPENROWSET('MSDASQL',
'Driver={Microsoft Text Driver (*.xml)};DefaultDir=C:\Radu\test.xml;',
'SELECT * FROM [test.xml];' )
resulted in an error:
Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online.
I've tried to RECONFIGURE the permissions for this, but with no success.
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
also resulted in:
Msg 15247, Level 16, State 1, Procedure sp_configure, Line 94
User does not have permission to perform this action.
Msg 5812, Level 14, State 1, Line 2
You do not have permission to run the RECONFIGURE statement.
Msg 15123, Level 16, State 1, Procedure sp_configure, Line 51
The configuration option 'Ad Hoc Distributed Queries' does not exist, or it may be an advanced option.
Msg 5812, Level 14, State 1, Line 4
You do not have permission to run the RECONFIGURE statement.
I'm still trying to find a solution just to import the information into the database, I don't need a special format, just to get the data in the system, although ideally I would like to import each xml line as a record in my table.
It appears that most of my options are cut off, so I would very much appreciate any suggestions.
UPDATE:
I am going to create a stored procedure to import this data, so ideally this would be generic, no hardcoding with the XML information.
For BULK INSERT operation, you need administrator privileges for that database. You can take XML data in XML variable and insert into table as mentioned in this link :
Import 'xml' into Sql Server
Since you dont have bulk upload permissions then you need some tool to convert the XML into XSLX or XSL then open it and copy all the rows and paste it in the table.
Or
This still uses a bulk load component, try it, if it get worked.
http://support.microsoft.com/kb/316005

Opening a batch file using SQL Server procedure

Opening a batch file using SQL Server procedure....
For this, I took the example of opening a notepad. And able to open it with the following code :
start "c:\windows\system32" notepad.exe
--- saved as : note.bat
I tried to open it with following procedure :
ALTER procedure [dbo].[executebat]
as
begin
EXEC master.dbo.xp_CMDShell 'D:\ducont\note.bat'
end
also I enabled the xp_cmdshell option with following code ::
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
But when I try to execute the procedure, it showing continuously
" Query Executing..."
and no output !!
Guide me if I went wrong anywhere .
Simple answer. When you run xp_cmdshell it is creating a shell to do any work you ask it to. Your bat file is running inside that shell, and will open notepad, again inside the shell. You just won't be able to see it. Since it's expecting input (and can't get any) you are stuck with the " Query Executing..."
There really isn't a good way to do what you are asking for other than possibly using CLR. Unfortunately I don't know enough about it to say for certain one way or another. There may be an easier way in SQL 2012 or 2014 but I haven't seen it yet.

I need best practice in T-SQL Export data to CSV (with header)

What I need to do is export data into CSV file using T-SQL.
And I'm very confused about there are many ways can do it, I don't know to choose which one, please help me to confirm the bollowing:
As I know there are about 3 methods, and I want you help me to confirm:
Using Microsoft.Jet.OLEDB.4.0, like this:
INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Text;Database=C:\Temp\;HDR=Yes;',
'SELECT * FROM test.csv')
(object_id, name)
SELECT object_id, name
FROM sys.tables;
but this need the csv file is there, and with header
using SQLCMD
command line.
using BCP
Use union, get data and it's column header.
This is all my understanding about T-SQL export to CSV, please help me to confirm.
Is there other way to export to CSV?
Thanks!
Execute the below command in SQL Server:
EXEC xp_cmdshell 'SQLCMD -S . -d MsVehicleReg2 -Q "SELECT * FROM tempViolationInfo" -s "," -o "O:\result.csv"';
You could use a UNION to create a header row, like this:
SELECT 'object_id', 'name'
UNION ALL
SELECT object_id, name
FROM sys.tables
Here is the T-SQL way:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\;HDR=YES;FMT=Delimited','SELECT * FROM [FileName.csv]')
SELECT Field1, Field2, Field3 FROM DatabaseName
But, there's a couple of caveats:
You need to have the Microsoft.ACE.OLEDB.12.0 provider available. The Jet 4.0 provider will work, too, but it's ancient, so I used this one instead.
The .CSV file will have to exist already. If you're using headers (HDR=YES), make sure the first line of the .CSV file is a delimited list of all the fields.
For Ace.OLEDB.12.0 (the new Jet redistributable engine), you can install the 32-bit or 64-bit stand-alone engine, even if you have the "other flavor" already installed, whether on its own, from Access, etc:
use the /passive command-line option:
(32-bit): AccessDatabaseEngine.exe /passive
(64-bit): AccessDatabaseEngine_64.exe /passive
In my case, I have 64-bit SQL Express 2008 R2, and had 32-bit Office 12 apps installed (thus, the 32-bit ACE drivers were installed). I installed the 64-bit AccessDatabaseEngine_64.exe, and it's sort of working for me now...
Also, this is assuming you've done the other configuration work:
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0',
N'AllowInProcess', 1
GO
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0',
N'DynamicParameters', 1
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
regarding "best practice", there is no best practice.
there are several options available, not limited to:
in T-SQL with INSERT INTO OPENROWSET(...) SELECT * from [MyTable]...
executing BCP, whether from a job step or in T-SQL with xp_cmdshell
SSIS packages
PowerShell (from a job step, in SQL Server 2008+) or other external script/executable

Resolve hostnames with t-sql

How can i resolve a hostname in t-sql? a 2000 compatible method is preferred. Although something that works on 2005/2008 would also be helpful.
eg. If i have the hostname stackoverflow.com i want to return 69.59.196.211
Well, I suppose you could use xp_cmdshell to execute nslookup and parse the results. Seems like a really awkward thing for SQL Server to be doing, though.
exec master..xp_cmdshell 'nslookup intel.com'
.. then you'll probably want to stuff that in a temp table and walk through the results.
You could also, if you can get access to SQL Server 2005 or 2008, build a stored procedure or function in .NET and do a simple call to Dns.GetHostAddresses().
SQL Server may block access to procedure 'sys.xp_cmdshell' as part of the security configuration. As a system administrator you can enable the use of 'xp_cmdshell' as follows:
-- Allow advanced options.
EXEC sp_configure 'show advanced options', 1;
GO
-- Update the currently configured value for advanced options.
RECONFIGURE;
GO
-- Then, enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- Update the currently configured value for this feature.
RECONFIGURE;
GO
-- Then you can go ahead and run ...
exec master..xp_cmdshell 'nslookup microsoft.com'
GO