我正在尝试在由其他两个值组成的存储过程中设置一个值。假设我们有这张桌子
CREATE TABLE [dbo].[Consecutives]
(
[Consecutives_ID] [INT] IDENTITY(1,1) NOT NULL,
[Name] [VARCHAR](100) NULL,
[Value] [INT] NOT NULL,
[Prefix] [VARCHAR] NOT NULL,
CONSTRAINT PK_Consecutives PRIMARY KEY (Consecutives_ID)
);
并通过一个存储过程试图创建一个由 prefix + value
组成的值INSERT INTO [dbo].[Consecutives] ([Name], [Value], [Prefix])
VALUES (Users, 50, "USR-")
预期结果将用作另一张表的ID,理论上我们期望的是“ USR-50 ”
在创建的存储过程中,我无法将VARCHAR
与INT
联接在一起,但是无法将它们串联起来。
这是我的存储过程代码:
CREATE PROCEDURE InsertUser
(@Name VARCHAR(100),
@LogIn VARCHAR(100),
@Pass VARCHAR(100))
AS
DECLARE @ID VARCHAR(100)
SET @ID = @ID + (SELECT [dbo].[Consecutives]([Prefix])
FROM [dbo].[Consecutives]
WHERE Name = 'Usuarios')
+
(SELECT [dbo].[Consecutives]([value])
FROM [dbo].[Consecutives]
WHERE Name = 'Usuarios')
INSERT INTO [dbo].[Users] ([User_ID], [LogIn], [Pass])
VALUES (@ID, @LogIn, @Pass)
答案 0 :(得分:3)
我认为该过程中存在很多错误,请检查以下语法:
CREATE PROCEDURE InsertUser
(
@Name varchar(100),
@LogIn varchar(100),
@Pass varchar(100)
)
AS
DECLARE @ID varchar(100)
SELECT @ID = CAST([Prefix] AS VARCHAR(50)) +
CAST([value] AS VARCHAR(50))
FROM [dbo].[Consecutives]
WHERE Name = @Name)
INSERT INTO [dbo].[Users] ([User_ID], [LogIn], [Pass])
VALUES (@ID,@LogIn,@Pass)