Solved

SQL 2005 XML

Posted on 2014-03-14
11
699 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

762 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

20 Experts available now in Live!

Get 1:1 Help Now