简体   繁体   English

字符串和变量 SQL 的串联

[英]Concatenation of character string and variable SQL

SET @Result = '{ "host_name": "' + @hostname + '", "status": 15 }'

Could you explain to me what we get with the value of Result please?你能向我解释一下我们从 Result 的值中得到了什么吗? If we assume that hostname = "machine1".如果我们假设主机名 = "machine1"。

To test this, I created a table and a Stored Procedure and Print the value of Result :为了测试这一点,我创建了一个表和一个存储过程并打印了 Result 的值:

CREATE TABLE utilisateur
(
    
    nom VARCHAR(100),
    prenom VARCHAR(100),    
    age VARCHAR(5),
    
)
INSERT INTO utilisateur (prenom, nom, age)
 VALUES
 ('Rébecca', 'Armand', 24)
go

 CREATE PROCEDURE [sp_DB] 
  @hostname nvarchar(50),
  @Result nvarchar(2000) OUTPUT

AS
  select age as a from utilisateur where nom = 'Armand' 
  SET @hostname = 'machine1'
  SET @Result = '{ "host_name": "' + @hostname + '", "a": 15 }' 
  print @Result

EXEC [sp_DB]@hostname ='machine1',@Result = 'test';

But the execution does not display the content of the Result variable.但是执行不显示 Result 变量的内容。

PS : I use sqlfiddle.com/ to execute the code PS:我使用 sqlfiddle.com/ 来执行代码

You have to define the output variable on the procedure calling scope, and use the keyword OUTPUT to retrieve output the value.您必须在过程调用范围内定义输出变量,并使用关键字 OUTPUT 来检索输出值。 So if you do it like:所以如果你这样做:

declare @result_out nvarchar(2000)
EXEC [sp_DB]@hostname ='machine1',@Result=@result_out OUTPUT;
select @result_out as [Result]

It will work.它会起作用。

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

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