Check for XML data type variable

I have an sp that has a variable of type XML I want to check to make sure it was passed in correctly? the IF LEN(@Data) > 0 proc=duces the following Error
Argument data type xml is invalid for argument 1 of len function.

Any help is greatly appreciated.

CREATE PROCEDURE [dbo].[RunXML_CreateTempValidateRemovalTubeLocations]
      -- Add the parameters for the stored procedure here
      @Data XML
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.

    -- Insert statements for procedure here

-- Using the value() method

      IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempValidateRemovalTubeData]') AND type in (N'U'))
      DROP TABLE [dbo].[TempValidateRemovalTubeData]

      IF LEN(@Data) > 0
            SELECT T.Tube.value('@bc', 'VARCHAR(50)') AS bc,
                     T.Tube.value('@RackBc', 'VARCHAR(50)') AS RackBc,
                     T.Tube.value('@Row', 'Int') AS Row,
                     T.Tube.value('@Col', 'Int') AS Col
            INTO TempValidateRemovalTubeData
            FROM @Data.nodes('Tubes/Tube') AS T(Tube);

      CREATE TABLE [dbo].[TempValidateRemovalTubeData](
            [bc] [varchar](50) NULL,
            [RackBc] [varchar](50) NULL,
            [Row] [int] NULL,
            [Col] [int] NULL
      ) ON [PRIMARY]


      SELECT * FROM TempValidateRemovalTubeData
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you cast it to NVARCHAR(MAX) is should be fine (or if you know it is a certain length less than 4000 use that)

If you need to know length of XML data then you have to use

DATALENGTH() function

Unfortunately, it will say nothing about XML data validity because you may store almost any text into XML columns.
Please try this condition:

IF @Data.exist('*') = 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

OK, so I am fairly new to XML stuff in SQL too.
OK, barely starting but had seen that method for looking up specific elements but had not thought of this way yet.
Just to see how it worked I threw together the following and...
Very handy indeed.
Cheers Vacheron.
DECLARE @data1 XML, @data2 XML, @data3 XML, @data4 XML
SET @data1 = '<hello>there</hello>'
SET @data2 = 'hello there'
SET @data3 = ''
SELECT @data1.exist('*'), @data2.exist('*'), @data3.exist('*'), @data4.exist('*')

Open in new window

Yes, it should work :-)
You could even test

DECLARE @data1 XML, @data2 XML, @data3 XML, @data4 XML
SET @data1 = '<hello>there</hello>'
SET @data2 = '<hello>there<and>anywhere</and></hello>'
SELECT @data1.exist('/hello'), @data1.exist('/hello/and'), @data2.exist('/hello/and')
Aye. I was just testing results of * with valid data, plain text, zero text and null
I am learning also.

Thanks to Vacheron.
TraciShultzAuthor Commented:
Perfect just what I was looking for. Thanks for all the comments...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.