我在SQL中有一个表,其中包含一个名为CCARD-000093
CCARD-000094
CCARD-000095
CCARD-000096
CCARD-000097
CCARD-000098
CCARD-000099
CCARD-000100
CCARD-000101
数据,例如
SELECT 'CCARD-' +
(SELECT CAST(MAX(CAST(SUBSTRING(ccc.CreditCardCode,7, 6) as INT)) AS
NVARCHAR(MAX)) FROM CustomerCreditCard ccc)
将数据插入此表时,如何获取序列中的下一个值?
这是我到目前为止所拥有的
CCARD-101
这会给CCARD-000101
如何让它返回defaultConfig {
applicationId "com.example.myname.myprj"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
???
答案 0 :(得分:1)
如果要在插入Record时增加表,可以使用Identity和Presisted Concept自动递增
IF OBJECT_ID('TestTables', 'U') IS NOT NULL
BEGIN
DROP TABLE TestTables
END
重置您要开始的身份
DBCC checkident ('TestTables', reseed, 93)
CREATE TABLE TestTables
(
CreditCard AS ('CCARD-0000' + CAST(AutoIncId as varchar)) PERSISTED NOT NULL PRIMARY KEY,
[AutoIncId] INT IDENTITY NOT NULL,
[Bank] VARCHAR(10)
);
INSERT INTO TestTables ([Bank]) VALUES ('Indus')
INSERT INTO TestTables ([Bank]) VALUES ('ICICI')
INSERT INTO TestTables ([Bank]) VALUES ('HDFC')
INSERT INTO TestTables ([Bank]) VALUES ('DBS')
SELECT * FROM TestTables
答案 1 :(得分:1)
下一个值,固定长度,带前导' 0':
SELECT 'CCARD-' + RIGHT(REPLICATE('0',6)+CAST(1+MAX(CAST(SUBSTRING(ccc.CreditCardCode,7, 6) as INT)) AS NVARCHAR(6)),6)
FROM CustomerCreditCard ccc
答案 2 :(得分:0)
这似乎可以解决问题
INSERT INTO CustomerCreditCard (CustomerCode, CreditCardCode, CreditCardDescription, NameOnCard, Address, City, State, PostalCode, Country, ExpMonth, ExpYear, Telephone, Email, IsActive, ResidenceType, UserCreated, DateCreated, UserModified, DateModified, County, StartMonth, StartYear)
SELECT c.CustomerCode,
(SELECT 'CCARD-' + REPLICATE('0', 6 - LEN((SELECT CAST(MAX(CAST(SUBSTRING(ccc.CreditCardCode,7, 6) as INT)) + 1 AS NVARCHAR(MAX)) FROM CustomerCreditCard ccc))) +
(SELECT CAST(MAX(CAST(SUBSTRING(ccc.CreditCardCode,7, 6) as INT)) + 1 AS NVARCHAR(MAX)) FROM CustomerCreditCard ccc)),
'Web Credit Card',
c.CustomerName,
c.Address,
c.City,
c.State,
c.PostalCode,
c.Country,
'Jan',
2025,
c.Telephone,
c.Email,
1,
c.ResidenceType,
'webadmin',
GETDATE(),
'webadmin',
GETDATE(),
c.County,
'Jan',
2010
FROM Customer c
WHERE c.CustomerCode = 'CUST-015056'