获取INSERT语句的字段的MAX值

时间:2011-07-22 20:33:32

标签: sql

我正在尝试在小设置表中插入新记录,我想获取DisplayOrder字段的MAX值并向其添加10。我在插入值中出现MAX函数错误。

INSERT INTO tMrMenu
       ([ParentId]
       ,[DisplayOrder]
       ,[ItemName]
       ,[ItemDescription]
       ,[ItemURL]
       ,[ItemImage]
       ,[CreateDate]
       ,[CreateUser]
       ,[LastUpdateDate]
       ,[LastUpdateUser]
       ,[module]
       ,[isactive])
 SELECT
        ( 1
        , (SELECT MAX(DisplayOrder) + 10 FROM tMrMenu)
        , 'EDM Summary Text'
        , 'EDM Summary Text'
        , '/Offline/Reports/EdmSummaryText'
        , 'cli.gif'
        , GETDATE()
        , 'Garry.Bargsley'
        , GETDATE()
        , 'Garry.Bargsley'
        , 'MR'
        , 1)

3 个答案:

答案 0 :(得分:1)

你有额外的括号:

INSERT INTO tMrMenu
       ([ParentId]
       ,[DisplayOrder]
       ,[ItemName]
       ,[ItemDescription]
       ,[ItemURL]
       ,[ItemImage]
       ,[CreateDate]
       ,[CreateUser]
       ,[LastUpdateDate]
       ,[LastUpdateUser]
       ,[module]
       ,[isactive])
 SELECT
        1
        , (SELECT MAX(DisplayOrder) + 10 FROM tMrMenu)
        , 'EDM Summary Text'
        , 'EDM Summary Text'
        , '/Offline/Reports/EdmSummaryText'
        , 'cli.gif'
        , GETDATE()
        , 'Garry.Bargsley'
        , GETDATE()
        , 'Garry.Bargsley'
        , 'MR'
        , 1

答案 1 :(得分:0)

Declare @max int
SET @max = (SELECT MAX...)

INSERT INTO tMrMenu...
SELECT
...
@max,
...

答案 2 :(得分:0)

INSERT INTO tMrMenu
       ([ParentId]
       ,[DisplayOrder]
       ,[ItemName]
       ,[ItemDescription]
       ,[ItemURL]
       ,[ItemImage]
       ,[CreateDate]
       ,[CreateUser]
       ,[LastUpdateDate]
       ,[LastUpdateUser]
       ,[module]
       ,[isactive])
SELECT MAX(DisplayOrder) + 10 
        , 'EDM Summary Text'
        , 'EDM Summary Text'
        , '/Offline/Reports/EdmSummaryText'
        , 'cli.gif'
        , GETDATE()
        , 'Garry.Bargsley'
        , GETDATE()
        , 'Garry.Bargsley'
        , 'MR'
        , 1
FROM tMrMenu