五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > The typical scenarios of using Insert in T-sql

The typical scenarios of using Insert in T-sql

文章來源:365jz.com     點擊數(shù):211    更新時間:2009-09-20 08:58   參與評論

1.The most common one
INSERT INTO V1 (col1, col2)  VALUES ('Row 1',1);

2.Sometimes you just need to insert a default row
INSERT INTO T1 DEFAULT VALUES;

3.What if you have a Identity column, and you still want to insert a row with you own value of that column?
CREATE TABLE dbo.T1 ( column_1 int IDENTITY, column_2 VARCHAR(30));
GO
SET IDENTITY_INSERT T1 ON;
GO
INSERT INTO T1 (column_1,column_2)  VALUES (-99, 'Explicit identity value');

4.What if the table that you want to insert has a uniqueidentifier column?
CREATE TABLE dbo.T1
(
    column_1 int IDENTITY,
    column_2 uniqueidentifier,
);
GO
INSERT INTO dbo.T1 (column_2)  VALUES (NEWID());

------------------------------------------------------------------------------------------
-- above are all the use of "INSERT INTO". Now let try the "INSERT...SELECT"
------------------------------------------------------------------------------------------

5.most common "insert... select"
insert T1
select col1, col2 from T2

6.like "select into", you can only set value for part of the columns
insert test12(ItemID)
select ItemID
from test11 where ItemID=-6188872580015720368

7.you can use EXEC if you want, to run a sp or string
insert T1 exec('select col1, col2 from T2') -- run a query string
insert T1 exec sp_YouSPName  -- run a sp

8.two ways of top insert. I prefer the second one, and you?
insert top (10) into test12 select * from test11
insert test12 select top 10 * from test11

-----------------------------------------------------------------------------
--For above senarios, you all need to create a table before you insert rows into it
--now let's see something different
--------------------------------------------------------------------------------
9.quick copy
select * into T2 from T1  -- you don't need to create T2 before it

10.quick create a empty table using the same schema with a existed table
select * into T2 from T1 where 1=0

--------------------------------------------------------------------------
--What if we want to get some thing out of the new-inserted rows?
--Like the value of a indentify column, or the value of some computed column?
--------------------------------------------------------------------------------
11. A simple example of 'Output'
--ScrapReasonID is a IDENTITY column in Table 'ScrapReason'
DECLARE @MyTableVar table( ScrapReasonID smallint,
                           Name varchar(50),
                           ModifiedDate datetime);
GO
INSERT Production.ScrapReason
    OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate
        INTO @MyTableVar

VALUES (N'Operator error', GETDATE());

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答!! 點擊進入論壇

發(fā)表評論 (211人查看,0條評論)
請自覺遵守互聯(lián)網(wǎng)相關的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學習

業(yè)務咨詢

· 技術(shù)支持
· 服務時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號