sql-server-2016

Max dates for each sequence within partitions

雨燕双飞 提交于 2019-12-24 18:53:25
问题 I would like to see if somebody has an idea how to get the max and min dates within each 'id' using the 'row_num' column as an indicator when the sequence starts/ends in SQL Server 2016. The screenshot below shows the desired output in columns 'min_date' and 'max_date'. Any help would be appreciated. 回答1: Try something like SELECT Q1.id, Q1.cat, MIN(Q1.date) AS min_dat, MAX(Q1.date) AS max_dat FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY id, cat ORDER BY [date]) AS r1, ROW_NUMBER() OVER

How to get XML subnodes as strings along with parent attributes?

你说的曾经没有我的故事 提交于 2019-12-24 16:50:25
问题 I need to parse xml which consist of nodes having attributes and subnodes. The result should be attribute of parent with xml of child node declare @xml xml set @xml = '<root> <group Description="firstgroup"> <nodeA age="10" birthplace="Anchorage"/> <nodeB mode="A" ability="read"/> </group> <group Description="nextgroup"> <nodeA age="10" birthplace="London"/> <nodeB count="2" birthplace="Paris"/> </group> </root>' select c.value('@Description', 'varchar(max)') as 'Description' from @xml.nodes(

Find next record where status field is different from current

前提是你 提交于 2019-12-24 11:25:21
问题 I have a table that is used to log events. Two types specifically : ON and OFF. There are sometimes overlapping log entries as there can be 2 simultaneous devices logging. This is not crucial, as the end report should give a [mostly] correct overview of ON -> OFF periods. Below is a sample, with the 3rd column just for illustration: It does not exist. ActionTaken ID ID_of_next_OFF Switched ON 1 3 Switched ON 2 6 Switched OFF 3 Switched ON 4 7 Switched ON 5 8 Switched OFF 6 Switched OFF 7

Deploy Database with SqlPackage.exe or Microsoft.SqlServer.Dac.dll, any difference or benefits?

和自甴很熟 提交于 2019-12-24 10:09:01
问题 What is the difference between using Sqlpackage.exe and Microsoft.SqlServer.Dac.dll to deploy a database? Is there any benefits? We are just trying to deploy a database to localhost. Just curious if Sqlpackage exe has more options/functionalities than Microsoft.SqlServer.Dac.dll, or does it really matter at this point? SQL Package: SqlPackage.exe /Action:Publish /SourceFile:"TestDatabase.dacpac" /TargetServerName:"localhost" /TargetDatabaseName:TestDatabase Microsoft.SqlServer.Dac.dll add

Use Entity Framework in CLR Stored procedure

我与影子孤独终老i 提交于 2019-12-24 09:28:02
问题 I found a post from seven or so years ago that the Entity Framework could not be used in a CLR stored procedure. Has this been rectified in the past seven or so years? Is an update available that will allow the Entity Framework to work in a CLR stored procedure? 回答1: The normal reason you don't do this is that if you did , you would have to install all of .NET Framework assemblies that EF depends on into the database as unsafe assemblies, and you would have to update them every time the .NET

Can't connect to Localdb but can using namedpipe

China☆狼群 提交于 2019-12-24 09:17:58
问题 I am really suck with connecting my app to a database. I am trying to connect to a database using (localdb)\MSSQLLocalDB in the connection string and I get this error: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error

Getting max post_year group by email with num of years

最后都变了- 提交于 2019-12-24 07:56:02
问题 Given the table below Email Post_Year country ====== ========== ======= a@a.com 2006 US a@a.com 2007 US a@a.com 2008 HK1 a@a.com 2008 HK b@b.com 2009 CN b@b.com 2010 SW I want to have all columns with max Post_year group by email, if there are multiple max Post_year, just choose one as well as the num_of_yrs which is max(post_year)-min(post_year) of that particular email. Email Post_Year country Num_Of_Yrs ====== ========== ======= ============= a@a.com 2008 HK 2 [which is 2008-2006] b@b.com

Should you use master.dbo when accessing sp_ procedures?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 07:38:58
问题 I'm 100% convinced this is a duplicate but after more than an hour of searching I just can't seem to find the answer. When using special procedures (i.e. the sp_ ones like sp_executesql ), is it wise to use the full 3-part identifier master.dbo (or master.. ) or just use them as is? I'm looking for the most performance optimized version of this: 1. sp_executesql 2. master..sp_executesql 3. master.dbo.sp_executesql Are 2 and 3 identical in terms of performance specifically regarding the above

UPDATE query on field that has dynamic data mask

﹥>﹥吖頭↗ 提交于 2019-12-24 07:16:30
问题 I have this situation: A table in SQL Server 2016 has Dynamic Data masks configured on one column. I read one whole row into a front-end and display the values, including the masked field that returns value 'xxxx' To update the table, the application reads the values from the user front-end and generates an UPDATE SQL statement, that includes the 'xxxx' value for the masked field. SQL Server 2016 overwrites the existing value of the masked field with the value 'xxxx'. Is there a way to

Preventing Conditional INSERT/UPDATE Race Condition in MS-SQL

青春壹個敷衍的年華 提交于 2019-12-24 06:06:12
问题 I wonder that do i follow correct approach and need your help to figure out Here my non-protected query DECLARE @cl_WordId bigint = NULL SELECT @cl_WordId = cl_WordId FROM tblWords WHERE cl_Word = @cl_Word AND cl_WordLangCode = @cl_WordLangCode IF (@cl_WordId IS NULL) BEGIN INSERT INTO tblWords (cl_Word, cl_WordLangCode, cl_SourceId) VALUES (@cl_Word, @cl_WordLangCode, @cl_SourceId) SET @cl_WordId = SCOPE_IDENTITY() SELECT @cl_WordId END ELSE BEGIN SELECT @cl_WordId END And to protect it, i