Add a uniqueidentifier column to the table with the default value NEWID (). This ensures that each column receives a new unique identifier that is not incremental.
CREATE TABLE MyTable ( ... PIN uniqueidentifier NOT NULL DEFAULT newid() ... )
The unique identifier is guaranteed to be unique not only for this table, but for all tables.
If it is too large for your application, you can get a smaller PIN from this number, you can do it as follows:
SELECT RIGHT(REPLACE((SELECT PIN from MyTable WHERE UserID=...), '-', ''), 4)
Please note that the returned smaller PIN is not guaranteed to be unique to all users, but may be more manageable depending on your application.
EDIT: if you need a small PIN code with guaranteed uniqueness, the difficult part is that you need to know at least the maximum number of users to select the appropriate pin size. As the number of users increases, the likelihood of an IDU collision increases. This is similar to the Coupon Collector problem and is suitable for n log n complexity, which will lead to very slow inserts (the insertion time is proportional to the number of existing elements, so inserting M elements then becomes O (N ^ 2)). The easiest way to avoid this is to use a large unique identifier and select only part of it for your PIN code, assuming that you can refuse the uniqueness of PIN values.
EDIT2:
If you have a table definition like this
CREATE TABLE YourTable ( [id] [int] IDENTITY(1,1) NOT NULL, [pin] AS (CONVERT(varchar(9),id,0)+RIGHT(pinseed,3)) PERSISTED, [pinseed] [uniqueidentifier] NOT NULL )
This will create a pin from the pinseed unique identifier and line identifier. (RAND does not work - since the SQL server will use the same value to initialize multiple rows, this does not apply to NEWID ())
Just to be said, I advise you not to consider it in any way safe. You should keep in mind that it is always possible that another user can guess someone else's PIN code if you do not limit the number of guesses allowed in any way (for example, stop accepting requests after 3 attempts, similar to the bank that placed your card after 3 incorrect PINs -records.)