2017-05-22 76 views
2

我是SQL中的新手。請多多包涵。謝謝SQL插入到選擇自動增量變量

我想插入記錄到表名t1其中 這些記錄的一些字段來自臨時表@t2

create table t1 
(
    transid    varchar(15) not null primary key, 
    content    varchar(1000), 
    userid    int not null, 
    dtcreate   datetime not null, 
) 

DECLARE @seqno INT = 1; 

DECLARE @t2 TABLE (userid INT, content VARCHAR(1000), dtcreate DATETIME) 

比方說,我有10個記錄@t2

我要插入到T1爲@ T2的所有記錄(臨時表)

INSERT INTO t1 (transid,content,userid,dtcreate) 
SELECT (CONVERT(VARCHAR(10), dtcreate, 112)+RIGHT('0000'+CONVERT(VARCHAR, ( 
@seqno =+ 1)), 5)) , content, userid, dtcreate FROM @t2 

這就是@seqno =+ 1不允許我這樣做。如果我想要這樣的東西,我該怎麼做?多謝你們。我是一個超級新手。

+0

創建一個序列,並在插入語句中選擇下一個值以生成增量值。 – Galcoholic

+0

提供表格定義的好工作,如果有INSERT樣本記錄,則更好。 – KtX2SkD

回答

0

首先在你創建表這樣

CREATE TABLE t1 (
    transid INT NOT NULL AUTO_INCREMENT, 
    content Varchar(1000), 
    userid INT Not Null, 
    dtcreate DateTime not null, 
    PRIMARY KEY (Transid) 
) 
AUTO_INCREMENT = 1; 

以上查詢給你你的表
自動增量比使用這樣的查詢

INSERT INTO t1 (transid,content,userid,dtcreate) 
SELECT transid,content,userid,dtcreate 
FROM @t2 
0

使用ROW_NUMBER()函數代替@ seqno

INSERT INTO #t1 (transid,content,userid,dtcreate) 
SELECT (CONVERT(VARCHAR(10), dtcreate, 112)+RIGHT('0000'+CONVERT(VARCHAR, ( 
ROW_NUMBER()over(order by (Select 1)))), 5)) , content, userid, dtcreate FROM @t2 
0

似乎像ROW_NUMBER可以做到這一點。

一個抽象的例子:

DECLARE @T1 TABLE (ID INT) 
DECLARE @T2 TABLE (ID INT, VALUE INT) 
INSERT INTO @T1 VALUES (1), (4) 
INSERT INTO @T2 SELECT *, (ROW_NUMBER() OVER(ORDER BY ID))+100 FROM @T1 
SELECT * FROM @T1 
SELECT * FROM @T2 

應用,爲您的代碼:

declare @t1 table 
(
    transid    varchar(15) not null primary key, 
    content    varchar(1000), 
    userid    int not null, 
    dtcreate   datetime not null 
) 

DECLARE @t2 TABLE (userid INT, content VARCHAR(1000), dtcreate DATETIME) 

INSERT INTO @t1 (transid,content,userid,dtcreate) 
SELECT (CONVERT(VARCHAR(10), dtcreate, 112)+RIGHT('0000'+CONVERT(VARCHAR, ( 
row_number() over(order by userid))+100), 5)) , content, userid, dtcreate FROM @t2 

一個問題我承認,是不能省略ORDER BY,希望這不是一個問題。

0
DECLARE @seqno INT = 1; 

DECLARE @t2 TABLE (userid INT, content VARCHAR(1000), dtcreate DATETIME) 
INSERT INTO @t2 (content, userid, dtcreate) 
SELECT 'AA',1,'2017-01-05' 
SELECT @seqno = ROW_NUMBER()OVER(Order by transid)+1 From t1 

--SELECT @seqno 

INSERT INTO t1 (transid,content,userid,dtcreate) 
SELECT (CONVERT(VARCHAR(10), GETDATE(), 112)+RIGHT('0000'+CONVERT(VARCHAR(50), (@seqno)), 5)) As seqno 
, content, userid, dtcreate FROM @t2 

SELECT * from t1 

下面是輸出

transid   content userid dtcreate 
------------------------------------------------------- 
2017052200001 AA  1  2017-01-05 00:00:00.000 
2017052200002 AA  1  2017-01-05 00:00:00.000 
2017052200003 AA  1  2017-01-05 00:00:00.000 
2017052200004 AA  1  2017-01-05 00:00:00.000 
0

使用ROW_NUMBER

例如:

  DECLARE @t2 TABLE 
       (
        content VARCHAR(10) , 
        userid INT , 
        dtcreate DATETIME 
       ) 


      INSERT INTO @t2 
        (content, userid, dtcreate) 
      VALUES ('A', 20, '2017.01.01'), 
        ('B', 21, '2017.01.02'), 
        ('C', 22, '2017.01.03'), 
        ('D', 23, '2017.01.04') 

      ; 
    WITH CTE 
       AS (SELECT ctr , 
          content , 
          userid , 
          dtcreate 
        FROM  (SELECT ROW_NUMBER() OVER (ORDER BY userid ASC) ctr , 
             content , 
             userid , 
             dtcreate 
           FROM  @t2 
          ) T 
       ) 
     ---INSERT INTO t1 
     ---  (transid , 
     ---  content , 
     ---  userid , 
     ---  dtcreate 
     --- ) 
       SELECT (CONVERT(VARCHAR(10), dtcreate, 112) + RIGHT('0000' 
                    + CONVERT(VARCHAR, ctr), 
                    5)) transid , 
         content , 
         userid , 
         dtcreate 
       FROM CTE 

結果:

 transid   content userid  dtcreate 
     --------------- ---------- ----------- ----------------------- 
     2017010100001 A   20   2017-01-01 00:00:00.000 
     2017010200002 B   21   2017-01-02 00:00:00.000 
     2017010300003 C   22   2017-01-03 00:00:00.000 
     2017010400004 D   23   2017-01-04 00:00:00.000