Retrieving Automatic XML Data via Stored Procedure in Java (MS SQL)

I am using the following query to return through a stored procedure,

SELECT * FROM test FOR XML AUTO

      

This inturn gives me the following output when I run the stored procedure,

OUTPUT on the server:

<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
.........

      

When I tried to execute the procedure using preparecall in java and I tried to return the result set, this should return one xml line, but I get this splitting as shown below,

Record1:
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data
Record 2: 1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test column1="data1" column2="data2" />
<test colu
.............. and so on.

      

Below is a snippet of my code,

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = getConnection();
CallableStatement statement = connection.prepareCall("{call test_proc()}");
ResultSet rs = statement.getResultSet();

while(rs.next()){
        c++;
        System.out.println("Record "+c+":"+rs.getString(1));
}
System.out.println("Count : "+c);

      

This inturn returns count 3 instead of 1, but it works fine in sql server when I run this as an anonymous procedure.

I also tried the method getSQLXML(int column)

but didn't use it.

Can anyone help you with this?

Adding one more point: I tried a simple query execution (via java code) - SELECT * FROM test FOR XML AUTO

and tried to get the result set, but still it gives the same result. :(

+3


source to share


1 answer


Add ELEMENTS as well as a column in one table.



SELECT * FROM test FOR XML AUTO ,ELEMENTS

      

0


source







All Articles