SQL插入到选择自动增量变量中

时间:2017-05-22 07:06:37

标签: mysql sql sql-server tsql

我是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)

假设我在@t2

中有10条记录

我想在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不允许我这样做的地方。如果我想要这样的东西怎么办?多谢你们。我是一个超级新手。

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用row_number()函数代替@seqno

//          SONIC PROTOTYPE CODE         

//      Written by a complete amateur so take good care!

//  This is the code that will go in to my sonic screwdriver
//  Hardware includes Adafruit Feather 32U4, Music maker feather wing with amp Adafruit NeoPixel stick

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN A1

const int sw1 = 2;     // This sets the name of the pins
const int sw2 = 3;
const int sw3 = 5;
const int sw4 = 6;
const int sw5 = 9;
const int sw6 = 10;
const int sw7 = 11;
const int sw8 = 12;

                    struct RGB {          //for fire this could posibly reside elsewhere in the script but I'm not too sure where
                    byte r;
                    byte g;
                    byte b;
                    };

                    RGB flameColors[] = {
                    //  { 226, 121, 35},  // Orange flame   /// select what colour fire you want here 
                    //  { 158, 8, 148},   // Purple flame 
                    //  { 74, 150, 12},   // Green flame
                    //  { 226, 15, 30}    // Red flame
                        { 15,50,148}      //Blue flame
                    };
                    //  Number of flame colors
                    int NUMBER_OF_COLORS = sizeof(flameColors) / sizeof(RGB);

                    //  Tracks the current color
                    int currentColorIndex = 0;

                    //  The button pin
                    const int buttonPin = 2;

                    //  Variable for reading the pushbutton status
                    int buttonState = 0;         

                    //  Tracking if it's ok to shift colors or not
                    bool okToChangeColors = true;




                    float redStates[8];               // I added these for twinkle
                    float blueStates[8];
                    float greenStates[8];
                    float fadeRate = 0.96;           

                    int totalLEDs = 8;                // added for tardis
                    int ledFadeTime = 5;


  Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, A1, NEO_GRB + NEO_KHZ800);

    // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
    // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
    // and minimize distance between Arduino and first pixel.  Avoid connecting
    // on a live circuit...if you must, connect GND first.


    void setup() 
  {

       pinMode(sw1, INPUT_PULLUP);    // the pins for your pull low are set here
       pinMode(sw2, INPUT_PULLUP);
       pinMode(sw3, INPUT_PULLUP);
       pinMode(sw4, INPUT_PULLUP);
       pinMode(sw5, INPUT_PULLUP);
       pinMode(sw6, INPUT_PULLUP);
       pinMode(sw7, INPUT_PULLUP);
       pinMode(sw8, INPUT_PULLUP);

      strip.begin();
      strip.show(); // Initialize all pixels to 'off'
    }

    void loop() 
  {
      // Some example procedures showing how to display to the pixels:
     // if (digitalRead(sw1) == LOW) colorWipe(strip.Color(255, 0, 0), 50, sw1); // Red
     // if (digitalRead(sw7) == LOW) colorWipe(strip.Color(0, 255, 0), 50, sw7); // Green
      if (digitalRead(sw2) == LOW) skip(150, 0, 150, 250, sw2);                                                                                                                                     
      if (digitalRead(sw1) == LOW) BouncingBalls(0, 0, 255, 3, sw1);                            
      if (digitalRead(sw3) == LOW) twinkle (sw3);                                       
    //  if (digitalRead(sw3) == LOW) colorWipe(strip.Color(0, 0, 255), 50, sw3); // Blue
    //  if (digitalRead(sw4) == LOW) theaterChase(strip.Color(127, 127, 127), 50, sw4); // White
    //  if (digitalRead(sw5) == LOW) theaterChase(strip.Color(127, 0, 0), 50, sw5); // Red
     // if (digitalRead(sw6) == LOW) theaterChase(strip.Color(0, 0, 127), 50, sw6); // Blue
      if (digitalRead(sw4) == LOW) rainbow(20, sw4);
      if (digitalRead(sw5) == LOW) rainbowCycle(20, sw5);
      if (digitalRead(sw6) == LOW) fire (sw6);
      if (digitalRead(sw7) == LOW) theaterChaseRainbow(50, sw7);
      //if (digitalRead(sw8) == LOW) tardis(sw8);
                  if (digitalRead(sw8) == LOW) scanner (sw8);                                                                                     
    }

答案 2 :(得分: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,希望这不是问题。

答案 3 :(得分:0)

var url= '/Service/Grid';
$("#DivID").load(url);

以下是OutPut

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

答案 4 :(得分: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