简体   繁体   English

SQL Server 2014中的CONCAT

[英]CONCAT in SQL Server 2014

I have a problem when I want to use CONCAT inside my stored procedure. 我想在存储过程中使用CONCAT时遇到问题。 I press execute and get this error message: 我按执行并得到此错误信息:

Mens 195, Nivel 15, Estado 10, Procedimiento upd_agregar, Línea 96' 男子195,尼维尔15,埃斯塔多10,Procedimiento upd_agregar,利内尼亚96'
CONCAT' is not a recognized built-in function name. CONCAT'不是公认的内置函数名称。

This is my table: 这是我的桌子:

create table sucursales
(
    idSucursal  nvarchar(5) primary key,
    idEmpresa   int not null,
    sucursal    nvarchar(25) not null,
    direccion   nvarchar(100) not null,
    telefono    nvarchar(25),
    email       nvarchar(25) not null,
    constraint fk_suc_emp foreign key(idEmpresa) 
                references empresas(idEmpresa)
)

This is my query: 这是我的查询:

create procedure upd_agregar
    (@idEmpresa int, 
     @sucursal nvarchar(25),
     @direccion nvarchar(100),
     @telefono nvarchar (25),
     @email nvarchar(25))
as
    declare @longitud int, @codEmpresa nvarchar(2),@codSucursal nvarchar (2)

    --Generar codigo de empresa
    IF len(@idEmpresa) < 2
        SET @codEmpresa = CONCAT ('0',@idEmpresa)
    ELSE
        SET @codEmpresa = @idEmpresa
GO

and then when I want to make a select like this, I receive an error that I must declare a scalar variable @codSucursal: 然后当我要进行这样的选择时,我收到一个错误,我必须声明一个标量变量@codSucursal:

SELECT @codSucursal = isnull(max(cast(substring (idSucursal,4,2)AS int)),0) + 1 FROM sucursales 
WHERE idEmpresa = @idEmpresa

CONCAT was added in SQL Server 2012. Check the version of your server, maybe you are not using 2014 as you think: CONCAT是在SQL Server 2012中添加的。检查服务器的版本,也许您未按照预期使用2014:

SELECT @@VERSION

Besides, you don't really need CONCAT here and you don't need to check the LEN to convert an integer into a string with leading zeroes: 此外,您实际上并不需要CONCAT ,也不需要检查LEN即可将整数转换为带前导零的字符串:

SET @codEmpresa = RIGHT('00' + CAST(@idEmpresa as nvarchar(2)), 2)

Try this: 尝试这个:

create procedure upd_agregar
    (@idEmpresa int, @sucursal nvarchar(25),@direccion nvarchar(100),@telefono nvarchar (25),@email nvarchar(25))
    as
        declare @longitud int, @codEmpresa nvarchar(2),@codSucursal nvarchar (2)
        --Generar codigo de empresa
        IF len(CAST(@idEmpresa AS NVARCHAR(2))) < 2
        SET @codEmpresa = CONCAT ('0', CAST(@idEmpresa AS NVARCHAR(2)))
        ELSE
        SET @codEmpresa = @idEmpresa

        PRINT @codEmpresa
    GO

-- EXEC upd_agregar 1, 'B', 'C', 'D', 'E'
-- Result: 01

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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