将列插入表中并逐行递增

时间:2015-07-25 11:05:17

标签: sql sql-server insert max

鉴于以下表格,我试图将eventid设置为最高值,加1。所以max(eventid) + 1。我似乎无法获得正确的SQL语法来实现这一目标。

我现在所拥有的有效但不能满足我需要的是以下内容。如何显示eventid,在本例中为96740?

INSERT INTO stock_history
            (lastmodby,
             event,
             previous_stock,
             new_stock,
             lastmodified,
             productid)
SELECT '160'                     AS lastmodby,
       'SALE'                    AS event,
       stockstatus               AS previous_stock,
       stockstatus + 1           AS new_stock,
       Getdate()                 AS lastmodified,
       products_joined.productid AS productid
FROM   products_joined
WHERE  productcode = 'abc' 

stock_history表

+--------+-----------+----------------+-------+-----------+-------+---------+-----------------------+-----------+
|   id   | productid | previous_stock | count | new_stock | event | eventid |     lastmodified      | lastmodby |
+--------+-----------+----------------+-------+-----------+-------+---------+-----------------------+-----------+
| 105619 |      9282 |              9 |     1 |        10 | SALE  |         | 7/24/2015 5:29:00 PM  |       160 |
| 105578 |      9282 |              8 |     1 |         9 | ORDER |   96739 | 7/23/2015 7:30:00 PM  |     37655 |
|  89241 |      9282 |              7 |     1 |         8 | ORDER |   96738 | 6/1/2014 6:06:00 PM   |     30761 |
|  86773 |      9282 |              6 |     1 |         7 | ORDER |   96737 | 4/12/2014 4:36:00 PM  |     29745 |
|  70419 |      9282 |              5 |     1 |         6 | ORDER |   96736 | 5/21/2013 1:17:00 PM  |      1754 |
|  69088 |      9200 |             19 |     1 |        20 | EDIT  |   96735 | 4/28/2013 10:26:00 AM |      1754 |
|  69050 |      9200 |             18 |     1 |        19 | ORDER |   96734 | 4/27/2013 2:17:00 PM  |     23001 |
|  68127 |      9200 |             17 |     1 |        18 | ORDER |   96733 | 4/13/2013 12:34:00 PM |     22674 |
|  67064 |      9200 |             16 |     1 |        17 | ORDER |   96732 | 3/30/2013 9:23:00 AM  |     22327 |
+--------+-----------+----------------+-------+-----------+-------+---------+-----------------------+-----------+

products_joined table

+-------------+-----------+-------------+
| productcode | productid | stockstatus |
+-------------+-----------+-------------+
| abc         |      9282 |           9 |
| xyz         |      9200 |          19 |
+-------------+-----------+-------------+

2 个答案:

答案 0 :(得分:2)

您最好使用IDENTITY列 - 在性能和可靠性方面,SQL Server将比您更有效地处理更多This question为您提供了一些选项,例如

ALTER TABLE stock_history DROP COLUMN eventid
ALTER TABLE stock_history ADD eventid INT IDENTITY(1,1)

也就是说,在技术上可以在查询中执行此操作。对于IDENTITY列不是选项的情况,可能有助于了解此模式。

INSERT INTO stock_history
            (lastmodby,
             event,
             previous_stock,
             new_stock,
             lastmodified,
             productid,
             eventid)
SELECT '160'                     AS lastmodby,
       'SALE'                    AS event,
       stockstatus               AS previous_stock,
       stockstatus + 1           AS new_stock,
       Getdate()                 AS lastmodified,
       products_joined.productid AS productid,
       (SELECT MAX(eventid) + 1 FROM stock_history) AS eventid
FROM   products_joined
WHERE  productcode = 'abc'

请注意,如果此查询一次执行多次,则可能导致重复eventid

答案 1 :(得分:1)

这取决于您使用的SQL:SQL,MySQL,Access。 此链接可能对您有所帮助,它涵盖了所有语法:

http://www.w3schools.com/sql/sql_autoincrement.asp