Skip to content

Instantly share code, notes, and snippets.

@3leftturns
Created June 4, 2014 03:19
Show Gist options
  • Save 3leftturns/83ec0b1a500640ff888e to your computer and use it in GitHub Desktop.
Save 3leftturns/83ec0b1a500640ff888e to your computer and use it in GitHub Desktop.
SQL: A very painful way to get around using the IDENTITY constraint to create a unique primary key. Lesson learned: use IDENTITY when designing the table that needs an autogenerated unique integer PK.
BEGIN TRANSACTION
--Declare variable to hold max primary key value
DECLARE @priKey int =
(SELECT MAX(PK)
FROM normalSales) + 1
--INSERT values into table
INSERT INTO normalSales(PK, saleDate, saleAmount, city, saleQuantity, saleTotal)
VALUES (
@priKey,
GETDATE(),
21000,
'Salt Lake City',
1,
21000
)
COMMIT TRANSACTION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment