user1027620
user1027620

Reputation: 2785

Looking for a way to store a table (or any other type) of data on the server that never expires

I'm not sure how to do the this. But I need to store data into two columns on the server, where column 1 has a static id and column 2 has a dynamic id that will change each time a client connects to the website.

The reason for this is to keep track of the dynamic id.

What should I look for exactly? I don't even know why my query on Google should be, and what is the best way of doing this, if possible at all.

Upvotes: 0

Views: 51

Answers (1)

user596075
user596075

Reputation:

This shouldn't be too hard to implement. You can use an RDBMS to store the persistent data. I'm not sure if you already have one, but I'd recommend SQL Server.

Have a table like this:

create table UserId
(
    UserIdStatic int identity(1, 1) not null, -- or whatever you want to use for a static id
    UserIdDynamic int not null
)
go

Then you can create a function to get a random number that isn't already taken in your UserId table. This would work:

create view RandomNumGen
as
    select cast(rand() * 1000000 as int) as RandomNumber
go

create function RandUniqueId ()
returns int
as
begin
    declare @randomNumber int

    while (1 = 1)
    begin
        select @randomNumber = RandomNumber
        from RandomNumGen
        if not exists
        (
            select *
            from UserId
            where UserIdDynamic = @randomNumber
        )
        begin
            return @randomNumber
        end
        else
        begin
            continue
        end
    end

    return @randomNumber
end

(Note: you need to create the view because you can't call RAND() in a UDF)

Then when your user logs in, just generate a new random number and set it as their dynamic ID.

Upvotes: 1

Related Questions