必须声明标量变量“@MyNewID”
Must declare the scalar variable "@MyNewID"
我试图通过使用号码池为 table 'Students' 中的每一行分配一个 UniqueID(因此 Table UNIQUE_NUMBERS)。
我不确定我的实施是否正确,但我总是收到错误。有人可以协助处理代码段吗?
我正在创建一个 Web 应用程序,其中 adds/edits/deletes 个学生在数据库 table 中。我试图确保每个用户都有一个不超过 8 个数值的唯一标识符
CREATE TABLE DBO.UNIQUE_NUMBERS
(
UNIQUE_NUMBER INT
)
INSERT INTO DBO.UNIQUE_NUMBERS (UNIQUE_NUMBER)
VALUES ('1000')
DECLARE @MyNewID VARCHAR(8);
DECLARE @NextNo INT;
UPDATE DBO.UNIQUE_NUMBERS
SET UNIQUE_NUMBER = UNIQUE_NUMBER +1, @NextNo = UNIQUE_NUMBER;
SET @MyNewID = REPLACE(STR(@NextNo,8),'','0');
INSERT INTO DBO.STUDENTS (STUDENT_ID, STUDENT_FIRSTNAME, STUDENT_SURNAME)
VALUES ('SID'+ (SELECT * FROM DBO.UNIQUE_NUMBERS) , 'Daenerys' , 'Targaryen' ),
('SID'+ @MyNewID , 'Jon' , 'Snow' ),
('SID'+ @MyNewID , 'Gregor' , 'Clegane' ),
('SID'+ @MyNewID , 'Arya' , 'Stark' ),
('SID'+ @MyNewID , 'Cersei' , 'Lannister' ),
('SID'+ @MyNewID , 'Joffrey' , 'Baratheon' ),
('SID'+ @MyNewID , 'Petyr' , 'Baelish' ),
('SID'+ @MyNewID , 'Khal' , 'Drogo' ),
('SID'+ @MyNewID , 'Theon' , 'Greyjoy' ),
('SID'+ @MyNewID , 'Ramsey' , 'Bolton' )
当我通过 Web 应用程序视图查看数据时,STUDENT_ID 应显示如下示例:
STD12345678
然后为下一个学生递增:
STD12345679
这样的事情怎么样 -
create table #temp
( STUDENT_KEY INT identity (10000000,1),
STUDENT_ID AS CONCAT('STD',STUDENT_KEY),
STUDENT_FIRSTNAME VARCHAR(255),
STUDENT_SURNAME VARCHAR(255)
)
INSERT INTO #temp
VALUES ( 'Daenerys' , 'Targaryen' ),
( 'Jon' , 'Snow' ),
( 'Gregor' , 'Clegane' ),
( 'Arya' , 'Stark' ),
( 'Cersei' , 'Lannister' ),
( 'Joffrey' , 'Baratheon' ),
( 'Petyr' , 'Baelish' ),
( 'Khal' , 'Drogo' ),
( 'Theon' , 'Greyjoy' ),
( 'Ramsey' , 'Bolton' )
SELECT * FROM #temp
我试图通过使用号码池为 table 'Students' 中的每一行分配一个 UniqueID(因此 Table UNIQUE_NUMBERS)。
我不确定我的实施是否正确,但我总是收到错误。有人可以协助处理代码段吗?
我正在创建一个 Web 应用程序,其中 adds/edits/deletes 个学生在数据库 table 中。我试图确保每个用户都有一个不超过 8 个数值的唯一标识符
CREATE TABLE DBO.UNIQUE_NUMBERS
(
UNIQUE_NUMBER INT
)
INSERT INTO DBO.UNIQUE_NUMBERS (UNIQUE_NUMBER)
VALUES ('1000')
DECLARE @MyNewID VARCHAR(8);
DECLARE @NextNo INT;
UPDATE DBO.UNIQUE_NUMBERS
SET UNIQUE_NUMBER = UNIQUE_NUMBER +1, @NextNo = UNIQUE_NUMBER;
SET @MyNewID = REPLACE(STR(@NextNo,8),'','0');
INSERT INTO DBO.STUDENTS (STUDENT_ID, STUDENT_FIRSTNAME, STUDENT_SURNAME)
VALUES ('SID'+ (SELECT * FROM DBO.UNIQUE_NUMBERS) , 'Daenerys' , 'Targaryen' ),
('SID'+ @MyNewID , 'Jon' , 'Snow' ),
('SID'+ @MyNewID , 'Gregor' , 'Clegane' ),
('SID'+ @MyNewID , 'Arya' , 'Stark' ),
('SID'+ @MyNewID , 'Cersei' , 'Lannister' ),
('SID'+ @MyNewID , 'Joffrey' , 'Baratheon' ),
('SID'+ @MyNewID , 'Petyr' , 'Baelish' ),
('SID'+ @MyNewID , 'Khal' , 'Drogo' ),
('SID'+ @MyNewID , 'Theon' , 'Greyjoy' ),
('SID'+ @MyNewID , 'Ramsey' , 'Bolton' )
当我通过 Web 应用程序视图查看数据时,STUDENT_ID 应显示如下示例:
STD12345678
然后为下一个学生递增:
STD12345679
这样的事情怎么样 -
create table #temp
( STUDENT_KEY INT identity (10000000,1),
STUDENT_ID AS CONCAT('STD',STUDENT_KEY),
STUDENT_FIRSTNAME VARCHAR(255),
STUDENT_SURNAME VARCHAR(255)
)
INSERT INTO #temp
VALUES ( 'Daenerys' , 'Targaryen' ),
( 'Jon' , 'Snow' ),
( 'Gregor' , 'Clegane' ),
( 'Arya' , 'Stark' ),
( 'Cersei' , 'Lannister' ),
( 'Joffrey' , 'Baratheon' ),
( 'Petyr' , 'Baelish' ),
( 'Khal' , 'Drogo' ),
( 'Theon' , 'Greyjoy' ),
( 'Ramsey' , 'Bolton' )
SELECT * FROM #temp