FOR XML could not serialize the data because it contains character (0x0000)
The line:
...
SELECT
[CodFuncionario],
STUFF
(
(
SELECT
' / ' +
CAST
(
[DescFuncao] + '-' +
[DescTempoExperiencia]
AS VARCHAR(MAX)
)...
Should be:
...
SELECT
[CodFuncionario],
STUFF
(
(
SELECT
' / ' +
CAST
(
replace -- *** NEW! ***
(
[DescFuncao] + '-' +
[DescTempoExperiencia],
char(0),
''
)
AS VARCHAR(MAX)
)...
I get the same error when I do this:
DECLARE @foo VARCHAR(32) = CHAR(0); -- 0x0000
SELECT @foo FOR XML PATH, TYPE;
So, find all instances of DescFuncao
or DescTempoExperiencia
(sorry, you don't use table aliasing, so it's impossible to tell which table they come from) where the contents contain CHAR(0)
, and fix them. For example:
UPDATE dbo.whatever
SET DescFuncao = REPLACE(DescFuncao, CHAR(0), '')
WHERE DescFuncao LIKE '%' + CHAR(0) + '%';
It's not enough to filter those rows out in your query, because you don't know at what point the XML methods will go to work, but you may also try:
STUFF(( SELECT ' / ' + CAST(REPLACE([DescFuncao] + '-'
+ [DescTempoExperiencia], CHAR(0), '') AS VARCHAR(MAX))
Of course, fixing the source data once will be much more efficient than running these replace routines every time.
Note, this might not be the only specific character that causes this problem. 0x0001
-> 0x0008
will also generate the same error. So if you have those characters in there too, you should investigate where they are coming from, and fix the source.
I tested this with ASCII characters 0-255 and found out that you get this error for characters: 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x000B, 0x000C, 0x000E, 0x000F, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F
.
One workaround is to remove , TYPE
from your XML statement.
Another way is to remove those characters in the select statement:
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(
REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(
REPLACE( REPLACE( REPLACE( REPLACE(
< YOUR EXPRESSION TO BE CLEANED >
,char(0x0000),'') ,char(0x0001),'') ,char(0x0002),'') ,char(0x0003),'') ,char(0x0004),'')
,char(0x0005),'') ,char(0x0006),'') ,char(0x0007),'') ,char(0x0008),'') ,char(0x000B),'')
,char(0x000C),'') ,char(0x000E),'') ,char(0x000F),'') ,char(0x0010),'') ,char(0x0011),'')
,char(0x0012),'') ,char(0x0013),'') ,char(0x0014),'') ,char(0x0015),'') ,char(0x0016),'')
,char(0x0017),'') ,char(0x0018),'') ,char(0x0019),'') ,char(0x001A),'') ,char(0x001B),'')
,char(0x001C),'') ,char(0x001D),'') ,char(0x001E),'') ,char(0x001F),'')
You could also create a function with these replace statements.