1

I am facing similar problem (see error msg below). I am searching stored procedures for specific word in a SQL Server database. Pulling result sets are objectName, ObjectType and ObjectText (in which search word exist). I am trying to bring ObjectText as XML form so can be clickable to open directly from result sets. It is working fine with triggers but not with stored procedures as seems facing illegal characters while converting into XML form.

I have tried couple of suggestions provided on net such as cast/convert/replace unified characters, XML, ascii etc and replace illegal characters like &#xD, &#xA, &#x9 but no success.

SELECT 
    o.name as 'ObjectName', 
    CASE o.xtype 
       WHEN 'C' THEN 'CHECK constraint ' 
       WHEN 'D' THEN 'Default or DEFAULT constraint'
       WHEN 'F' THEN 'FOREIGN KEY constraint'
       WHEN 'FN' THEN 'Scalar function'
       WHEN 'IF' THEN 'In-lined table-function'
       WHEN 'K' THEN 'PRIMARY KEY or UNIQUE constraint'
       WHEN 'L' THEN 'Log'
       WHEN 'P' THEN 'Stored procedure'
       WHEN 'R' THEN 'Rule'
       WHEN 'RF' THEN 'Replication filter stored procedure' 
       WHEN 'S' THEN 'System table'  
       WHEN 'TF' THEN 'Table function' 
       WHEN 'TR' THEN 'Trigger'  
       WHEN 'U' THEN 'User table' 
       WHEN 'V' THEN 'View' 
       WHEN 'X' THEN 'Extended stored procedure' 
       ELSE o.xtype 
    END as 'ObjectType',
    CAST(c.text as XML) as 'SearchObject'
FROM 
    syscomments c
INNER JOIN 
    sysobjects o ON c.id = o.id
LEFT JOIN 
    sysobjects p ON o.Parent_obj = p.id
WHERE 
    o.xtype = 'P'
    AND (c.text LIKE '%loan_timestamp%' OR 
         c.text LIKE '%aclk_timestamp%' OR
         c.text LIKE '%addr_timestamp%')

Error:

Msg 9455, Level 16, State 1, Line 121
XML parsing: line 44, character 28, illegal qualified name character

CAST(c.text as XML) AS 'SearchObject' 

This column should result be in clickable XML so stored procedures / triggers can be opened directly from result sets.

1 Answer 1

2

If I get this correctly, you do not show us, what your column c.text might contain, but you show us a rather big query, which has nothing to do with your issue...

If I get this correctly, you want to include c.text into your resultset as clickable XML. although it is not well formatted, valid XML (or at least not in all cases).

Two approaches:

  • Try TRY_CAST instead of CAST to return a NULL if the cast goes wrong
  • Use a subquery like
    (SELECT c.text AS [*] FOR XML PATH(''),TYPE) AS SearchObject

The first will not throw an error, but won't make you happy. Any occurance of &, < or > (and many other forbidden characters) will force this to show up with NULL...

The second will take the columns content and will implicitly do all the escaping for your... Be aware, that within the XML viewer you will see escaped sequences instead of forbidden characters

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.