从不同的列中获取值并存储在 Sql 服务器中以逗号分隔的字符串中
Getting value from different columns and storing in a string with comma separated in Sql server
我的要求是 Table 有 5 列。我想要的是从行中获取值,并希望将所有 5 个值存储在以逗号分隔的变量中。
table Name: table1
col1 col2 col3 col4 col5
1 2 3 4 5
结果应该是
1,2,3,4,5
注意:我不想提及列名,如果我们使用 Concat 函数,我们需要提及列,但这里我的要求是我将只有 table 名称
使用 table : information_schema.columns
DECLARE @col1 varchar(10)
DECLARE @col2 varchar(10)
DECLARE @col3 varchar(10)
DECLARE @col4 varchar(10)
DECLARE @col5 varchar(10)
SET @col1 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '1')
SET @col2 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '2')
SET @col3 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '3')
SET @col4 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '4')
SET @col5 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '5')
DECLARE @sqlText nvarchar(1000);
SET @sqlText = N'SELECT ' + @col1 + ',' + @col2 + ',' + @col3 + ','+ @col4 + ','+ @col5 +' FROM dbo.table1'
Exec (@sqlText)
我总是犹豫是否给出建议动态 SQL 的答案,通常当需要动态 SQL 时,最好在 SQL 之外解决问题。 Erland Sommarskog 就此主题撰写了大量文章 - The Curse and Blessings of Dynamic SQL
无论如何,您的问题可以使用动态 SQL 来解决。您可以使用系统视图构建 SQL,并使用 SQL Server's XML extensions:
连接列名称
DECLARE @TableName SYSNAME = 'dbo.table1';
DECLARE @SQL NVARCHAR(MAX) =
'SELECT CONCAT(' + STUFF(( SELECT ','','',' + QUOTENAME(c.Name)
FROM sys.columns c
WHERE c.object_id = OBJECT_ID(@TableName, 'U')
ORDER BY Column_id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 5, '') + ')
FROM ' + @TableName + ';';
EXECUTE sp_executesql @SQL;
所以对于这个 table:
CREATE TABLE dbo.Table1
(
Col1 VARCHAR(10),
Col2 VARCHAR(10),
Col3 INT,
Col4 INT,
Col5 VARCHAR(30)
);
生成的SQL为:
SELECT CONCAT([Col1],',',[Col2],',',[Col3],',',[Col4],',',[Col5])
FROM dbo.table1;
如果您的 table 中总是恰好有 5 列,这将有效。
请注意,这不是指列名称
;WITH cte(a,b,c,d,e) as
(
-- can test with this:
--SELECT * FROM (values(1,2,3,4,5)) x(col1,col2,col3,col4,col5)
SELECT * FROM table1
)
SELECT
concat(a, ',', b, ',', c, ',', d, ',', e)
FROM
cte
结果:
1,2,3,4,5
只需将您的 table 的名称替换为变量 @tablename 就可以了。
NULL 值将被转换为 'NULL' 字符串
SET NOCOUNT ON
DECLARE @tablename nvarchar(50) = 'atable'
DECLARE @colList nvarchar(max)
DECLARE @sql nvarchar(max)
--Select list of colum names
select @colList = coalesce('CAST('+@Collist+'+'', '' ' +'+ CAST(','')+name+' as nvarchar)'
from
(
select name
from sys.columns
where [object_id] = OBJECT_ID(@tablename)
) sub
SET @colList = REPLACE(REPLACE(@colList, 'CAST(', 'ISNULL(CAST('), 'nvarchar)', 'nvarchar), ''NULL'')')
SET @sql = 'SELECT '+@colList+' FROM '+@tablename
EXEC(@sql)
示例:
这个table (atable)
col1 col2
-------------------------------------------------- -----------
foo 1
bar 2
abc 3
def 4
ghi 5
yep NULL
将转换为
--------------------------------------------------------------
foo, 1
bar, 2
abc, 3
def, 4
ghi, 5
yep, NULL
returns 每行 1 个结果 table。
我的要求是 Table 有 5 列。我想要的是从行中获取值,并希望将所有 5 个值存储在以逗号分隔的变量中。
table Name: table1
col1 col2 col3 col4 col5
1 2 3 4 5
结果应该是
1,2,3,4,5
注意:我不想提及列名,如果我们使用 Concat 函数,我们需要提及列,但这里我的要求是我将只有 table 名称
使用 table : information_schema.columns
DECLARE @col1 varchar(10)
DECLARE @col2 varchar(10)
DECLARE @col3 varchar(10)
DECLARE @col4 varchar(10)
DECLARE @col5 varchar(10)
SET @col1 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '1')
SET @col2 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '2')
SET @col3 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '3')
SET @col4 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '4')
SET @col5 = (select column_name from information_schema.columns where table_name = 'table1' and ordinal_position = '5')
DECLARE @sqlText nvarchar(1000);
SET @sqlText = N'SELECT ' + @col1 + ',' + @col2 + ',' + @col3 + ','+ @col4 + ','+ @col5 +' FROM dbo.table1'
Exec (@sqlText)
我总是犹豫是否给出建议动态 SQL 的答案,通常当需要动态 SQL 时,最好在 SQL 之外解决问题。 Erland Sommarskog 就此主题撰写了大量文章 - The Curse and Blessings of Dynamic SQL
无论如何,您的问题可以使用动态 SQL 来解决。您可以使用系统视图构建 SQL,并使用 SQL Server's XML extensions:
连接列名称DECLARE @TableName SYSNAME = 'dbo.table1';
DECLARE @SQL NVARCHAR(MAX) =
'SELECT CONCAT(' + STUFF(( SELECT ','','',' + QUOTENAME(c.Name)
FROM sys.columns c
WHERE c.object_id = OBJECT_ID(@TableName, 'U')
ORDER BY Column_id
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 5, '') + ')
FROM ' + @TableName + ';';
EXECUTE sp_executesql @SQL;
所以对于这个 table:
CREATE TABLE dbo.Table1
(
Col1 VARCHAR(10),
Col2 VARCHAR(10),
Col3 INT,
Col4 INT,
Col5 VARCHAR(30)
);
生成的SQL为:
SELECT CONCAT([Col1],',',[Col2],',',[Col3],',',[Col4],',',[Col5])
FROM dbo.table1;
如果您的 table 中总是恰好有 5 列,这将有效。 请注意,这不是指列名称
;WITH cte(a,b,c,d,e) as
(
-- can test with this:
--SELECT * FROM (values(1,2,3,4,5)) x(col1,col2,col3,col4,col5)
SELECT * FROM table1
)
SELECT
concat(a, ',', b, ',', c, ',', d, ',', e)
FROM
cte
结果:
1,2,3,4,5
只需将您的 table 的名称替换为变量 @tablename 就可以了。
NULL 值将被转换为 'NULL' 字符串
SET NOCOUNT ON
DECLARE @tablename nvarchar(50) = 'atable'
DECLARE @colList nvarchar(max)
DECLARE @sql nvarchar(max)
--Select list of colum names
select @colList = coalesce('CAST('+@Collist+'+'', '' ' +'+ CAST(','')+name+' as nvarchar)'
from
(
select name
from sys.columns
where [object_id] = OBJECT_ID(@tablename)
) sub
SET @colList = REPLACE(REPLACE(@colList, 'CAST(', 'ISNULL(CAST('), 'nvarchar)', 'nvarchar), ''NULL'')')
SET @sql = 'SELECT '+@colList+' FROM '+@tablename
EXEC(@sql)
示例: 这个table (atable)
col1 col2
-------------------------------------------------- -----------
foo 1
bar 2
abc 3
def 4
ghi 5
yep NULL
将转换为
--------------------------------------------------------------
foo, 1
bar, 2
abc, 3
def, 4
ghi, 5
yep, NULL
returns 每行 1 个结果 table。