SQL Server:无法在其他字符串的中间连接字符串

时间:2022-01-09 15:53:59

In my SQL Server stored procedures I want to do this:

在我的SQL Server存储过程中,我想这样做:

"hello xxx how are you"

where xxx is a value of a variable

其中xxx是变量的值

I tried this

我试过这个

CREATE PROCEDURE insertToPinTable
    (@callerID VARCHAR (200),  
     @vAccount VARCHAR (200))
AS
BEGIN
     DECLARE @textEnglish VARCHAR
     textEnglish = CONCAT ( "hello", @vAccount)

     INSERT INTO pinData([CallerID], [body], [processed]) 
     VALUES (@callerID, @textEnglish, 0)
END

I get an error

我收到一个错误

Incorrect syntax near 'textEnglish'

'textEnglish'附近的语法不正确

it seems that using CONCAT is not correct, could you help me please

似乎使用CONCAT是不正确的,你能帮我吗?

(if I can insert the value of vAccount to the hello word, then in the same way I can insert the value of textEnglish, to the string how are you)

(如果我可以将vAccount的值插入到hello单词中,那么我可以以相同的方式将textEnglish的值插入到字符串中,你好吗?)

2 个解决方案

#1


4  

CREATE  PROCEDURE insertToPinTable
(
@callerID VARCHAR (200),  
@vAccount VARCHAR (200)    
)
AS
BEGIN

DECLARE @textEnglish VARCHAR(255) --missing length previously
set @textEnglish = 'hello'+ @vAccount --missing set keyword
INSERT INTO pinData([CallerID], [body], [processed]) VALUES (@callerID, @textEnglish, 0)

END

#2


0  

If you just want to change a string from

如果您只想更改字符串

"hello xxx how are you"

to

"hello Mr. Smith how are you"

You might think about place holder replacement:

您可以考虑更换占位符:

Define your string as a template:

将字符串定义为模板:

DECLARE @template VARCHAR(100)='hello {Name} how are you?';

And then use

然后使用

SELECT REPLACE(@template,'{Name}','Mr. Smith');

#1


4  

CREATE  PROCEDURE insertToPinTable
(
@callerID VARCHAR (200),  
@vAccount VARCHAR (200)    
)
AS
BEGIN

DECLARE @textEnglish VARCHAR(255) --missing length previously
set @textEnglish = 'hello'+ @vAccount --missing set keyword
INSERT INTO pinData([CallerID], [body], [processed]) VALUES (@callerID, @textEnglish, 0)

END

#2


0  

If you just want to change a string from

如果您只想更改字符串

"hello xxx how are you"

to

"hello Mr. Smith how are you"

You might think about place holder replacement:

您可以考虑更换占位符:

Define your string as a template:

将字符串定义为模板:

DECLARE @template VARCHAR(100)='hello {Name} how are you?';

And then use

然后使用

SELECT REPLACE(@template,'{Name}','Mr. Smith');