TraciShultz
asked on
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_CreateTempVa lidateRemo valTubeLoc ations]
-- Add the parameters for the stored procedure here
@Data XML
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- Using the value() method
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempVal idateRemov alTubeData ]') AND type in (N'U'))
DROP TABLE [dbo].[TempValidateRemoval TubeData]
IF LEN(@Data) > 0
BEGIN
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 TempValidateRemovalTubeDat a
FROM @Data.nodes('Tubes/Tube') AS T(Tube);
END
ELSE
BEGIN
CREATE TABLE [dbo].[TempValidateRemoval TubeData](
[bc] [varchar](50) NULL,
[RackBc] [varchar](50) NULL,
[Row] [int] NULL,
[Col] [int] NULL
) ON [PRIMARY]
END
SELECT * FROM TempValidateRemovalTubeDat a
END
Argument data type xml is invalid for argument 1 of len function.
Any help is greatly appreciated.
CREATE PROCEDURE [dbo].[RunXML_CreateTempVa
-- Add the parameters for the stored procedure here
@Data XML
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- Using the value() method
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TempVal
DROP TABLE [dbo].[TempValidateRemoval
IF LEN(@Data) > 0
BEGIN
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 TempValidateRemovalTubeDat
FROM @Data.nodes('Tubes/Tube') AS T(Tube);
END
ELSE
BEGIN
CREATE TABLE [dbo].[TempValidateRemoval
[bc] [varchar](50) NULL,
[RackBc] [varchar](50) NULL,
[Row] [int] NULL,
[Col] [int] NULL
) ON [PRIMARY]
END
SELECT * FROM TempValidateRemovalTubeDat
END
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.
DATALENGTH() function
Unfortunately, it will say nothing about XML data validity because you may store almost any text into XML columns.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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('*')
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></he llo>'
SELECT @data1.exist('/hello'), @data1.exist('/hello/and') , @data2.exist('/hello/and')
DECLARE @data1 XML, @data2 XML, @data3 XML, @data4 XML
SET @data1 = '<hello>there</hello>'
SET @data2 = '<hello>there<and>anywhere
SELECT @data1.exist('/hello'), @data1.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.
Thanks to Vacheron.
ASKER
Perfect just what I was looking for. Thanks for all the comments...
IF LEN(CAST(@Data AS NVARCHAR(MAX))) > 0