Parsing XML using T-SQL in SQL Server 2012 - sql-server
I've parsed XML using namespaces in SQL before but I'm having a real bear with this one. I'm trying to extract the ip addresses from the xml but having no luck. I'm thinking it has something to do with the xsd namespace, but can't seem to find the right combination of namespace declaration and node pathing.
Here's a sample of the xml:
<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo">
<GuestStackInfo xsi:type="GuestStackInfo">
<dnsConfig>
<dhcp>false</dhcp>
<hostName>SXVCUP05</hostName>
<domainName>corp.dtcc.com</domainName>
<ipAddress>172.18.18.13</ipAddress>
<ipAddress>172.18.18.20</ipAddress>
<ipAddress>172.22.43.132</ipAddress>
<ipAddress>172.22.43.148</ipAddress>
<ipAddress>172.22.37.68</ipAddress>
<ipAddress>172.22.37.84</ipAddress>
<searchDomain>corp.dtcc.com</searchDomain>
<searchDomain>dtcc.com</searchDomain>
<searchDomain>backup.dtcc.com</searchDomain>
</dnsConfig>
<ipRouteConfig>
<ipRoute>
<network>0.0.0.0</network>
<prefixLength>0</prefixLength>
<gateway>
<ipAddress>172.28.224.1</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.18.8.133</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.22.8.14</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.0</network>
<prefixLength>24</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.112</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>224.0.0.0</network>
<prefixLength>4</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>255.255.255.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
</ipRouteConfig>
</GuestStackInfo>
</obj>
Using the following code to parse the xml
WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema' as xsd)
SELECT a.b.value('ipAddress[1]', 'varchar(100)') AS ipaddress
FROM #sam_xml.nodes('/obj/GuestStackInfo/dnsConfig') a(b)
Thanks!
The values you are looking for are living in the default namespace xmlns. This is the only namespace you need to declare:
DECLARE #sam_xml XML=
N'<obj xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:vim25" versionId="6.0" xsi:type="ArrayOfGuestStackInfo">
<GuestStackInfo xsi:type="GuestStackInfo">
<dnsConfig>
<dhcp>false</dhcp>
<hostName>SXVCUP05</hostName>
<domainName>corp.dtcc.com</domainName>
<ipAddress>172.18.18.13</ipAddress>
<ipAddress>172.18.18.20</ipAddress>
<ipAddress>172.22.43.132</ipAddress>
<ipAddress>172.22.43.148</ipAddress>
<ipAddress>172.22.37.68</ipAddress>
<ipAddress>172.22.37.84</ipAddress>
<searchDomain>corp.dtcc.com</searchDomain>
<searchDomain>dtcc.com</searchDomain>
<searchDomain>backup.dtcc.com</searchDomain>
</dnsConfig>
<ipRouteConfig>
<ipRoute>
<network>0.0.0.0</network>
<prefixLength>0</prefixLength>
<gateway>
<ipAddress>172.28.224.1</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.18.8.133</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.22.8.14</network>
<prefixLength>32</prefixLength>
<gateway>
<ipAddress>172.28.224.10</ipAddress>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.0</network>
<prefixLength>24</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.112</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>172.28.224.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>224.0.0.0</network>
<prefixLength>4</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
<ipRoute>
<network>255.255.255.255</network>
<prefixLength>32</prefixLength>
<gateway>
<device>0</device>
</gateway>
</ipRoute>
</ipRouteConfig>
</GuestStackInfo>
</obj>';
--the query will use .nodes() to dive one level deeper then your query in order to get all IP-addresses from within <dnsConfig>:
WITH XMLNAMESPACES(DEFAULT 'urn:vim25')
SELECT a.b.value('text()[1]', 'varchar(100)') as ipaddress
FROM #sam_xml.nodes('/obj/GuestStackInfo/dnsConfig/ipAddress') a(b)
Related
Read XML on SQL Server : Select custom attribute
I'm trying to read an XML, it works pretty well on the whole except for the attribute named : <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> which I am trying to get the value "1234567890" Here is the test code: DECLARE #XML XML = '<customers> <customer customer-no="00000001"> <credentials> <login>test#email.com</login> </credentials> <profile> <preferred-locale>fr_BE</preferred-locale> <custom-attributes> <custom-attribute attribute-id="ServiceId">1</custom-attribute> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> <customer customer-no="00000002"> <credentials> <login>test2#email.com</login> </credentials> <profile> <preferred-locale>fr_FR</preferred-locale> <custom-attributes> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> </customers>' SELECT CustomerNo = Events.value('#customer-no', 'int'), --EventType = Events.value('#Type', 'varchar(20)'), CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'), CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'), EventKind =Events.value('(profile/custom-attributes/custom-attribute)[2]', 'varchar(60)') FROM #XML.nodes('/customers/customer') AS XTbl(Events) The current result is: CustomerNo CustomerLogin CustomerLocal EventKind 1 test#email.com fr_BE 1234567890 2 test2#email.com fr_FR NULL And it's logical since custom-attributes are optional, so you can't access them with the index. So I'm looking for a way to access them by name: attribute-id="loyaltyNumber" Thanks,
..filter the path to the attribute which has attribute-id = “loyaltyNumber” EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id="loyaltyNumber"])[1]', 'varchar(60)')
You can just add the attribute-id of the custom attribute to your query: DECLARE #XML XML = '<customers> <customer customer-no="00000001"> <credentials> <login>test#email.com</login> </credentials> <profile> <preferred-locale>fr_BE</preferred-locale> <custom-attributes> <custom-attribute attribute-id="ServiceId">1</custom-attribute> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> <customer customer-no="00000002"> <credentials> <login>test2#email.com</login> </credentials> <profile> <preferred-locale>fr_FR</preferred-locale> <custom-attributes> <custom-attribute attribute-id="loyaltyNumber">1234567777</custom-attribute> </custom-attributes> </profile> <note/> </customer> </customers>' SELECT CustomerNo = Events.value('#customer-no', 'int'), --EventType = Events.value('#Type', 'varchar(20)'), CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'), CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'), EventKind =Events.value('(profile/custom-attributes/custom-attribute[#attribute-id = "loyaltyNumber"])[1]', 'varchar(60)') FROM #XML.nodes('/customers/customer') AS XTbl(Events)
Deadlock between two select
I am having a typical situation of a deadlock between two select statements. I have a JMS listener running, handling two jms message at the same time. They both run a select query and i end up with a key lock on the same table all the time. This is the details of the deadlock. <deadlock-list> <deadlock victim="process276e56fcca8"> <process-list> <process id="process276e56fcca8" taskpriority="0" logused="764" waitresource="KEY: 7:72057594422558720 (8e392f9a7525)" waittime="4129" ownerId="24935297" transactionname="implicit_transaction" lasttranstarted="2017-09-02T10:01:18.853" XDES="0x276dd31f7b8" lockMode="S" schedulerid="5" kpid="25308" status="suspended" spid="65" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2017-09-02T10:01:18.963" lastbatchcompleted="2017-09-02T10:01:18.963" lastattention="1900-01-01T00:00:00.963" clientapp="Microsoft JDBC Driver for SQL Server" hostname="GAETAN-PC" hostpid="0" loginname="fundhive" isolationlevel="read committed (2)" xactid="24935297" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058"> <executionStack> <frame procname="adhoc" line="1" stmtend="274" sqlhandle="0x02000000ad097723a3df0ffd0ae8c2aae2d6e9938b5224fa0000000000000000000000000000000000000000"> unknown </frame> <frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"> unknown </frame> </executionStack> <inputbuf> select * ,61 as userId from getDocumentWorkflowMessages(20285) where 1=1 and vehicleGroupId = '10372' and investorLevelGroupId = '13933' </inputbuf> </process> <process id="process276f302fc28" taskpriority="0" logused="764" waitresource="KEY: 7:72057594422558720 (7d9127e683cc)" waittime="4188" ownerId="24935134" transactionname="implicit_transaction" lasttranstarted="2017-09-02T10:01:18.690" XDES="0x27671594728" lockMode="S" schedulerid="3" kpid="13788" status="suspended" spid="73" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2017-09-02T10:01:18.783" lastbatchcompleted="2017-09-02T10:01:18.783" lastattention="1900-01-01T00:00:00.783" clientapp="Microsoft JDBC Driver for SQL Server" hostname="GAETAN-PC" hostpid="0" loginname="fundhive" isolationlevel="read committed (2)" xactid="24935134" currentdb="7" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128058"> <executionStack> <frame procname="adhoc" line="1" stmtend="274" sqlhandle="0x0200000046471c35dd8038f6b5d5a8a05c3680d8fe3b66dc0000000000000000000000000000000000000000"> unknown </frame> <frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"> unknown </frame> </executionStack> <inputbuf> select * ,61 as userId from getDocumentWorkflowMessages(20285) where 1=1 and vehicleGroupId = '10385' and investorLevelGroupId = '14481' </inputbuf> </process> </process-list> <resource-list> <keylock hobtid="72057594422558720" dbid="7" objectname=" DocumentWorkflowStatuses" indexname="idx_DocumentWorkflowStatuses_isArchived" id="lock276c4fac900" mode="X" associatedObjectId="72057594422558720"> <owner-list> <owner id="process276f302fc28" mode="X"/> </owner-list> <waiter-list> <waiter id="process276e56fcca8" mode="S" requestType="wait"/> </waiter-list> </keylock> <keylock hobtid="72057594422558720" dbid="7" objectname=" DocumentWorkflowStatuses" indexname="idx_DocumentWorkflowStatuses_isArchived" id="lock276e1728600" mode="X" associatedObjectId="72057594422558720"> <owner-list> <owner id="process276e56fcca8" mode="X"/> </owner-list> <waiter-list> <waiter id="process276f302fc28" mode="S" requestType="wait"/> </waiter-list> </keylock> </resource-list> </deadlock> </deadlock-list> I read lots of post but i am not sure how to approach the problem. I tried with and without an index on the table DocumentWorkflowStatus but it's the same things. This is the getDocumentWorkflowMessages query : CREATE FUNCTION [dbo].[getDocumentWorkflowMessages] ( #projectId bigint ) RETURNS TABLE RETURN SELECT t.* ,dbo.WorkflowStatuses.id AS workflowStatusId ,( CASE WHEN isnull(t.workflowScheme_fk, - 1) = - 1 -- IS NULL THEN '' ELSE dbo.WorkflowStatuses.NAME END ) AS workflowStateName ,dbo.WorkflowStatuses.id AS workflowStateId ,CASE WHEN isnull(t.workflowScheme_fk, '') = '' THEN - 1 WHEN WorkflowStatusCategories.id IS NOT NULL THEN WorkflowStatusCategories.id ELSE isnull(( SELECT TOP 1 id FROM dbo.getWorstWorkflowIdForStructure(t.projectLanguageId, t.vehicleGroupId, t.investorLevelGroupId, t.structureId) ), - 1) END AS worstWorkflowCategoryId from dbo.getProjectDetailView(#projectId,1) t LEFT JOIN dbo.DocumentWorkflowStatuses docs ON docs.structure_fk = t.structureId AND isnull(docs.versionLanguage_fk, - 1) = isnull(t.projectLanguageId, - 1) AND isnull(docs.vehicleGroup_fk, 0) = isnull(t.vehicleGroupId, 0) AND isnull(docs.vehicleInvestorGroup_fk, 0) = isnull(t.investorLevelGroupId, 0) AND isnull(docs.country_fk, 0) = isnull(t.countryId,0) LEFT JOIN dbo.WorkflowStatuses ON WorkflowStatuses.id = docs.workflowStatus_fk LEFT JOIN dbo.WorkflowStatusCategories ON WorkflowStatusCategories.id = WorkflowStatuses.workflowStatusCategory_fk GO This is my index : CREATE NONCLUSTERED INDEX idx_DocumentWorkflowStatuses_isArchived ON [dbo].[DocumentWorkflowStatuses] ([structure_fk],[versionLanguage_fk],[vehicleGroup_fk],[vehicleInvestorGroup_fk],[country_fk]) INCLUDE ( [workflowStatus_fk]) GO
LINQ to SQL - Dead Lock on Insert
I am facing deadlock issue on SQL Server -2014 standard edition. Two inserts are getting deadlocked over a clustered index. It is an OLTP database and table is high insert and high read, table already has primary key with clustered index and having more that 13 Million records The SQL Statements are given below exec sp_executesql N'INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID]) VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20, #p21, #p22, #p23, #p24, #p25, #p26, #p27, #p28, #p29, #p30, #p31, #p32, #p33, #p34, #p35, #p36, #p37, #p38, #p39, #p40, #p41, #p42, #p43, #p44, #p45, #p46, #p47, #p48) SELECT [t0].[UID], [t0].[TIMESTAMP] FROM [dbo].[PatientBilledItem] AS [t0] WHERE [t0].[UID] = (SCOPE_IDENTITY())',N'#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000)',#p0=3796014,#p1='2017-06-20 12:12:49.300',#p2=N'Consultancy',#p3=38328,#p4=500,#p5=4985,#p6='2017-06-20 12:13:12.603',#p7=4985,#p8='2017-06-20 12:13:12.603',#p9=N'A',#p10=4,#p11=10692556,#p12=NULL,#p13=500,#p14=NULL,#p15=NULL,#p16=1,#p17=1159,#p18=N'Dr. B K ',#p19=NULL,#p20=500,#p21=NULL,#p22=1395,#p23=NULL,#p24=NULL,#p25=NULL,#p26=NULL,#p27=NULL,#p28=NULL,#p29=NULL,#p30=NULL,#p31=NULL,#p32=NULL,#p33=NULL,#p34=NULL,#p35=NULL,#p36=NULL,#p37=NULL,#p38=NULL,#p39=NULL,#p40=NULL,#p41=NULL,#p42=NULL,#p43=NULL,#p44=NULL,#p45=NULL,#p46=NULL,#p47=NULL,#p48=NULL exec sp_executesql N'INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID]) VALUES (#p0, #p1, #p2, #p3, #p4, #p5, #p6, #p7, #p8, #p9, #p10, #p11, #p12, #p13, #p14, #p15, #p16, #p17, #p18, #p19, #p20, #p21, #p22, #p23, #p24, #p25, #p26, #p27, #p28, #p29, #p30, #p31, #p32, #p33, #p34, #p35, #p36, #p37, #p38, #p39, #p40, #p41, #p42, #p43, #p44, #p45, #p46, #p47, #p48) SELECT [t0].[UID], [t0].[TIMESTAMP] FROM [dbo].[PatientBilledItem] AS [t0] WHERE [t0].[UID] = (SCOPE_IDENTITY())',N'#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000)',#p0=3796013,#p1='2017-06-20 12:13:10.030',#p2=N'Consultancy',#p3=83986,#p4=600,#p5=763,#p6='2017-06-20 12:13:12.023',#p7=763,#p8='2017-06-20 12:13:12.023',#p9=N'A',#p10=8,#p11=10692557,#p12=NULL,#p13=600,#p14=NULL,#p15=NULL,#p16=1,#p17=1159,#p18=N'Dr.L K S',#p19=NULL,#p20=600,#p21=NULL,#p22=1506,#p23=NULL,#p24=NULL,#p25=NULL,#p26=NULL,#p27=NULL,#p28=NULL,#p29=NULL,#p30=NULL,#p31=NULL,#p32=NULL,#p33=NULL,#p34=NULL,#p35=NULL,#p36=NULL,#p37=NULL,#p38=NULL,#p39=NULL,#p40=NULL,#p41=NULL,#p42=NULL,#p43=NULL,#p44=NULL,#p45=NULL,#p46=NULL,#p47=NULL,#p48=NULL Yes the table is having trigger as well The event XML is attached <deadlock> <victim-list> <victimProcess id="processa19016ca8" /> </victim-list> <process-list> <process id="processa19016ca8" taskpriority="0" logused="3888" waitresource="KEY: 9:72057699966713856 (ffffffffffff)" waittime="1034" ownerId="1298235110" transactionname="user_transaction" lasttranstarted="2017-06-20T11:52:42.003" XDES="0x714fe0d90" lockMode="RangeI-N" schedulerid="1" kpid="7856" status="suspended" spid="66" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2017-06-20T11:52:42.830" lastbatchcompleted="2017-06-20T11:52:42.820" lastattention="1900-01-01T00:00:00.820" clientapp=".Net SqlClient Data Provider" hostname="WIN-2HO2RRV99BU" hostpid="2980" loginname="sa" isolationlevel="serializable (4)" xactid="1298235110" currentdb="9" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056"> <executionStack> <frame procname="adhoc" line="1" stmtstart="1236" stmtend="3454" sqlhandle="0x0200000064f4502b431082b3ac55b13757583f58c2c98c7c0000000000000000000000000000000000000000"> unknown </frame> <frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"> unknown </frame> </executionStack> <inputbuf> (#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000))INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [Sp </inputbuf> </process> <process id="process18572db848" taskpriority="0" logused="3896" waitresource="KEY: 9:72057699966713856 (ffffffffffff)" waittime="1012" ownerId="1298235129" transactionname="user_transaction" lasttranstarted="2017-06-20T11:52:42.027" XDES="0x1ada4fed90" lockMode="RangeI-N" schedulerid="1" kpid="14928" status="suspended" spid="118" sbid="0" ecid="0" priority="0" trancount="2" lastbatchstarted="2017-06-20T11:52:42.853" lastbatchcompleted="2017-06-20T11:52:42.847" lastattention="1900-01-01T00:00:00.847" clientapp=".Net SqlClient Data Provider" hostname="WIN-PGECRKPS51J" hostpid="3092" loginname="sa" isolationlevel="serializable (4)" xactid="1298235129" currentdb="9" lockTimeout="4294967295" clientoption1="671088672" clientoption2="128056"> <executionStack> <frame procname="adhoc" line="1" stmtstart="1236" stmtend="3454" sqlhandle="0x0200000064f4502b431082b3ac55b13757583f58c2c98c7c0000000000000000000000000000000000000000"> unknown </frame> <frame procname="unknown" line="1" sqlhandle="0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"> unknown </frame> </executionStack> <inputbuf> (#p0 bigint,#p1 datetime,#p2 nvarchar(4000),#p3 int,#p4 float,#p5 int,#p6 datetime,#p7 int,#p8 datetime,#p9 nvarchar(4000),#p10 int,#p11 bigint,#p12 float,#p13 float,#p14 nvarchar(4000),#p15 nvarchar(4000),#p16 float,#p17 int,#p18 nvarchar(4000),#p19 int,#p20 float,#p21 float,#p22 int,#p23 int,#p24 float,#p25 nvarchar(4000),#p26 float,#p27 float,#p28 float,#p29 int,#p30 bigint,#p31 bigint,#p32 nvarchar(4000),#p33 float,#p34 float,#p35 nvarchar(4000),#p36 int,#p37 int,#p38 float,#p39 int,#p40 bigint,#p41 float,#p42 float,#p43 nvarchar(4000),#p44 datetime,#p45 int,#p46 nvarchar(4000),#p47 int,#p48 nvarchar(4000))INSERT INTO [dbo].[PatientBilledItem]([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [Sp </inputbuf> </process> </process-list> <resource-list> <keylock hobtid="72057699966713856" dbid="9" objectname="HEALTHOBJECT.dbo.PatientBilledItem" indexname="IX_PatientBillableItem" id="lock28bc25a00" mode="RangeS-S" associatedObjectId="72057699966713856"> <owner-list> <owner id="process18572db848" mode="RangeS-S" /> <owner id="process18572db848" mode="RangeI-N" requestType="convert" /> </owner-list> <waiter-list> <waiter id="processa19016ca8" mode="RangeI-N" requestType="convert" /> </waiter-list> </keylock> <keylock hobtid="72057699966713856" dbid="9" objectname="HEALTHOBJECT.dbo.PatientBilledItem" indexname="IX_PatientBillableItem" id="lock28bc25a00" mode="RangeS-S" associatedObjectId="72057699966713856"> <owner-list> <owner id="processa19016ca8" mode="RangeS-S" /> <owner id="processa19016ca8" mode="RangeI-N" requestType="convert" /> </owner-list> <waiter-list> <waiter id="process18572db848" mode="RangeI-N" requestType="convert" /> </waiter-list> </keylock> </resource-list> </deadlock>
INSERTs alone won't deadlock each other. If you call SELECT WHERE UID=SCOPE_IDENTITY() in the same transaction though, the server will have to take a SHARED lock on any indexes that contain UID. That's because you tried to read the value of SCOPE_IDENTITY() which will change if another row is added, leading to inconsistent data. If you use the REPEATABLE READ isolation level, you could end up with a deadlock. The solution is very easy - don't use a separate SELECT. You can use the OUTPUT clause of INSERT to get all the new values, like identity, default values etc : INSERT INTO [dbo].[PatientBilledItem] ([PatientBillUID], [EventOccuredDttm], [ServiceName], [BillableItemUID], [Amount], [CUser], [CWhen], [MUser], [MWhen], [StatusFlag], [OwnerOrganisationUID], [PatientBillableItemUID], [Discount], [NetAmount], [Description], [Comments], [ItemMultiplier], [BSMDDUID], [ItemName], [DiscountAuthorizedBy], [ConsultantShare], [ServiceTax], [CareProviderUID], [QNUOMUID], [SpecialAmount], [IsRefunded], [ConsultantDiscount], [EducationCess], [HigherEducationCess], [RecordedByUID], [OriginalCreditBillUID], [PatientPackageItemUID], [BatchID], [VATPercentage], [InternalCost], [CalculateTaxOnMRP], [StoreUID], [BillPackageUID], [SplitDiscount], [RSLVLUID], [ParentUID], [PackageItemAmount], [RoundOff], [AuthNo], [ExpiryDttm], [BILGRDUID], [ApprovalCode], [ApprovedBy], [InternalBatchID]) OUTPUT inserted.UID,inserted.Timestamp VALUES ...... You can combine this with the SNAPSHOT isolation level to reduce blocking between transactions.
Based on provided info, first assumption is that these deadlocks are caused by one of following missing indexes CREATE /*UNIQUE*/ NONCLUSTERED INDEX IUN_PatientBilledItem_UID_#_TIMESTAMP ON dbo.PatientBilledItem (UID) INCLUDE(TIMESTAMP) or CREATE /*UNIQUE*/ NONCLUSTERED INDEX IUN_PatientBilledItem_UID_TIMESTAMP ON dbo.PatientBilledItem (UID, TIMESTAMP) needed by following SELECT statements: SELECT [t0].[UID], [t0].[TIMESTAMP] FROM [dbo].[PatientBilledItem] AS [t0] WHERE [t0].[UID] = (SCOPE_IDENTITY()) which generate RangeS-S locks (also because TX isolation level is SERIALIZABLE). Note: Uncomment /*UNIQUE*/ keyword if current index has unique values or unique pair of values (compound index).
UPDATING AN XML Tag Value
Here is my sample script. What I want to do is to update tags intstatusID And strStatus of xml variable #xmlPlan that satisfy join condition Declare #xmlPlan xml = N'<planBean> <intPlanID>1</intPlanID> <intStatusID>2</intStatusID> <strPlanStatus>Published</strPlanStatus> <strName>1 Loyalty Programs Plan</strName> <supplierGroupsList> <supplierGroup> <intPlanSupplierGroupID>1</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 1</strName> <intStatusID>8</intStatusID> <strStatus>New</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Enabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>HJK *</strName> <strSourceSupplierID>0XX135</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> <supplier> <strName>KGH LABORATORIES LTD</strName> <strSourceSupplierID>0XX136</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> <supplierGroup> <intPlanSupplierGroupID>2</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 2</strName> <intStatusID>8</intStatusID> <strStatus>New</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Disabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>ABC FOODSERVICE *</strName> <strSourceSupplierID>0XX134</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> <supplierGroup> <intPlanSupplierGroupID>3</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 3</strName> <intStatusID>8</intStatusID> <strStatus>New</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Disabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>XZ FOODSERVICE *</strName> <strSourceSupplierID>0XX133</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> </supplierGroupsList> </planBean>' Create Table #SupplierGroups (strName varchar(500), intStatus int, strStatus varchar(200) ) Insert Into #SupplierGroups (strName, intStatus, strStatus) Values ('Supplier Group 1', 10, 'Active'), ('Supplier Group 3', 10, 'Active') SELECT a.*, t.c.value('(strName/text())[1]', 'varchar(500)') FROM #xmlPlan.nodes('planBean/supplierGroupsList/supplierGroup') t(c) Inner Join #SupplierGroups a ON a.strName = t.c.value('(strName/text())[1]', 'varchar(500)') -- I want XML tags intStatusID AND strStatus -- To be updated as [[intStatusID 10 /intStatusID, strStatus Active /strStatus]] of "Supplier Group 1" And "Supplier Group 3" only -- I want #xmlPlan variable like below /* <planBean> <intPlanID>1</intPlanID> <intStatusID>2</intStatusID> <strPlanStatus>Published</strPlanStatus> <strName>1 Loyalty Programs Plan</strName> <supplierGroupsList> <supplierGroup> <intPlanSupplierGroupID>1</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 1</strName> <intStatusID>10</intStatusID> <strStatus>Active</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Enabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>HJK *</strName> <strSourceSupplierID>0XX135</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> <supplier> <strName>KGH LABORATORIES LTD</strName> <strSourceSupplierID>0XX136</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> <supplierGroup> <intPlanSupplierGroupID>2</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 2</strName> <intStatusID>8</intStatusID> <strStatus>New</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Disabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>ABC FOODSERVICE *</strName> <strSourceSupplierID>0XX134</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> <supplierGroup> <intPlanSupplierGroupID>3</intPlanSupplierGroupID> <intPlanID>1</intPlanID> <strName>Supplier Group 3</strName> <intStatusID>10</intStatusID> <strStatus>Active</strStatus> <copySGAction>Enabled</copySGAction> <editSGAction>Enabled</editSGAction> <deleteSGAction>Disabled</deleteSGAction> <intActionId>5</intActionId> <supplierList> <supplier> <strName>XZ FOODSERVICE *</strName> <strSourceSupplierID>0XX133</strSourceSupplierID> <intStatusID>13</intStatusID> <strStatus>New</strStatus> <dtStartDate> <maxDate>20160101</maxDate> <minDate>20160101</minDate> <orgDate>20160101</orgDate> </dtStartDate> <dtEndDate> <maxDate>20161231</maxDate> <minDate>20161231</minDate> <orgDate>20161231</orgDate> </dtEndDate> </supplier> </supplierList> </supplierGroup> </supplierGroupsList> </planBean> */
Deadlock - SQL Server 2012 TempDB
We have an issue with deadlock in a SQL-server agent job. Attached is the xml_deadlock_report. We have some stored procedures that executes in the job and we use temporary tables #. Some example: SET #columnsSQL = ' SELECT #columnArrayOut = COALESCE(#columnArrayOut + '','' , '' '' ) ' + CHAR(13) +' + COLUMN_NAME ' + CHAR(13) +'from tempdb.INFORMATION_SCHEMA.COLUMNS ' + CHAR(13) +' where table_name = ' + CHAR(13) +' object_name(' + CHAR(13) +' object_id(''tempdb..'+ #globalTableName +'''),' + CHAR(13) +' (select database_id from sys.databases where name = ''tempdb''))' + CHAR(13) +'AND COLUMN_NAME <> ''SeqNbr''' + CHAR(13) +'ORDER BY ORDINAL_POSITION ' SET #sqlScript = 'INSERT INTO '+ #globalTableName + CHAR(13) + 'SELECT * '+ CHAR(13) + 'FROM ('+ CHAR(13) + 'SELECT '+ #columnArray +', CAST(ROW_NUMBER() OVER(ORDER BY '+ #orderBy +') AS VARCHAR) AS SeqNbr FROM '+ #TableName +' WITH (NOLOCK)'+ CHAR(13) + 'UNION'+ CHAR(13) + 'SELECT '+ #columnArrayHeading +', ''0'''+ CHAR(13) + ') AS Temp'+ CHAR(13) + 'ORDER BY CAST(SeqNbr AS INT) ASC'+ CHAR(13) + 'SELECT '+ #columnArrayOut +', CASE convert(INT, SeqNbr) WHEN 0 THEN ''SeqNbr'' ELSE SeqNbr END FROM '+ #globalTableName + CHAR(13) + 'ORDER BY CAST(SeqNbr AS INT) ASC'+ CHAR(13) + 'DROP TABLE '+ #globalTableName; I am thinking it must be that we need to add nolock after some code, am I on the right track? We have different customers on the same SQL-Server instance and it is SQL-Server 2012. <deadlock> <victim-list> <victimProcess id="process6ff025c38" /> </victim-list> <process-list> <process id="process6ff025c38" taskpriority="0" logused="27564" waitresource="KEY: 2:281474978938880 (6f07e5ef564d)" waittime="3457" ownerId="2943291814" transactionname="ExportFilter" lasttranstarted="2015-12-28T07:15:10.007" XDES="0xdb84ad760" lockMode="S" schedulerid="3" kpid="1536" status="suspended" spid="72" sbid="0" ecid="0" priority="0" trancount="1" lastbatchstarted="2015-12-28T07:15:09.980" lastbatchcompleted="2015-12-28T07:15:09.970" lastattention="1900-01-01T00:00:00.970" clientapp="SQLAgent - TSQL JobStep (Job 0x93F327E5D307B843A95C7883AEE52C41 : Step 2)" isolationlevel="read committed (2)" xactid="2943291814" currentdb="9" lockTimeout="4294967295" clientoption1="538968096" clientoption2="128056"> <executionStack> <frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x02000000bdb8ee370644f54bc076833c46eb8ce8ae6c21370000000000000000000000000000000000000000"> SELECT #columnArrayOut = COALESCE(#columnArrayOut + ',' , ' ' ) + COLUMN_NAME from tempdb.INFORMATION_SCHEMA.COLUMNS where table_name = object_name( object_id('tempdb..##w_xxx004718_2328_MG_SO'), (select database_id from sys.databases where name = 'tempdb')) AND COLUMN_NAME <> 'SeqNbr' ORDER BY ORDINAL_POSITION </frame> <frame procname="mssqlsystemresource.sys.sp_executesql" line="1" stmtstart="-1" sqlhandle="0x0400ff7f427f99d9010000000000000000000000000000000000000000000000000000000000000000000000"> sp_executesql </frame> isolationlevel="read committed (2)" <frame procname="adhoc" line="1" stmtstart="78" sqlhandle="0x020000006df5ee06624475cdc854f45d0579dd554dddd61c0000000000000000000000000000000000000000"> SELECT #columnArrayOut = COALESCE(#columnArrayOut + ',' , ' ' ) + COLUMN_NAME from tempdb.INFORMATION_SCHEMA.COLUMNS where table_name = object_name( object_id('tempdb..##w_xxx050820_2164_MG_LO'), (select database_id from sys.databases where name = 'tempdb')) AND COLUMN_NAME <> 'SeqNbr' ORDER BY ORDINAL_POSITION </frame> <frame procname="mssqlsystemresource.sys.sp_executesql" line="1" stmtstart="-1" sqlhandle="0x0400ff7f427f99d9010000000000000000000000000000000000000000000000000000000000000000000000"> sp_executesql </frame> <resource-list> <keylock hobtid="281474978938880" dbid="2" objectname="tempdb.sys.sysschobjs" indexname="clst" id="lock15895c380" mode="X" associatedObjectId="281474978938880"> <owner-list> <owner id="processea1d7ecf8" mode="X" /> </owner-list> <waiter-list> <waiter id="process6ff025c38" mode="S" requestType="wait" /> </waiter-list> </keylock> <keylock hobtid="281474978938880" dbid="2" objectname="tempdb.sys.sysschobjs" indexname="clst" id="lock375a18480" mode="X" associatedObjectId="281474978938880"> <owner-list> <owner id="process6ff025c38" mode="X" /> </owner-list> <waiter-list> <waiter id="processea1d7ecf8" mode="S" requestType="wait" /> </waiter-list> </keylock> </resource-list> </deadlock>