开发者

Getting MAX Value of a Field for INSERT Statement

开发者 https://www.devze.com 2023-03-22 20:34 出处:网络
I am trying to insert a new record in a small settings table and I would like to get the MAX value of the DisplayOrder field and add 10 to it.I get errors with the MAX function in the value of the ins

I am trying to insert a new record in a small settings table and I would like to get the MAX value of the DisplayOrder field and add 10 to it. I get errors with the MAX function in the value of the insert.

INSERT INTO tMrMenu
       ([ParentId]
       ,[DisplayOrder]
       ,[ItemName]
     开发者_如何学C  ,[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)


You have extra parenthesis:

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


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

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消