Solved

SQL 2005 XML

Posted on 2014-03-14
11
701 Views
Last Modified: 2014-03-17
The below T-SQL works, but when I try to change file XML file path and name to a variable, it does not work.  It gives the error of: Must declare the scalar variable "@x".

DECLARE @x xml
DECLARE @FileName varchar(100)

SELECT @x = SQLs FROM OPENROWSET (BULK 'C:\Temp\ForTables.xml', SINGLE_BLOB) AS Solution(SQLs)

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

----------------------------------------------------------------------------------------------------

DECLARE @x xml
DECLARE @FileName varchar(100)

SET @FileName = 'C:\Temp\ForTables.xml'

DECLARE @CMD varchar(max)

SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
EXEC (@cmd)

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
0
Comment
Question by:dastaub
  • 6
  • 5
11 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39930689
but when I try to change file XML file path and name to a variable, it does not work.
To quote from SQL Server's BOL:
OPENROWSET does not accept variables for its arguments.
.  So you will have to build the entire string and then execute it as dynamic SQL
0
 

Author Comment

by:dastaub
ID: 39930813
the below builds the entire string and executes it as a dynamic SQL, but does not work and gives the above described error message

DECLARE @CMD varchar(max)

SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
EXEC (@cmd)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39931756
Please post the value for @cmd.
0
 

Author Comment

by:dastaub
ID: 39932129
the value of @cmd is after the equals sign.

SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39932730
Can you post the result please, as in:
SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
Print @cmd
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:dastaub
ID: 39932997
Print @cmd  would result in:

SELECT @x = SQLs FROM OPENROWSET (BULK 'C:\Temp\ForTables.xml', SINGLE_BLOB) AS Solution(SQLs)
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39933070
Now I see your point.  I am sorry I was so dense.

You need to use sp_executesql as in:
Declare @FileName nvarchar(1000) = 'C:\Temp\ForTables.xml'


DECLARE @CMD nvarchar(max),
		@x xml
DECLARE @Parms nvarchar(100) = N'@FileName nvarchar(1000), @X xml OUTPUT'

SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
EXECUTE sp_executesql @CMD, @Parms, @FileName = @FileName, @x = @x OUTPUT

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

-- Do your stuff

EXEC sp_xml_removedocument  @hdoc 

Open in new window

0
 

Author Comment

by:dastaub
ID: 39933471
I'm glad you hung in there with me.
0
 

Author Comment

by:dastaub
ID: 39934690
A minor change had to occur.  the DECLARE and the SET had to be in two lines.
EXAMPLE:
DECLARE @FileName nvarchar(1000) = 'C:\Temp\ForTables.xml'
had to be changed to:
DECLARE @FileName nvarchar(1000)
SET @FileName = 'C:\Temp\ForTables.xml'
The below T-SQL works.  
------------------------------------------------------------------------------------------------------

Declare @FileName nvarchar(1000)

SET @FileName = 'C:\Temp\ForTables.xml'

DECLARE @CMD nvarchar(max),
            @x xml,
        @Parms nvarchar(100)

SET @Parms = '@FileName nvarchar(1000), @X xml OUTPUT'

SET @cmd = 'SELECT @x = SQLs FROM OPENROWSET (BULK ''' + @FileName + ''', SINGLE_BLOB) AS Solution(SQLs)'
EXECUTE sp_executesql @CMD, @Parms, @FileName = @FileName, @x = @x OUTPUT

DECLARE @hdoc int

EXEC sp_xml_preparedocument @hdoc OUTPUT, @x

SELECT * FROM OPENXML (@hdoc, '/CMPDocument/CMPData/EventLog/SystemLog/SYSs', 2)
WITH (
      [InstanceID] varchar(500)
      ,ErrorTime varchar(500)
      ,ErrorSource varchar(500)
      ,ErrorMessage varchar(max)
      )

SELECT * FROM OPENXML (@hdoc, '/CMPDocument/CMPData/EventLog/ApplicationLog/LOGs', 2)
WITH (
      [InstanceID] varchar(500)
      ,ErrorTime varchar(500)
      ,ErrorSource varchar(500)
      ,ErrorMessage varchar(max)
      )

EXEC sp_xml_removedocument  @hdoc
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39935733
A minor change had to occur.  the DECLARE and the SET had to be in two lines.
You are right.  My code is not supported in SQL Server 2005.
0
 

Author Comment

by:dastaub
ID: 39935888
Yes, I assumed it was an SQL version issue.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now