SQL Server 2012-Data Update operations

Source: Internet
Author: User
Tags getdate one table

SELECT *from dbo. student;--1, inserting data insert, comma-delimited can insert multiple inserts dbo at the same time. Student (Stuid, Class, Stuname, Stuenname, Stuage, Stubirthday, Stusex, Height, Remark) VALUES (' 007 ', --Stuid-varchar (3),--class-int N ' hehe ',--stuname-nvarchar (+) ' Hehe ',--stuenname -varchar (+),--Stuage-int GETDATE (),--stubirthday-datetime N ' men ',--stusex-nvarch          AR (Ten),--height-int ' test '--Remark-text), (' 008 ',--Stuid-varchar (10)          3,--class-int N ' Hello ',--Stuname-nvarchar ' Nihao ',--Stuenname-varchar (50) ----Stuage-int GETDATE (),--stubirthday-datetime N ' female ',--Stusex-nvarchar (10) 190,- -Height-int ' test '--remark-text);--2, writes data from one table net to another table insert INTO dbo. Student SELECT ' 009 ', Class, ' Kitty ', ' Kitty ', Stuage, Stubirthday, Stusex, Height, Rema RK from dbo. Student WHERE stuid = ' 008 ';--3, OUTPUT throws a write stuiddeclare @stuId VARCHAR INSERT dbo. Student (Stuid, Class, Stuname, Stuenname, Stuage, Stubirthday, Stusex, Height, Remark) OUTPUT Inserted.          Stuidvalues (' 010 ',--Stuid-varchar (3),--Class-int N ' jingle ',--Stuname-nvarchar (50)          ' Dingdang ',--Stuenname-varchar (),--Stuage-int GETDATE (),--Stubirthday-datetime N ' Men ',--Stusex-nvarchar (Ten),--height-int ' test '--Remark-text)--4, OUTPUT cast Another physical table written out (history table) DECLARE @stuIdTable table (id INT) INSERT dbo. Student (Stuid, Class, Stuname, Stuenname, Stuage, Stubirthday, Stusex, Height, Remark) OUTPUT Inserted.  Stuid into @stuIdTable – OUTPUT into can only be written in the table values (' 012 ',--Stuid-varchar (10)        3,--class-int N ' Vanilla ',--Stuname-nvarchar (50) ' vanilla ',--Stuenname-varchar (50) ,--Stuage-int GETDATE (),--stubirthday-datetime N ' men ',--Stusex-nvarchar (10) 190, --height-int ' test '--remark-text) SELECT * from @stuIdTable--5, update data updated dbo. Student SET height=height+10 WHERE Height is not null--6, data Update according to another table: Update with two tables established relationship--join method for multiple table updates update T SET t.re Mark = ' Class 01 student ' from dbo. Student as T joins dbo. ClassInfo as CON t.class=c.id where c.id= ' 1 '--where the way to do multiple table updates update t SET T.remark = ' class 02 student ' from dbo. Student as T, dbo. ClassInfo as Cwhere t.class=c.id and c.id= ' 2 '--7, Output view data updated before and after update dbo. Student set stusex= ' female ' OUTPUT inserted.stusex,deleted.stusexwhere stuid= ' 007 '--8, the command after the SET statement in update is executed concurrently, No sequential update of dbo. Student SET stusex= ' male ', stuname=stuname+ '-' +stusexwhere stuid= ' 007 ' SELECT * FROM dbo. Student WHERE stuid= ' 007 '--two fields are exchanged for values of update dbo. Student SET StuaGe=height,height=stuagewhere stuid= ' 007 ' SELECT * FROM dbo. Student WHERE stuid= ' 007 '--9, Deleteselect * into Student_his from dbo. Student WHERE 1=2delete dbo. Student OUTPUT Deleted.stuid,deleted.class,deleted.stuname,deleted.stuenname,deleted.stubirthday,deleted.stusex, Deleted.height,deleted.remarkinto dbo. Student_his (Stuid,class,stuname,stuenname,stubirthday,stusex,height,remark) WHERE stuid= ' 007 '--10, Truncate Table = Delete tablename (without any conditions): Clears the table, resets the self-increment column, logs small, and operates faster TRUNCATE TABLE dbo. Student_his SELECT * FROM [dbo]. [Student_his]

  

SQL Server 2012-Data Update operations

Related Article

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.