hello i am using the following stored procedure to insert the xml data in a table.
PROCEDURE [dbo].[ExpertSystem_SET_QuestionData]
#XmlQue NVARCHAR(MAX)
AS
set nocount on
DECLARE #XmlHdl INT
BEGIN
SET #XmlQue=N''+#XmlQue
EXEC sp_xml_preparedocument #XmlHdl OUTPUT,#XmlQue
INSERT INTO ExpertSystem_Master
SELECT QIdx,#TemplateId,QDetailsx,IsYesx,IsNox,Eligibleyesx,Eligiblenox,0,0
FROM
OPENXML(#XmlHdl,'/Template/QueInfo',1)
WITH
( QIdx INT '#QId',
QDetailsx Ntext '#QDetails',
IsYesx Ntext '#IsYes',
IsNox Ntext '#IsNo',
Eligibleyesx CHAR(1) '#Eligibleyes',
Eligiblenox CHAR(1) '#Eligibleno'
)
END
In this case i am passing the values to parameter as
ExpertSystem_SET_QuestionData '<?xml version="1.0" encoding="UTF-16"?><Template TId="1"><QueInfo QId="1" QDetails="Are you " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueInfo QId="2" QDetails=" राज्य " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueAns QId="1" isMsgFlag="N" Msg="you are not Eligible"/><QueAns QId="2" isMsgFlag="N" Msg="you are not eligible"/></Template>'
The data is inserted , but it is inserted as ????? instead of ** "राज्य "**.
When i am calling the procedure as
ExpertSystem_SET_QuestionData 'N<?xml version="1.0" encoding="UTF-16"?><Template TId="1"><QueInfo QId="1" QDetails="Are you Domicile of maharashtra" IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueInfo QId="2" QDetails="Have you passed Secondary School 10th Examination?महाराष्ट्र राज्य मार्ग परिवहन महामंडळ " IsYes="Yes" IsNo="No" Eligibleyes="Y" Eligibleno="N"/><QueAns QId="1" isMsgFlag="N" Msg="you are not Eligible"/><QueAns QId="2" isMsgFlag="N" Msg="you are not eligible"/></Template>'
i get the error as "The XML parse error 0xc00ce556 occurred on line number 1, near the XML text "N "
can you please help me out with the issue,as the procedure is called at run time so any data can be passed to the #XmlQue variable.
so how am i supposed to append "N" with the #XmlQue variable ???
Thanks in advance.
The N should come before the xml value.
ExpertSystem_SET_QuestionData N'<?xml version="1.0" ...
Related
I'm trying to get all this XML result in one line instead of 3 for each column
DECLARE #ii INT = 10;
DECLARE #String1 NVARCHAR(4000);
SET #String1 = '';
WHILE(#ii <= 18)
BEGIN
SET #String1 = (#String1 + 'SELECT LoanNumber = ''Complaint'+CONVERT(VARCHAR(2),#ii)+'-Call1'' , LoanStatus=''Compliants'' , LoanStatusDate = CAST(GETDATE() AS DATE)
UNION
SELECT LoanNumber = ''Complaint'+CONVERT(VARCHAR(2),#ii)+'-Call2'', LoanStatus=''Compliants'' , LoanStatusDate = CAST(GETDATE() AS DATE)
UNION
SELECT LoanNumber = ''Complaint'+CONVERT(VARCHAR(2),#ii)+'-Call3'', LoanStatus=''Compliants'' , LoanStatusDate = CAST(GETDATE() AS DATE)')
IF #ii != 18
SET #string1 = #string1 + ' UNION '
ELSE
SET #string1 = #string1 + 'FOR XML PATH (''Loan''),ROOT(''Loans'') '
SET #ii = #ii+1
END
EXEC sp_executesql #String1
I want something like this:
<Loans>
<LoanNumber>Complaint10-Call1<LoanStatus>Compliants<LoanStatusDate>2019-01-18
</Loan>
<Loan>
<LoanNumber>Complaint10-Call2 <LoanStatus>Compliants<LoanStatusDate>2019-01-18
</Loan>
<Loan>
<LoanNumber>Complaint10-Call3<LoanStatus>Compliants<LoanStatusDate>2019-01-18
</Loan>
Instead of the result that you get when you execute the code I provided. I appreciate your help.
This might be wild guessing, but I've got the feeling, that I understand, what this is about:
if you run the code you will see the result. no input data is needed .
I just want the structure of the xml outcome to all be on one line for
one set of each loop
Your provided code leads to this:
<Loans>
<Loan>
<LoanNumber>Complaint10-Call1</LoanNumber>
<LoanStatus>Compliants</LoanStatus>
<LoanStatusDate>2019-01-22</LoanStatusDate>
</Loan>
<Loan>
<LoanNumber>Complaint10-Call2</LoanNumber>
<LoanStatus>Compliants</LoanStatus>
<LoanStatusDate>2019-01-22</LoanStatusDate>
</Loan>
<!-- more of them-->
</Loans>
This is perfectly okay, valid XML.
But you want the result
outcome to all be on one line for one set of each loop
Something like this?
<Loans>
<Loan>
<LoanNumber>Complaint10-Call1</LoanNumber><LoanStatus>Compliants</LoanStatus><LoanStatusDate>2019-01-22</LoanStatusDate>
</Loan>
<!-- more of them-->
</Loans>
There is a big misconception I think... XML is not the thing you see. The same XML can look quite differently, without any semantic difference:
Check this out:
DECLARE #xmltable table(SomeXml XML)
INSERT INTO #xmltable VALUES
--the whole in one line
('<root><a>test</a><a>test2</a></root>')
--all <a>s in one line
,('<root>
<a>test</a><a>test2</a>
</root>')
--each element in one line
,('<root>
<a>test</a>
<a>test2</a>
</root>')
--white space going wild...
,('<root>
<a>test</a>
<a>test2</a>
</root>');
--now check the results
SELECT * FROM #xmltable;
This means: How the XML appears is a matter of the interpreter. The same XML opened with another tool might appear differently. Dealing with XML means dealing with data but not with format... The actual format has no meaning and should not matter at all...
Starting with SQL-Server 2016 you might have a look at JSON, if you need a tiny format:
DECLARE #somedata table(SomeValue VARCHAR(100),SomeStatus VARCHAR(100),SomeDate DATE);
INSERT INTO #somedata VALUES
('Complaint10-Call1','Complaints','2019-01-22')
,('Complaint10-Call2','Complaints','2019-01-22')
,('Complaint10-Call3','Complaints','2019-01-22');
SELECT * FROM #somedata FOR JSON PATH;
The result comes in one line:
[{"SomeValue":"Complaint10-Call1","SomeStatus":"Complaints","SomeDate":"2019-01-22"},{"SomeValue":"Complaint10-Call2","SomeStatus":"Complaints","SomeDate":"2019-01-22"},{"SomeValue":"Complaint10-Call3","SomeStatus":"Complaints","SomeDate":"2019-01-22"}]
I have an XML column in my Table and i wanted to replace particular text wherever it appear in that column with a new text. Here is the xml structure,
<Story>
<StoryNonText>
<NonText>
<ImageID>1</ImageID>
<Src>http://staging.xyz.com/FolderName/1.png</Src>
</NonText>
<NonText>
<ImageID>2</ImageID>
<Src>http://staging.xyz.com/FolderName/2.png</Src>
</NonText>
</StoryNonText>
</Story>
In the above XML I wanted to replace all the <Src> values having http://staging.xyz.com/ to http://production.xyz.com/. Please guide me how i can do this!
You can use Replace() function as below:
Update TableName
SET
ColumnName=replace(CAST(ColumnName AS VARCHAR(8000)),'<Src>http://staging.xyz.com/','<Src>http://production.xyz.com/')
With a little help from a couple of XML functions you can do this in a loop.
The loop is necessary since replace value of can only replace one value at a time. This code assumes the URL is located first in the node and not embedded in text anywhere.
declare #T table(X xml);
insert into #T(X) values('<Story>
<StoryNonText>
<NonText>
<ImageID>1</ImageID>
<Src>http://staging.xyz.com/FolderName/1.png</Src>
</NonText>
<NonText>
<ImageID>2</ImageID>
<Src>http://staging.xyz.com/FolderName/2.png</Src>
</NonText>
</StoryNonText>
</Story> ');
declare #FromURL nvarchar(100);
declare #ToURL nvarchar(100);
set #FromURL = 'http://staging.xyz.com/';
set #ToURL = 'http://production.xyz.com/';
while 1 = 1
begin
update #T
set X.modify('replace value of (//*/text()[contains(., sql:variable("#FromURL"))])[1]
with concat(sql:variable("#ToURL"), substring((//*/text()[contains(., sql:variable("#FromURL"))])[1], string-length(sql:variable("#FromURL"))+1))')
where X.exist('//*/text()[contains(., sql:variable("#FromURL"))]') = 1;
if ##rowcount = 0
break;
end;
select *
from #T
replace value of (XML DML)
concat Function (XQuery)
contains Function (XQuery)
string-length Function (XQuery)
sql:variable() Function (XQuery)
There are many ways to do that.
The first way is to add a WHILE loop. Inside a loop, you search (CHARINDEX) for a position of first tag and first tag. Then, knowing the start and end positions, replace the value. Then on the next iteration you search again, but change starting position in CHARINDEX() function
The second way is to use SELECT ... FROM OPENXML + EXEC sp_xml_preparedocument
I am following XML based transport of data using FOR XML AUTO. When a column containing control character was encountered and a XML transform as below was attempted, it resulted in an exception:
DECLARE #TrialData NVARCHAR(200) = ''; --Not empty string..contains a control character '<-'
DECLARE #TrialDataInHex VARBINARY(100)
SET #TrialDataInHex = CONVERT(VARBINARY(100),#TrialData)
SELECT #TrialDataInHex; -- returns 0x1B00 looks like this '<-'
Now, when I try to insert the same into XML variable like below
DECLARE #a_XML XML;
SET #a_XML = #TrialData;
This resulted in
XML parsing: line 0, character 0, unrecognized input signature
Can Anyone suggest how control characters are usually handled when XML FROM SQL data medium is used?
You have to wrap that value in an XML element:
XMLELEMENT("TrialData" , #TrialData)
generates
<TrialData>←</TrialData>
Can't seem to edit my old post but I am trying to execute this SQL script
UPDATE Promotions
set Description = '£5 Off £25 Spend',
UsageText = '£5 Off £25 Spend',
EmailText = '£5 Off £25 Spend',
PromotionRuleData= '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-13T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed> </PromotionRuleBase><PromotionRuleBase xsi:type="MinimumCartAmountPromotionRule"><CartAmount>24.99</CartAmount></PromotionRuleBase></ArrayOfPromotionRuleBase>',
PromotionDiscountData = '<ArrayOfPromotionDiscountBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionDiscountBase xsi:type="OrderPromotionDiscount"><DiscountType>Fixed</DiscountType><DiscountAmount>5.00</DiscountAmount></PromotionDiscountBase></ArrayOfPromotionDiscountBase>'
where Name = 'test1,test2,etc...'
It comes back with this error
Msg 402, Level 16, State 1, Line 1
The data types varchar and text are incompatible in the equal to operator.
I try to use where CAST(PromotionRuleData as NVARCHAR(MAX))
So the line reads as
CAST(PromotionRuleData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase ...
but no luck.
You cannot compare a string literal against a text column in SQL Server.
Which column is of datatype text ? Your name column that you use in the WHERE clause by any chance?
If so, use this WHERE instead:
WHERE CAST(Name AS VARCHAR(MAX)) = 'test1,test2,etc...'
or better yet: convert your column to a more appropriate datatype like VARCHAR(n) (unless you really need 2 GB = 2 billion characters - if so, then use VARCHAR(MAX))
Trying to run this SQL script
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-11T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
where PromotionDiscountData = '<ArrayOfPromotionRuleBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><PromotionRuleBase xsi:type="StartDatePromotionRule"><StartDate>2013-11-18T00:00:00</StartDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationDatePromotionRule"><ExpirationDate>2014-01-12T00:00:00</ExpirationDate></PromotionRuleBase><PromotionRuleBase xsi:type="ExpirationNumberOfUsesPerCustomerPromotionRule"><NumberOfUsesAllowed>1</NumberOfUsesAllowed></PromotionRuleBase><PromotionRuleBase xsi:type="ProductIdPromotionRule"><ProductIds><int>55232</int></ProductIds><RequireQuantity>false</RequireQuantity><Quantity>1</Quantity><AndTogether>false</AndTogether></PromotionRuleBase></ArrayOfPromotionRuleBase>'
but getting this error
Msg 402, Level 16, State 1, Line 1
The data types xml and varchar are incompatible in the equal to operator.
any idea how to fix this?
Basically within each cell I am trying to change the StartDate only
I suppose the type of PromotionDiscountData column is XML, so that's why I suggest you to use the following code snippet:
update Promotions
set PromotionDiscountData = '<ArrayOfPromotionRuleBase ...'
where CAST(PromotionDiscountData as NVARCHAR(MAX)) = '<ArrayOfPromotionRuleBase...'