How to check if an XML type field is null or empty in SQL SERVER

Asked

Viewed 265 times

0

I need to check the amount of 0, NULL or empty in fields of different types, My difficulty is when it comes to fields of type XML. I tried to include in my logic the function exist(), but I do not know if it is the best way to do this check, using the code I have done before.

/*vérifie si la colonne numérique a des zéros et si la colonne avec des textes est vide (ou ne contient que espaces)*/

if @dataType IN ('int', 'bit', 'smallint', 'numeric', 'float', 'bigint', 'varbinary')
    set @checks = '[' +@colName +']' + '=0'

else if @dataType IN ('varchar', 'char', 'nvarchar', 'date', 'datetime')
    set @checks = ' ltrim(rtrim(['+ @colName + '])) ' + 'like '''''

else if @dataType IN ('xml')




/*vérifie si est nullable et si la colonne est nule*/
if @isNullable = 1
if LEN(@checks)> 0
    set @checks = @checks + ' OR [' + @colName + '] IS NULL '
else
    set @checks = '[' +@colName + '] IS NULL '


set @queryStat = @queryStat + @checks

EXEC sp_executesql @queryStat, @paramDefinition , @cnt = @vides output

1 answer

1

You don’t have to make a if for the xml type; just check if the field is null. For this you must have @isNullable = 1 and will enter this section that makes the check:

/*vérifie si est nullable et si la colonne est nule*/
if @isNullable = 1
if LEN(@checks)> 0
    set @checks = @checks + ' OR [' + @colName + '] IS NULL '
else
    set @checks = '[' +@colName + '] IS NULL '
  • I tried to check only if it was null, but I got the error below, as if I hadn’t checked for type xml Xml data type is not supported in Distributed queries. Remote Object 'CTCL-dbpdata24 sql2016.blatuio269_Prd.dbo.tabStagingMessages' has xml column(s)

  • 1

    Oi Raquel. The error is not pq the xml is null, but rather that the xml type is not supported in distributed queries. I imagine inside sp_executesql have something like SELECT XXX FROM LINK.MinhaBAse.dbo.TabelaX;. In this case, you would have to cast the xml for another type, but then you would have to see the stored.

  • That’s right. I’ll have to do it another way. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.