Import XML file into SQL Server without BULK - sql

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

Related

Error while executing xp_readerrorlog using OPENROWSET in SQL Server - querying remote data sources

Can you please help me to fix this query?
SELECT
FROM OPENROWSET('SQLNCLI',
'Server=.;Trusted_Connection=Yes;',
'SET FMTONLY OFF;EXEC master.sys.xp_readerrorlog')
I get this error:
Msg 11519, Level 16, State 1, Procedure sys.sp_describe_first_result_set, Line 1 [Batch Start Line 12]
The metadata could not be determined because statement 'EXEC master.sys.xp_readerrorlog' invokes an extended stored procedure.
I have enabled Ad-Hoc Distributed Queries as well by using below query but getting same error.
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
You cannot use OPENROWSET for an extended stored procedure. You could create a linked server
EXEC {Your Linked Server Name}.master.sys.xp_readerrorlog;
But you say you have 120 instances. (Quite why you don't have management software for so many, is another question...) So you are better off using Powershell to do this. For example
Get-SqlErrorLog
-Since LastMonth
-ServerInstance "your","Server","Instances","Here"
You would probably want other code to filter and group the results, but you haven't shown what you want.
For example you could do something like this
Get-Content "ServerInstances.txt"
| Get-SqlErrorLog -After "2022-09-16 10:00:00"
| Where Source -eq "Backup"

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.

Export to Excel from stored procedure - SQL blocked

I have a stored procedure that I'm trying to export to excel.
insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\sales.xls;;HDR=YES', 'SELECT * FROM [Sheet1$]')
exec des_z_test '19FDA2C7-E494-E411-80D2-0050568C3F74', 'E1AB4FD4-4C3B-E411-80D2-0050568C3F74'
However, I'm getting this error. The stored procedure runs fine on it's own. I suspect the issue is from the insert statement
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', search for 'Ad Hoc Distributed Queries' in SQL Server Books Online.
Any ideas?
Zane answered below, "Sounds like a job for Integration Services!"

How to copy table from Production to Development?

I am working on Development Server Named as SQLDEV01 and the db name is University and the table name is cse.students. During my work on table cse.students I lost some rows so I need to get the all the exact data from Production server.production Server Name is SQLPROD01.
How can I query to get the production data without using SSIS?
Setup a linked server from the development to production servers before doing the following from SSMS. All code should be executed on the development server.
http://technet.microsoft.com/en-us/library/ms188279.aspx
-- On Development server [SQLDEV01]
TRUNCATE TABLE [University].[cse].[students];
GO
-- Use link server to move data
INSERT INTO
[University].[cse].[students]
SELECT
*
FROM
[SQLPROD01].[University].[cse].[students]
GO
This assumes there are no identity columns on the target.
If you do have identity columns, turn on/off allow inserts before/after executing the above insert.
-- Before Insert, execute this statement
SET IDENTITY_INSERT [University].[cse].[students] ON
GO
-- After Insert, execute this statement
SET IDENTITY_INSERT [University].[cse].[students] OFF
GO
You can also create an ad-hoc connection using the OPENROWSET command.
http://technet.microsoft.com/en-us/library/ms190312.aspx
INSERT INTO
[University].[cse].[students]
SELECT
PRD.*
FROM
OPENROWSET('SQLNCLI', 'Server=SQLPROD01;Trusted_Connection=yes;',
'SELECT * FROM [University].[cse].[students]') AS PRD;
I did not check the syntax for your environment, please check.
If add hoc queries are set off (0), have the DBA turn them on (1) temporarily. Execute the following on the production server.
http://msdn.microsoft.com/en-us/library/ms187569.aspx
-- Show all settings
sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
-- What is the current setting?
sp_configure
GO
-- Allow add hoc queries
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
GO
If (s)he does not allow that, you are stuck with a physical linked server.
If you are using delegation - choice #3, you have to watch out for the double hop issue.
http://blogs.msdn.com/b/sql_protocols/archive/2006/08/10/694657.aspx
I would configure the linked server with a specific account on production that has rights to select the data. See choice #4.
There are a few ways to do this. One that is pretty much failsafe is the following:
In Microsoft SQL Management Studio expand the server node for the source server - Then expand the Databases node. Right click on the source database and select Tasks -> Generate Scripts. When the dialog pops up click next. Select the "Select specific database objects" radio button. Expand the Tables node as check the table you want to copy. Click the next button at the bottom. Click the advanced button. In the options that pop up for Script DROP and CREATE select Script DROP and CREATE on the right. For the option Type of data to script select schema and data on the right. Click OK. Now back on the main dialog you need to select "Save to Clipboard" or "Save to new query window". I usually select clipboard because I am usually going to a different server but select what works best for you. Click next. Click next again and the script will generate according to your selections. Now just run that script on the destination database.
please use this query generalized query by replacing your entries
select * into targetTable from [sourceserver].[sourcedatabase].[dbo].[sourceTable]

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

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.