Created
June 4, 2014 03:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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