tsql

SQL Server XML Column Performance

柔情痞子 提交于 2020-06-28 06:03:50
问题 Converting nText columns which contained XML to the XML data type has resulted in worse performance in SQL Server. I am currently working on a project where nText columns have been used to store valid XML. I have successfully migrated these columns to the XML data type. However according to SQL Profiler the performance of the XML data type is worse than using nText or nvarchar(max) to store the XML. Everything I have read implies that this should not be the case. In order to verify this I

SQL Server: How to query data all rows as Json object into next to other columns?

天涯浪子 提交于 2020-06-27 13:08:31
问题 I have data like this: I want to query result like this: Here is my code SELECT PML_CODE ,PML_NAME_ENG ,( SELECT PML_ID ,PML_NO ,PML_CODE ,PML_NAME_ENG ,PML_FORMULA FROM DSP.PARAMET_LIST AS A WITH(NOLOCK) WHERE A.PML_ID = B.PML_ID FOR JSON PATH, WITHOUT_ARRAY_WRAPPER ) AS BR_OBJECT FROM DSP.PARAMET_LIST AS B WITH(NOLOCK) My code works for what I want, but I want to know if there is a better, faster way to write this query? 回答1: Next time please do not post pictures, but rather try to create

Sql - goto statement

邮差的信 提交于 2020-06-27 06:59:11
问题 Is it good practice to use 'goto' statements in SQL queries? 回答1: Depends on the SQL - some of the dialects don't provide a useful mechanism for flow control other than GOTO. GOTO is generally bad form. 回答2: Not in production code but for testing could be ok. For example, wanting to provide regression testing for a stored procedure where the "common bit" is the call to the procedure being tested and debug statements. declare @test int; set @test = 1; goto tests common: print "common bit"

Join together multiple columns split by a character in SQL

时光毁灭记忆、已成空白 提交于 2020-06-26 13:00:19
问题 using mssql, if i have data such as: cols: id, name, list1, list2 1, 'first', '10;15;30;50', '25;12;15;18' 2, 'second', '50;30;15;10, '12;25;11;15' ... 10,'tenth', '9;2;15;1', '5;13;17;45' im trying to create rows of results that join each of those list columns together, such as 1, 'first', 10, 25 1, 'first', 15, 12 1, 'first', 30, 15 1, 'first', 50, 18 2, 'second', 50, 12 2, 'second', 30, 25 2, 'second', 15, 11 2, 'second', 10, 15 ... 10, 'tenth', 9, 5 10, 'tenth', 2, 13 10, 'tenth', 15, 17

Join together multiple columns split by a character in SQL

ε祈祈猫儿з 提交于 2020-06-26 12:59:29
问题 using mssql, if i have data such as: cols: id, name, list1, list2 1, 'first', '10;15;30;50', '25;12;15;18' 2, 'second', '50;30;15;10, '12;25;11;15' ... 10,'tenth', '9;2;15;1', '5;13;17;45' im trying to create rows of results that join each of those list columns together, such as 1, 'first', 10, 25 1, 'first', 15, 12 1, 'first', 30, 15 1, 'first', 50, 18 2, 'second', 50, 12 2, 'second', 30, 25 2, 'second', 15, 11 2, 'second', 10, 15 ... 10, 'tenth', 9, 5 10, 'tenth', 2, 13 10, 'tenth', 15, 17

Parse JSON Array in T-SQL

六月ゝ 毕业季﹏ 提交于 2020-06-26 06:39:23
问题 In our SQL Server table we have a json object stored with an array of strings. I want to programatically split that string into several columns. However, I cannot seem to get it to work or even if it's possible. Is this a possibility to create multiple columns within the WITH clause or it is a smarter move to do it within the select statement? I trimmed down some of the code to give a simplistic idea of what's given. The example JSON is similar to { "arr": ["str1 - str2"] } SELECT b.* FROM

Parse JSON Array in T-SQL

余生颓废 提交于 2020-06-26 06:39:11
问题 In our SQL Server table we have a json object stored with an array of strings. I want to programatically split that string into several columns. However, I cannot seem to get it to work or even if it's possible. Is this a possibility to create multiple columns within the WITH clause or it is a smarter move to do it within the select statement? I trimmed down some of the code to give a simplistic idea of what's given. The example JSON is similar to { "arr": ["str1 - str2"] } SELECT b.* FROM

How to use CDATA in SQL XML

老子叫甜甜 提交于 2020-06-25 08:13:06
问题 Could someone please help me with an XML output template please. I have been requested by a client to create an xml output file. This file will then feed in to Client's CRM. So that's why, it has to be an exact match of Client's requested template. I have managed to match it perfectly apart from CDATA for couple of fields. below is the query you can use for testing purposes. I need CDATA wrap for fields Client and Area. I have also attached the output i am getting by running below code If

How to use CDATA in SQL XML

谁都会走 提交于 2020-06-25 08:12:30
问题 Could someone please help me with an XML output template please. I have been requested by a client to create an xml output file. This file will then feed in to Client's CRM. So that's why, it has to be an exact match of Client's requested template. I have managed to match it perfectly apart from CDATA for couple of fields. below is the query you can use for testing purposes. I need CDATA wrap for fields Client and Area. I have also attached the output i am getting by running below code If

t-sql loop through all rows and sum amount from column until value is reached

和自甴很熟 提交于 2020-06-23 12:50:47
问题 I have a table containing the below test data: I now would like to fill a restaurant with 12 seating spaces. This should result in: Basically, I need to loop from top to bottom through all rows and add the AmountPersons until I have filled the restaurant. In this example: (first few rows: AmountPersons) 3+1+2+4 = 10 UserId 52 can't be added because they reserved for 3 persons, which would result in 13 occupied places and there are only 12 available. In the next row it notices a reservation