SQL stored procedure: how to concatenate parameter value? - sql

This is original code:
EXEC #ReturnCode = msdb.dbo.sp_add_jobstep #job_id=#jobId, #step_name=N'TestStep',
#step_id=1,
#cmdexec_success_code=0,
#on_success_action=1,
#on_success_step_id=0,
#on_fail_action=2,
#on_fail_step_id=0,
#retry_attempts=0,
#retry_interval=0,
#os_run_priority=0, #subsystem=N'SSIS',
#command=N'/ISSERVER "\"\SSISDB\FolderName\ProjectName\PackageName.dtsx\"" /SERVER localhost /ENVREFERENCE 9 /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E',
#database_name=N'master', ...
I want to do something like this (replace value after ENVREFERENCE in long string with dynamic value):
DECLARE #myVariable int
SET #myVariable = 10
EXEC #ReturnCode = msdb.dbo.sp_add_jobstep #job_id=#jobId, #step_name=N'TestStep',
#step_id=1,
#cmdexec_success_code=0,
#on_success_action=1,
#on_success_step_id=0,
#on_fail_action=2,
#on_fail_step_id=0,
#retry_attempts=0,
#retry_interval=0,
#os_run_priority=0, #subsystem=N'SSIS',
#command=N'/ISSERVER "\"\SSISDB\AccuCenter\AccuCenterDBImport\VehicleMake.dtsx\"" /SERVER localhost /ENVREFERENCE ' + #myVariable + N' /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E',
#database_name=N'master'

Couldn't you put the whole in a variable ?
DECLARE #myCommand nvarchar(max)
SET #myCommand = N'/ISSERVER "\"\SSISDB\AccuCenter\AccuCenterDBImport\VehicleMake.dtsx\"" /SERVER localhost /ENVREFERENCE '
+ LTRIM(STR(#myVariable))
+ N' /Par "\"$ServerOption::LOGGING_LEVEL(Int16)\"";1 /Par "\"$ServerOption::SYNCHRONIZED(Boolean)\"";True /CALLERINFO SQLAGENT /REPORTING E'
By the way, you could build it entirely to fit your needs.
Then use it when calling sp_add_jobstep:
EXEC #ReturnCode = msdb.dbo.sp_add_jobstep
#job_id=#jobId,
#step_name=N'TestStep',
#step_id=1,
#cmdexec_success_code=0,
#on_success_action=1,
#on_success_step_id=0,
#on_fail_action=2,
#on_fail_step_id=0,
#retry_attempts=0,
#retry_interval=0,
#os_run_priority=0, #subsystem=N'SSIS',
#command=#myCommand
#database_name=N'master'

Related

Combining Stored Procedures Result Sets [duplicate]

This question already has answers here:
UNION the results of multiple stored procedures
(5 answers)
Closed 4 years ago.
I am executing multiple stored procedures which return results, but I want it to look like one result set. I tried UNION ALL but that only works with SELECT statements.
EXEC TaskSummary #TaskName = "Demo", #Days ="5", #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Framing", #Days="15", #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Electrical Rough-In", #Days= 7, #GroupName = "Foxtrot"
EXEC TaskSummary #TaskName = "Insulation", #Days = 2, #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Exterior Doors", #Days= 2, #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Install Windows", #Days= 2, #GroupName= "Charlie"
EXEC TaskSummary #TaskName = "Bathroom Tiles", #Days= 6, #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Prime Walls" , #Days= "2", #GroupName = "Bravo"
EXEC TaskSummary #TaskName = "Painting Interior" , #Days= "3" , #GroupName = "Charlie"
EXEC TaskSummary #TaskName = "Mill Work", #Days="3", #GroupName= "Charlie"
EXEC TaskSummary #TaskName = "Backsplash Install" , #Days= "1" , #GroupName= "Charlie"
EXEC TaskSummary #TaskName = "Electrical Dress-Up" , #Days="1" , #GroupName= "Foxtrot"
EXEC TaskSummary #TaskName = "Interior Hardware Install", #Days= "2" , #GroupName= "Charlie"
EXEC TaskSummary #TaskName = "Final Touch Up", #Days= "2", #GroupName = "Bravo"
EXEC TaskSummary #TaskName = "Punch List", #Days= "2", #GroupName= "Charlie"
You can create one temp table and insert all stored procedure result within it, then perform a select query:
--CREATE TEMP TABLE
CREATE TABLE #TBL(Column1 ....)
--Eexute all stored procedures with an insert statment
INSERT INTO #TBL
EXEC TaskSummary #TaskName = "Demo", #Days ="5", #GroupName = "Charlie"
INSERT INTO #TBL
EXEC TaskSummary #TaskName = "Framing", #Days="15", #GroupName = "Charlie"
...
--Select data from temp table
SELECT * FROM #TBL
You can use a table variable to collect all the results.
DECLARE #u TABLE
(<columns as the result of TaskSummary regarding number, order and type>);
INSERT INTO #u
EXECUTE TaskSummary <parameters>;
...
INSERT INTO #u
EXECUTE TaskSummary <other parameters>;
SELECT *
FROM #u;

SQL Server Naming Policy Management blocking correctly named stored procedure

I want to enforce a naming convention for user stored procedures to begin with 'usp_'. I created a policy and I receive an error no matter what the name of the stored procedure.
The condition is as follows:
DECLARE #condition_id INT
EXEC msdb.dbo.sp_syspolicy_add_condition
#name = N'con_multipart_name_usp_prefix',
#description = N'',
#facet = N'IMultipartNameFacet',
#expression = N'<Operator>
<TypeClass>Bool</TypeClass>
<OpType>LIKE</OpType>
<Count>2</Count>
<Attribute>
<TypeClass>String</TypeClass>
<Name>Name</Name>
</Attribute>
<Constant>
<TypeClass>String</TypeClass>
<ObjType>System.String</ObjType>
<Value>usp_%</Value>
</Constant>
</Operator>',
#is_name_condition = 2,
#obj_name = N'usp_%',
#condition_id = #condition_id OUTPUT
SELECT #condition_id
GO
Here is the SSMS properties box for the condition
The policy is as follows:
Declare #object_set_id int
EXEC msdb.dbo.sp_syspolicy_add_object_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #facet=N'IMultipartNameFacet', #object_set_id=#object_set_id OUTPUT
Select #object_set_id
Declare #target_set_id int
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/Sequence', #type=N'SEQUENCE', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/Sequence', #level_name=N'Sequence', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/StoredProcedure', #type=N'PROCEDURE', #enabled=True, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/StoredProcedure', #level_name=N'StoredProcedure', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/Synonym', #type=N'SYNONYM', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/Synonym', #level_name=N'Synonym', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/Table', #type=N'TABLE', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/Table', #level_name=N'Table', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/UserDefinedFunction', #type=N'FUNCTION', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/UserDefinedFunction', #level_name=N'UserDefinedFunction', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/UserDefinedType', #type=N'TYPE', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/UserDefinedType', #level_name=N'UserDefinedType', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/View', #type=N'VIEW', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/View', #level_name=N'View', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set #object_set_name=N'policy_naming_usp_prefix_ObjectSet', #type_skeleton=N'Server/Database/XmlSchemaCollection', #type=N'XMLSCHEMACOLLECTION', #enabled=False, #target_set_id=#target_set_id OUTPUT
Select #target_set_id
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database/XmlSchemaCollection', #level_name=N'XmlSchemaCollection', #condition_name=N'', #target_set_level_id=0
EXEC msdb.dbo.sp_syspolicy_add_target_set_level #target_set_id=#target_set_id, #type_skeleton=N'Server/Database', #level_name=N'Database', #condition_name=N'', #target_set_level_id=0
GO
Declare #policy_id int
EXEC msdb.dbo.sp_syspolicy_add_policy #name=N'policy_naming_usp_prefix', #condition_name=N'con_multipart_name_usp_prefix', #policy_category=N'', #description=N'', #help_text=N'', #help_link=N'', #schedule_uid=N'00000000-0000-0000-0000-000000000000', #execution_mode=1, #is_enabled=True, #policy_id=#policy_id OUTPUT, #root_condition_name=N'', #object_set=N'policy_naming_usp_prefix_ObjectSet'
Select #policy_id
GO
Here is the SSMS properties box for the policy
When I create a sample stored procedure that conforms to this naming convention, the policy blocks the create statement.
Here is the sample stored procedure:
CREATE PROCEDURE usp_test
AS
print 'test'
Finally, here is the error output:
Policy 'policy_naming_usp_prefix' has been violated by 'SQLSERVER:\SQL\SERVERNAME\Databases\DBNAME\StoredProcedures\dbo.usp_test'.
This transaction will be rolled back.
Policy condition: '#Name LIKE 'usp_%''
Policy description: ''
Additional help: '' : ''
Statement: '
CREATE PROCEDURE usp_test
AS
print 'test''.
Msg 515, Level 16, State 2, Procedure msdb.sys.sp_syspolicy_execute_policy, Line 69 [Batch Start Line 0]
Cannot insert the value NULL into column 'target_query_expression', table 'msdb.dbo.syspolicy_policy_execution_history_details_internal'; column does not allow nulls. INSERT fails.
The statement has been terminated.
What am I doing wrong here?
Based on your comments of being on the RTM version of 2017, you will need to updated to CU2 (at a minimum) as described in this KB.
Here's a link to that cumulative update.
However, if you are using Query Store you need CU3 instead.
Lastly, you could just come "up to date" and get CU13.

Send email does not printing query result in csv

I'm using following code to send an attachment in email in CSV format, this is working fine but when i try to use the original query i-e select col1, col2, col3 .. from DBTable in place of test query select ''1'', ''2'', ''3'', ''4'' email is not being sent, even there is not any error but email not being sent, stored procedure get executed in the same way with both queries.
EXEC msdb..sp_send_dbmail #profile_name='Notificatins and Alerts',
#recipients = #EmailRecipient,
#subject = #MessageSubject,
#body = #MessageBody,
#body_format = 'HTML',
#query='SET NOCOUNT ON;
select ''sep=,''
select ''1'', ''2'', ''3'', ''4'' ',
--select ''1'', ''2'', ''3'', ''4''
#attach_query_result_as_file=1,
#query_attachment_filename = 'Results.csv',
#query_result_separator =',',
#query_result_no_padding=1, --trim
--#query_result_width=32767, --stop wordwrap
#exclude_query_output =1,
#append_query_error = 0,
#query_result_header =0;
can any one help me on that.
Here is the solution. Hope this will help someone:
EXEC msdb..sp_send_dbmail #profile_name='Notificatins and Alerts',
#recipients = #EmailRecipient,
#subject = #MessageSubject,
#body = #MessageBody,
#body_format = 'HTML',
#query='SET NOCOUNT ON;
select ''sep=,''
select col1, col2, col3 .. from dbName.dbo.DBTable ',
--select ''1'', ''2'', ''3'', ''4''
#attach_query_result_as_file=1,
#query_attachment_filename = 'Results.csv',
#query_result_separator =',',
#query_result_no_padding=1, --trim
--#query_result_width=32767, --stop wordwrap
#exclude_query_output =1,
#append_query_error = 0,
#query_result_header =0;
simply one have to use DBName.dbo with table name that was missing in my query.

OPENXML to extract data from XML and add to the table

I have a XML file from where I want to get the data and place it into the SQL table. My problem is the path is different in the same XML file.
For an example,
one path = 'root/DescriptorRecord/DescriptorName'
second path = 'root/DescriptorRecord/ConceptList/Concept/TermList/Term'
I have no clue that how to get a value from two different path in a same XML.
Following is my XML :
<root>
<DescriptorRecord DescriptorClass = "1">
<DescriptorName>
<String>abc</String>
</DescriptorName>
<ConceptList>
<Concept PreferredConceptYN="Y">
<TermList>
<Term ConceptPreferredTermYN="Y" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="Y">
<String>abc</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="N" RecordPreferredTermYN="N">
<String>pqr</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="N">
<String>xyz</String>
</Term>
</TermList>
</Concept>
</ConceptList>
</DescriptorRecord>
<DescriptorRecord DescriptorClass = "1">
<DescriptorName>
<String>Numerics</String>
</DescriptorName>
<ConceptList>
<Concept PreferredConceptYN="Y">
<TermList>
<Term ConceptPreferredTermYN="Y" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="Y">
<String>123</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="N" RecordPreferredTermYN="N">
<String>456</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="N">
<String>789</String>
</Term>
</TermList>
</Concept>
</ConceptList>
</DescriptorRecord>
</root>
My SQL query :
DECLARE #XML AS XML, #hDoc AS INT, #SQL NVARCHAR (MAX)
SELECT #XML = XMLData FROM XMLwithOpenXML
EXEC sp_xml_preparedocument #hDoc OUTPUT, #XML
SELECT Descriptor, Terms
FROM OPENXML(#hDoc, 'root/DescriptorRecord/DescriptorName')
WITH
(
Descriptor [varchar](100) 'String',
Terms [varchar](100) 'String'
)
SELECT Terms
FROM OPENXML(#hDoc, 'root/DescriptorRecord/ConceptList/Concept/TermList/Term')
WITH
(
Descriptor [varchar](100) 'String'
Terms [varchar](100) 'String'
)
EXEC sp_xml_removedocument #hDoc
GO
Result :
Expected Result
So, setting up your sample in a variable:
DECLARE #X XML = N'<root>
<DescriptorRecord DescriptorClass = "1">
<DescriptorName>
<String>abc</String>
</DescriptorName>
<ConceptList>
<Concept PreferredConceptYN="Y">
<TermList>
<Term ConceptPreferredTermYN="Y" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="Y">
<String>abc</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="N" RecordPreferredTermYN="N">
<String>pqr</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="N">
<String>xyz</String>
</Term>
</TermList>
</Concept>
</ConceptList>
</DescriptorRecord>
<DescriptorRecord DescriptorClass = "1">
<DescriptorName>
<String>Numerics</String>
</DescriptorName>
<ConceptList>
<Concept PreferredConceptYN="Y">
<TermList>
<Term ConceptPreferredTermYN="Y" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="Y">
<String>123</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="N" RecordPreferredTermYN="N">
<String>456</String>
</Term>
<Term ConceptPreferredTermYN="N" IsPermutedTermYN="N" LexicalTag="NON" PrintFlagYN="Y" RecordPreferredTermYN="N">
<String>789</String>
</Term>
</TermList>
</Concept>
</ConceptList>
</DescriptorRecord>
</root>'
You'll want to select the descriptors first in an inner query, and return the results to an outer query, where you can use the descriptor column as a xpath criteria in the second .nodes() call. Like so:
SELECT Descriptor.Data.value('./DescriptorName[1]/String[1]','nvarchar(200)') AS Descriptor, Terms.Data.value('.','nvarchar(200)') AS Terms
FROM #x.nodes('root/DescriptorRecord') Descriptor(Data)
cross apply Descriptor.Data.nodes('./ConceptList/Concept/TermList/Term/String') Terms(Data)
EDIT: An attempt to make it less computationally expensive.

Updating Xml attributes with new values in a SQL Server 2008 table

I have a table in SQL Server 2008 that it has some columns. One of these columns is in Xml format
and I want to update some attributes.
For example my Xml column's name is XmlText and it's value in 5 first rows is such as:
<Identification Name="John" Family="Brown" Age="30" />
<Identification Name="Smith" Family="Johnson" Age="35" />
<Identification Name="Jessy" Family="Albert" Age="60" />
<Identification Name="Mike" Family="Brown" Age="23" />
<Identification Name="Sarah" Family="Johnson" Age="30" />
and I want to change all Age attributes that are 30 to 40 such as below:
<Identification Name="John" Family="Brown" Age="40" />
<Identification Name="Smith" Family="Johnson" Age="35" />
<Identification Name="Jessy" Family="Albert" Age="60" />
<Identification Name="Mike" Family="Brown" Age="23" />
<Identification Name="Sarah" Family="Johnson" Age="40" />
From the early versions of your question it looks like your XML actually is on different rows in a table. If that is the case you can use this.
update YourTable set
XMLText.modify('replace value of (/Identification/#Age)[1] with "40"')
where XMLText.value('(/Identification/#Age)[1]', 'int') = 30
Working sample using a table variable.
declare #T table(XMLText xml)
insert into #T values('<Identification Name="John" Family="Brown" Age="30" />')
insert into #T values('<Identification Name="Smith" Family="Johnson" Age="35" />')
insert into #T values('<Identification Name="Jessy" Family="Albert" Age="60" />')
insert into #T values('<Identification Name="Mike" Family="Brown" Age="23" />')
insert into #T values('<Identification Name="Sarah" Family="Johnson" Age="30" />')
update #T set
XMLText.modify('replace value of (/Identification/#Age)[1] with "40"')
where XMLText.value('(/Identification/#Age)[1]', 'int') = 30
select *
from #T
Try this:
declare #xml XML
SET #xml = '<Root>
<Identification Name="John" Family="Brown" Age="30" />
<Identification Name="Smith" Family="Johnson" Age="35" />
<Identification Name="Jessy" Family="Albert" Age="60" />
<Identification Name="Mike" Family="Brown" Age="23" />
<Identification Name="Sarah" Family="Johnson" Age="30" />
</Root>'
DECLARE #nodeCount int
DECLARE #i int
SET #i = 1
SELECT #nodeCount = #xml.value('count(/Root/Identification/#Age)','int')
PRINT 'Number of nodes found: ' + STR(#nodeCount)
WHILE (#i <= #nodeCount)
BEGIN
Set #xml.modify('replace value of (/Root/Identification/#Age)[.=30][1] with "40"')
SET #i = #i + 1
END
SELECT #xml
The modify method is your response. But if you need to have a condition you can use if expression in with section of this method.
DECLARE #t TABLE (RecordXML XML);
Declare #xml XML
SET #xml = '<Root>
<Identification Name="John" Family="Brown" Age="30" />
<Identification Name="Smith" Family="Johnson" Age="35" />
<Identification Name="Jessy" Family="Albert" Age="60" />
<Identification Name="Mike" Family="Brown" Age="23" />
<Identification Name="Sarah" Family="Johnson" Age="30" />
</Root>'
INSERT #t VALUES (#xml);
Declare #value nvarchar(50)
DECLARE #oldvalue nvarchar(50)
SET #value = '40'
SET #oldvalue = '30'
Declare #update_count xml
select #update_count = #xml.query('count(/Root/Identification/#Age[.=sql:variable("#oldvalue")])')
Declare #number int
select #number = convert(int, (convert(nvarchar(50), #update_count)))
declare #Node int
set #Node = 1
while #Node <= #number
begin
UPDATE
#t
SET
RecordXML.modify('replace value of
(/Root/Identification/#Age[.=sql:variable("#oldvalue")])[1] with sql:variable("#value")')
WHERE
RecordXML.exist('/Root/Identification[#Age=sql:variable("#oldvalue")]') = 1;
set #Node = #Node + 1
end
SELECT * FROM #t;