如何在动态sql查询中添加连字符

时间:2023-01-27 17:02:49

I have a table with columns A,B,C,D (type of varchar).

我有一个包含A,B,C,D列(varchar类型)的表。

From a stored procedure i am fetching data from the same table.I am created a dynamic sql query inside the stored procedure for fetching data from the table.

从存储过程我从同一个表中获取数据。我在存储过程中创建了一个动态sql查询,用于从表中获取数据。

what i want is that,

我想要的是,

need to combine column B and C together with a symbol(hyphen or colon) and display it as a single section.

需要将列B和C与符号(连字符或冒号)组合在一起,并将其显示为单个部分。

DECLARE @sSQL nvarchar(100);
DECLARE @symbol nvarchar(100);
SET @symbol='-'
SELECT @sSQL = N'SELECT [A], ([B], '+@symbol+', [C]) as Status FROM Table';
EXEC sp_executesql @sSQL

above query is not working for me.I need following query as a dynamic query.

以上查询对我不起作用。我需要将以下查询作为动态查询。

SELECT A,B+'-'+C as Status FROM Table

Please help.

1 个解决方案

#1


1  

You do need commas there. Here is the correct syntax:

你确实需要逗号。这是正确的语法:

SELECT @sSQL = N'SELECT [A], ([B] + '''+@symbol+''' + [C]) as Status FROM Table';

#1


1  

You do need commas there. Here is the correct syntax:

你确实需要逗号。这是正确的语法:

SELECT @sSQL = N'SELECT [A], ([B] + '''+@symbol+''' + [C]) as Status FROM Table';