auto reset sequence number to Zero with financial year in sql 2012 - sql

I want to create a sequence number which is new feature in sql 2012,
which will auto reset to 0 when a new financial year start,
financial year for me is 01 april 2015 to 31 march 2016.
I did RND from internet i got some hint to drop the sequence no,and then again recreate it.
Is it poosible to do with sequence number ,or else have to make new logic.
please help.

Obviously the answer to this question is - no, it's not possible. You can create sequence on numeric types only. But you can set some trigger, that will reset sequence object, when particular event occurs, like:
ALTER SEQUENCE dbo.Seq
RESTART
I think you have in your system something like closing procedure of financial year? So you can reset that sequence object as part of that procedure.

Related

Query to view the date and time a row was last updated for table without having an existing trigger\mechanism?

I'm sure this is going to be a hard no, but I have a table that I need to know the last update date and time for one particular row, written in the past.
Is there a way to find this information? Maybe a system column that accompanies every row that the rdbms writes by default but not visible to users? Or do we need to create a trigger\procedure to record this information on a table-by-table basis BEFORE records are written? Googling only suggests that this is the case.
I don't have DBA access and can't get it btw.
SQL Server Management Studio v18.1
You are right, if you have not taken any steps to record this info then it is unavailable to you. – Dale K Dec 10 at 1:56

How do you write a script to auto-fill other tables when someone submits a form?

I am fairly new to SQL databases and stored procedures so I apologize in advance if this question is too general or dumb... I have made a web form using ASP.NET Core 2.2 MVC and it is connected to an Azure SQL database. The form takes in 3 inputs: annual cost [decimal (19,4)], start month [date], duration [int].
For example, if the user enters 120,000 for the annual cost, 09/01/2019 for start month, and 12 months for duration, then I would like the script to cut the annual cost by the duration and populate a table called FY19 with 10,000 for the column September, 20,000 for the column October, 30,000 for the column November, 40,000 for the column December, then go to another table named FY20 and continue filling it in until 120,000 for the column August.
My question is, should I write this script as a stored procedure? Or write it as a method in my project controller? If I write is as a stored procedure, is it possible to automate it so that every time new data gets inserted into the main table, the script fills in the FYxx tables?
Again, I apologize for making such a general question but I am kinda stuck in this problem. Any guidance would be appreciated and if anyone is willing to take an hour of their day to help me through skype I would greatly appreciate it.
You can use TRIGGERS to automatically invoke SQL script.
You can do it manually in the controller too but I think it is more convenient to do with TRIGGERS.
You can computed column on your 2nd table using the ID of your main table. Here's the link on how to add computed columns.
https://learn.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table?view=sql-server-2017

SSAS - setting a slice

I've partitioned my cube to improve processing time. But I doubt it improved my query times. I read that I have to set the slice property.
So my partition query cuts it by
year 2012 and older (ie < year 2013)
the year 2013
and year 2014 and onwards
I'm wondering how i set my slice? this don't seem to work and I get an error.
[dim_report_year] < 2013
[dim_report_year] = 2013
[dim_report_year] > 2013
the restrictions imposed on partition slice where violated
Thanks.
In the slice definition you can use tuples only. You cannot use sets or conditions. Therefore you can create a new attribute with 3 values(x<2013, x=2013, x>2013) in time dimension, then you can use this new attribute in the slice definition.

Sql Server Record Number took a huge jump [duplicate]

This question already has answers here:
Identity increment is jumping in SQL Server database
(6 answers)
Closed 7 years ago.
I have a SQL Server database table that includes a column for the record number, which increments by one for each record. The data is received from an iPhone app that our field crews use for recording vehicle mileage. On Thursday 4/10 the final record number of the day was 2997, received at about 6:00 pm. On Friday 4/11 at 8:00 am, with the first received record of the day, the increment jumped to 3997, a jump of 1,000.
Is this something I need to be concerned about? Is it an anomaly of SQL Server that occasionally happens? Or could there be something in my VB 2010 server program that is cutting loose and trying to write to the database?
As far as my system is concerned, a gap in the record numbers doesn't matter as long as the system doesn't try to go back and fill them in out of sequence.
I'm pretty sure that it can't be attributed to hacking or other malicious intent, because each transaction between the iPhone and the server program is recorded in log files and there's no indication that any activity occurred in that interval.
Any ideas about this?
Thanks!
As said, if you have an identity field on the column which you have mentioned and if there was some data that was inserted in a transaction and rolled back after that, the identity field is still set to that value.
meaning, if I have an auto incrementing field and a transaction with 10 records, my auto increment field is now at 10 and starts for 11. I rollback the transaction, but the auto incrementing number doesn't go back to 1, but it will still start at 11.
Unless and until there is something behind the screen other than straight forward inserts, this should cause no issues to you, as said.

Calculating holiday dates

I have to create "holiday" table and then create php script so I could show it on my site.
Holidays can be specific, like 15.05.2012 - 15-th of the may.
And non-specific: First(or second, third) sunday of july
Is there any way to create calculated column, so this phrase "First(or second, third) sunday of july", could turn into x.07.2012.
Use a calendar table. There is no magic code built into SQL Server that knows when Easter is. This article shows the basic premise - you fill up a table with all the dates from year x to year y, then you update a column called IsHoliday for the dates that are holidays based on specific logic (easiest to do this once, in a loop, then all your code later can refer to the calculated bit):
ASP Faq reference. The current link no longer works, this is the archive.org cached version of the page
The link in the answer now takes you to a bogus page that wants to load a virus. Just heads up.
http://codeinet.blogspot.com/2006/08/auxiliary-calendar-table-for-sql.html
This seems to be a working version.