Database operations Change primary key, loop insert

Source: Internet
Author: User

--alter table Historydata Drop constraint pk_historydata--delete primary key
ALTER TABLE HISTORYDATA ADD constraint Pk_historydata primary KEY (Collectiontime,code)--Change primary key




Delete from Historydata where collectiontime between ' 2014-09-01 00:00:00 ' and ' 2014-11-01 00:00:00 ' and ID not in (
Select ID from (
Select Code,collectiontime,max (ID) ID from historydata
where collectiontime between ' 2014-09-01 00:00:00 ' and ' 2014-11-01 00:00:00 ' GROUP by Code,collectiontime) a)
--Delete the duplicate columns.




Loop insert
DECLARE @sDate varchar (20)
DECLARE @eDate varchar (20)
DECLARE @mindate datetime
DECLARE @maxdate datetime
DECLARE @mindateStr varchar (20)
DECLARE @maxdateStr varchar (20)


Set @sDate = ' 2015-1-16 06:30:00 '
Set @eDate = ' 2015-1-16 08:35:00 '




Set @mindate = Cast (@sDate as DateTime)
Set @maxdate = DateAdd (mi,5, @mindate)--mi represents minutes, hh hours, day days


While @maxdate <= cast (@eDate as DateTime)
Begin
Set @mindateStr = CONVERT (varchar), @mindate, 120)
Set @maxdateStr = CONVERT (varchar), @maxdate, 120)
--print (@mindateStr)
Print (@maxdateStr)

--Number of Inserts
--insert into
INSERT into historydata values (0.1337, ' 9001 ', @maxdate, ' iron ions ', 1,0.1337,0.1337, ' 2051 ', ' n ', ' ', ' 6001 ', ' n ', ' n ', ' n ', ' n ', 0, ' n ', ' n ')
Set @mindate = @maxdate
Set @maxdate = DateAdd (mi,5, @maxdate)
End

Database operations Change primary key, loop insert

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.