简体   繁体   中英

Converting value on stored procedure

I'm trying to set a value on a stored procedure that is composed by two other values. Let's say we have this table

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)
);

and with a stored procedure I'm trying to create a value composed from the prefix + value

INSERT INTO [dbo].[Consecutives] ([Name], [Value], [Prefix]) 
VALUES (Users, 50, "USR-")

The expected result will be used as the ID for another table, in theory what we expect is " USR-50 "

In the stored procedure I'm creating I can't join a VARCHAR with an INT , but I can't concatenate them.

Here's my stored procedure code:

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)

I think there is a lot of errors within the procedure, check the following syntax:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM