I have a bit of a dilemma. I was tasked with creating a local temporary table that would contain faculty members first name, last name, campus, and new id number. The ID number would be a randomly generated 5 digit number. (I am using Microsoft SQL Server Management Studio)
My problem is I am new to random number generation and local temp tables. I believe most of my code is correct expect for the "random id number" I need to make. I have googled my problem only thing is there seem to be many ways to create "random" numbers and I don't understand the method behind it.
I've included my code and the database below.
My code:
SELECT
FirstName, LastName, Campus,
LEFT(CAST(CAST(CEILING(RAND() *100000000) AS bigint) AS varchar), 5) AS IDnumber
INTO
#LocalTemp1
FROM
Faculty;
SELECT * FROM #LocalTemp1
Database:
CREATE TABLE Faculty
(
Faculty_ID INT PRIMARY KEY IDENTITY,
LastName VARCHAR(20) NOT NULL,
FirstName VARCHAR(20) NOT NULL,
Department VARCHAR(10) SPARSE NULL,
Campus VARCHAR (10) SPARSE NULL
);
INSERT INTO Faculty
VALUES ('Brown', 'Joe', 'Business', 'Kent'),
('Smith', 'John', 'Economics', 'Kent'),
('Jones', 'Sally', 'English', 'South'),
('Black', 'Bill', 'Economics', 'Kent'),
('Green', 'Gene', 'Business', 'South');
CREATE TABLE Course
(
Course_ID INT PRIMARY KEY IDENTITY,
Ref_Number CHAR (5) CHECK (Ref_Number LIKE '[0-9][0-9][0-9][0-9][0-9]'),
Faculty_ID INT NOT NULL REFERENCES Faculty (Faculty_ID),
Term CHAR (1) CHECK (Term LIKE '[A-C]'),
Enrollment INT NULL DEFAULT 0 CHECK (Enrollment < 40)
)
INSERT INTO Course
VALUES ('12345', 3, 'A', 24), ('54321', 3, 'B', 18),
('13524', 1, 'B', 7), ('24653', 1, 'C', 29),
('98765', 5, 'A', 35), ('14862', 2, 'B', 14),
('96032', 1, 'C', 8), ('81256', 5, 'A', 5),
('64321', 2, 'C', 23), ('90908', 3, 'A', 38);
A source I was looking at, still need a better understanding: Generating a random & unique 8 character string using MySQL
Reposting this because a lot of things were incorrect my last one such as tags and explanations.