sql-server-2012

Get previous row updated value using LAG Without using Recursive CTE

∥☆過路亽.° 提交于 2019-12-06 03:22:10
How to use LAG function to get the updated previous row value (without using Recursive CTE ). Please check the screenshot for sample output Query Tried Declare @Tbl as Table(SNO Int,Credit Money,Debit Money,PaidDate Date) Insert into @Tbl SELECT * FROM (VALUES (1,0,12,'7Jan16'), (2,10,0,'6Jan16'), (3,15,0,'5Jan16'), (4,0,5,'4Jan16'), (5,0,3,'3Jan16'), (6,0,2,'2Jan16'), (7,20,0,'1Jan16')) AS X(SNO,Credit,Debit,PaidDate) Select T.SNO, T.Credit, T.Debit, TotalDebit = Case When Credit < LAG(T.Debit, 1, 0) OVER (ORDER BY SNO) Then Debit + (LAG(T.Debit, 1, 0) OVER (ORDER BY SNO)-Credit) Else Debit

SQLCLR Function and System.Runtime.Serialization In GAC

梦想与她 提交于 2019-12-06 03:20:37
问题 I've built a SQLCLR function in C# that will deserialize JSON and return a table. The problem I have is getting the correct assemblies within SQL Server 2012. In order to utilize Newtonsoft's deserializer I've had to add the following assemblies to SQL Server: System.ServiceModel.Internals.dll SMDiagnostics.dll System.Runtime.Serialization.dll Newtonsoft.Json.dll This has all gone as planned but when I try to run my function I get the following error: System.IO.FileLoadException: Could not

How to calculate a IPv6 CIDR route prefix in SQL?

有些话、适合烂在心里 提交于 2019-12-06 03:14:41
I've been working on generating a CIDR from ranges of IPv4 and IPv6 addresses using T-SQL running on a SQL Server 2012 instance. Generally our application (outside of the db) is responsible for calculating a CIDR, but I presently have need to do the CIDR calculation in the database. Because IPv6 is too large to store in the bigint datatype we store IP addresses as either binary(4) or binary(16) . Calculating the route prefix for an IPv4 range is relatively simple if a little ugly: declare @ipv4_begin binary(4) ,@ipv4_end binary(4) set @ipv4_begin = 0xC0A80000 -- '192.168.000.000' set @ipv4_end

Identify sub-set of records based on date and rules in SQL Server

怎甘沉沦 提交于 2019-12-06 02:41:00
I have a dataset that looks like this: I need to identify the rows that have Linked set to 1 but ONLY where they are together when sorted by ToDate descending as in the picture. In other words I want to be able to identify these records ( EDITED ): This is a simplified dataset, in fact there will be many more records... The logic that defines whether a record is linked is if the FromDate of a record is within 8 weeks of the ToDate of the preceeding date...but this is testData so may not be perfect What's the best way to do that please? sagi You can use LAG() and LEAD() analytic functions:

How to insert record into a sql server express database table?

放肆的年华 提交于 2019-12-06 02:16:25
I'm trying to insert a textbox value to a database table called site_list . The site_list table contains two columns id and site_name , id set to auto increment This is the code I'm trying and when it execute there is no error, but the data is not showing up in the table SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=.\\SQLExpress;" + "User Instance=true;" + "Integrated Security=true;" + "AttachDbFilename=|DataDirectory|scraper_db.mdf;"; SqlCommand addSite = new SqlCommand("INSERT INTO site_list (site_name) " + " VALUES (@site_name)", conn); addSite.Parameters

Using SQL Server 2012 LAG

馋奶兔 提交于 2019-12-06 01:59:45
I am trying to write a query using SQL Server 2012 LAG function to retrieve data from my [Order] table where the datetime difference between a row and the previous row is less than equal to 2 minutes. The result I'm expecting is 1234 April, 28 2012 09:00:00 1234 April, 28 2012 09:01:00 1234 April, 28 2012 09:03:00 5678 April, 28 2012 09:40:00 5678 April, 28 2012 09:42:00 5678 April, 28 2012 09:44:00 but I'm seeing 1234 April, 28 2012 09:00:00 1234 April, 28 2012 09:01:00 1234 April, 28 2012 09:03:00 5678 April, 28 2012 09:40:00 5678 April, 28 2012 09:42:00 5678 April, 28 2012 09:44:00 91011

How to update inserted field in trigger

血红的双手。 提交于 2019-12-06 01:40:46
Ok, my situation is like this: I have a table named Company, and I want to add a trigger after every record I add in this table that checks if the column Name does not end with 'LTD' then add 'LTD' at the end of the Name. I get an error saying Incorrect syntax near ')'. How would I do this? Create Trigger [Add_LTD] on Company After Insert As Update Company Set Name = Name + ' LTD' If Exists (Select Name From Inserted Where Name Not Like '% LTD') You would need something like this: CREATE TRIGGER [Add_LTD] on dbo.Company AFTER INSERT AS UPDATE dbo.Company SET Name = Name + ' LTD' FROM Inserted

Select MAX dates plus ID value

China☆狼群 提交于 2019-12-06 00:29:22
Please consider the following table... DECLARE @tmp TABLE ( ID int, userID int, testID int, someDate datetime ) ...containing the following values: INSERT INTO @tmp (ID, userID, testID, someDate) VALUES (1, 1, 50, '2010-10-01') INSERT INTO @tmp (ID, userID, testID, someDate) VALUES (2, 1, 50, '2010-11-01') INSERT INTO @tmp (ID, userID, testID, someDate) VALUES (3, 1, 50, '2010-12-01') INSERT INTO @tmp (ID, userID, testID, someDate) VALUES (4, 2, 20, '2010-10-01') INSERT INTO @tmp (ID, userID, testID, someDate) VALUES (5, 2, 30, '2010-11-01') INSERT INTO @tmp (ID, userID, testID, someDate)

Deadlock on 2 pages

依然范特西╮ 提交于 2019-12-05 22:48:46
I'm troubleshooting some deadlocks I'm seeing in a production environment and I'm new to this but something seemed odd to me. So I have the deadlock graph below: The right side of the deadlock is an update that is the following: UPDATE order_sub_line SET sub_line_status = 300 WHERE order_sub_line_id = '75C387EC-A1A7-4587-9FA0-DD33A49009BC' It looks to me in the graph that this update is trying to acquire 2 page locks. order_sub_line_id is a clustered index. Should this be trying to acquire 2 page locks and if so why? Additional Information: The deadlock victim is a view (joins a few other

SSAS Dimension attribute as Calculated Measure

不羁的心 提交于 2019-12-05 22:33:29
I am having some issues trying to implement an average of a dimension attribute. The basic structure is: Booking Header Dimension Fact Table (multiple rows per Booking Header entry) On the booking header dimension I have a numerical attribute called Booking Window, and I want to be able to create a calculated measure that averages this value. We are using SQL Server 2012 standard edition. Any help would be greatly appreciated. The best approach would be to create a measure group from the dimension table (in BIDS, go to cube designer, tab "Cube Structure", right-click the cube object in the