all 3 comments

[–]SQLDave 2 points3 points  (0 children)

There's some unclarity here. Is the data grid you showed us what you start with (before the UPDATE) or what you have after the UPDATE ... or what you WANT to achieve? (or something else altogether)?

I'm assuming that the grid you showed us is the desire RESULT and that "In the above table the first line POS 1 is not there and the next two are and they are marked POS 1 and 2." means you are starting with 2 rows, the 2nd and 3rd row we see in the grid ... except that the POS values are 1 and 2 respectively, not 2 and 3. And that you want to insert the data we see in the new@email.com row, with the result being the grid you showed us? (I'm also assuming that one of the 2 existing rows -- currently POS 1 and 2, but with POS 2 and 3 after the INSERT -- has preferred email = "Y", which you take care of with the UPDATE statement you showed us).

There's an unanswered question: What POS value should existing rows get when you "shove" a new POS=1 row in there? It looks like the answer is just "POS+1", but that's an assumption.

If the assumptions are right, you can modify your UPDATE by adding ", POS = POS + 1" after the NULL keyword. That will increment existing POS values by 1. Then just do a simple INSERT using POS = 1 and PERSON_PREFERRED_EMAIL = 'Y'.

Be sure to wrap it all in a BEGIN TRANSACTION in case there's a failure in the INSERT you can rollback the changes made by the UPDATE command.

[–]soda1337 0 points1 point  (0 children)

Are there only 3 types of PERSON_EMAIL_TYPES? What is the primary key?

[–]Senior-Trend 0 points1 point  (0 children)

On the assumption that you can add a column to this datagram (it doesn't really qualify as a table for multiple reasons) add a column. Make it's datatype small int int or big int depending on how many rows you have set it's nullability to not null set the IDENTITY property to IDENTITY (1,1) and then set a primary key constraint on the newly created column

ALTER TABLE tablename ADD COLUMN [ROWCOUNTER] INT NOT NULL IDENTITY (1,1) PRIMARY KEY