Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 28th, 2003, 07:49 AM
Registered User
 
Join Date: Sep 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML in stored procedure

I need to execute a SQL statement that returns XML (for XML AUTO, ELEMENTS) then I need to INSERT the XML into a field in a Different Database an Table from within the SAME stored proc.

The first database is an online transactional database.

The second database is used by a proprietary system to manage file transfers from our National Headquarters to our 4000 offices.

The XML generated will be stored on location as a file. Information is the file is used to drive Desktop Software behavior.

So how do I do this

MyStoredProc
(
)
As
INSERT INTO SQLDB2 (myDestinationField) FROM (SELECT * FROM JOINS in DB1 FOR XML AUTO, ELEMENTS..)

Since I am new to SQL2000 a good code sample would be appreciated.


 
Old October 4th, 2003, 06:29 PM
Authorized User
 
Join Date: Jun 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First off your destination db must exist on the same server as the source db or you must use linked server(this is a pain) and the user that executes the stored procedure must have permissions on both dbs.

second, when I try to run this code it fails
    INSERT INTO NorthWind.dbo.Categories (CategoryName, Description)
    SELECT Title, Body
    FROM Article
    FOR XML AUTO
with the following error: The FOR XML clause is not allowed in a INSERT statement.

this will work (but its not what you want.)
    INSERT INTO NorthWind.dbo.Categories (CategoryName, Description)
    SELECT Title, Body
    FROM Article


I would recommend using a webservice or exe to handle the get of xml from the first db then insert the result to the second db as a text column?

most of my experience with FOR XML is for generating xml for xslt transformation to html, not for updates but I do recall reading about it some time ago Wrox does have a good book on MSSQL2000 XML http://www.amazon.com/exec/obidos/tg...glance&s=books

You could - if your xml is always going to be less than 8000 characters build an xml string manually and then insert this varchar.

    Declare @Title varchar(150)
    Declare @Author varchar(150)
    Declare @xml varchar(8000) -- max varchar

    -- get data from source db
    SELECT @Title = ISNULL(Title, 'no data'),
        @Author = ISNULL(Author, 'no data')
    FROM Article
    WHERE ArticleID = 3

    -- build xml
    SET @xml = '<Root>' +
        '<Title>' + @Title + '</Title>' +
        '<Author>' + @Author + '</Author>' +
        '</Root>'

    -- insert to destination db
    INSERT INTO Northwind.dbo.Categories(CategoryName, Description)
    VALUES('Test', @xml)

this has serious limitations and I would not recommend it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Pass XML parameter to stored procedure BCullenward Classic ASP Databases 3 September 10th, 2008 02:07 PM
XML Output from stored procedure NigelBronson ASP.NET 2.0 Basics 0 October 15th, 2006 06:24 PM
Stored Procedure Help BukovanJ SQL Language 2 October 10th, 2006 08:02 AM
stored procedure keyvanjan Classic ASP Basics 2 May 26th, 2006 01:16 AM
Stored Procedure to.... flyin ADO.NET 3 March 3rd, 2004 02:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.