简体   繁体   中英

How do you get a rowcount into SQL insert statements generated in Excel?

I'm generating SQL insert statements in Excel, but I've encountered a problem. The primary key of the table these statements apply to is not an auto-incrementing identity. I'm trying to find a way I can get a rowcount in Excel into the insert statements it's generating (thus effectively giving my rows an identity; or at least getting SQL Server to stop complaining upon execution).

Does anyone out there know of anything already built into Excel that will allow me to pull this off? If not, does anyone have any other ideas? Thanks in advance.

  1. Select from the table to get the max of the primary key
  2. Set the value to a variable, such as rowCount
  3. As you loop the Excel spreadsheet, increment the count by 1 and use that value for the insert statement

EDIT : Added example

DECLARE @MaxCount INT
SELECT @MaxCount = MAX(ID) FROM SomeTable

INSERT INTO (ID, ...) VALUES (@MaxCount + 1, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM