在 sql 服务器 2014 下合并特定条件下的列
Merging a column under a specific criteria under sql sever 2014
这个问题我已经尝试修复了几个小时,基本上我有一个 SQL 代码打印数据库中的 tables 及其列以及一些其他信息如下:
What I get
问题是我想删除显示 table 名称的第一列中的重复项,但我想在不同的行中保留 'columns' 属性,基本上我需要重复项成为为空或合并表示相同 table 的行的第一列。我知道这是广泛的解释,但这是我想要的样子:wanted result
这是我在 sql 服务器 2014 下使用的 sql 代码:
SELECT CAST(t.NAME AS CHAR(12)) AS [Table]
,CAST(c.NAME AS CHAR(20)) AS [Column]
,CAST(d.NAME AS CHAR(9)) AS [Data type]
,CASE
WHEN d.NAME IN (
'char'
,'varchar'
)
THEN STR(c.max_length, 6, 0)
ELSE ''
END AS [Length]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.precision, 9, 0)
ELSE ''
END AS [Precision]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.scale, 5, 0)
ELSE ''
END AS [Scale]
,CASE c.is_nullable
WHEN 0
THEN 'not null'
ELSE ''
END AS [Nullable]
FROM sys.columns AS c
JOIN sys.tables AS t ON c.object_id = t.object_id
JOIN sys.types AS d ON c.system_type_id = d.system_type_id
ORDER BY 1
,column_id;
该代码应该适用于 sql 服务器 2008 或更高版本下的任何数据库。感谢您的帮助。
这应该有效;
WITH CTE
AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY t.NAME ORDER BY c.NAME
) AS ID
,CAST(t.NAME AS CHAR(12)) AS [Table]
,CAST(c.NAME AS CHAR(20)) AS [Column]
,CAST(d.NAME AS CHAR(9)) AS [Data type]
,CASE
WHEN d.NAME IN (
'char'
,'varchar'
)
THEN STR(c.max_length, 6, 0)
ELSE ''
END AS [Length]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.precision, 9, 0)
ELSE ''
END AS [Precision]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.scale, 5, 0)
ELSE ''
END AS [Scale]
,CASE c.is_nullable
WHEN 0
THEN 'not null'
ELSE ''
END AS [Nullable]
FROM sys.columns AS c
JOIN sys.tables AS t ON c.object_id = t.object_id
JOIN sys.types AS d ON c.system_type_id = d.system_type_id
)
SELECT CASE
WHEN ct.ID = 1
THEN ct.[Table]
ELSE ''
END AS 'Table'
,ct.[Column]
,ct.[Data type]
,ct.Length
,ct.Precision
,ct.Scale
,ct.Nullable
FROM CTE ct
这个问题我已经尝试修复了几个小时,基本上我有一个 SQL 代码打印数据库中的 tables 及其列以及一些其他信息如下: What I get
问题是我想删除显示 table 名称的第一列中的重复项,但我想在不同的行中保留 'columns' 属性,基本上我需要重复项成为为空或合并表示相同 table 的行的第一列。我知道这是广泛的解释,但这是我想要的样子:wanted result
这是我在 sql 服务器 2014 下使用的 sql 代码:
SELECT CAST(t.NAME AS CHAR(12)) AS [Table]
,CAST(c.NAME AS CHAR(20)) AS [Column]
,CAST(d.NAME AS CHAR(9)) AS [Data type]
,CASE
WHEN d.NAME IN (
'char'
,'varchar'
)
THEN STR(c.max_length, 6, 0)
ELSE ''
END AS [Length]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.precision, 9, 0)
ELSE ''
END AS [Precision]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.scale, 5, 0)
ELSE ''
END AS [Scale]
,CASE c.is_nullable
WHEN 0
THEN 'not null'
ELSE ''
END AS [Nullable]
FROM sys.columns AS c
JOIN sys.tables AS t ON c.object_id = t.object_id
JOIN sys.types AS d ON c.system_type_id = d.system_type_id
ORDER BY 1
,column_id;
该代码应该适用于 sql 服务器 2008 或更高版本下的任何数据库。感谢您的帮助。
这应该有效;
WITH CTE
AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY t.NAME ORDER BY c.NAME
) AS ID
,CAST(t.NAME AS CHAR(12)) AS [Table]
,CAST(c.NAME AS CHAR(20)) AS [Column]
,CAST(d.NAME AS CHAR(9)) AS [Data type]
,CASE
WHEN d.NAME IN (
'char'
,'varchar'
)
THEN STR(c.max_length, 6, 0)
ELSE ''
END AS [Length]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.precision, 9, 0)
ELSE ''
END AS [Precision]
,CASE
WHEN d.NAME IN (
'numeric'
,'decimal'
)
THEN STR(c.scale, 5, 0)
ELSE ''
END AS [Scale]
,CASE c.is_nullable
WHEN 0
THEN 'not null'
ELSE ''
END AS [Nullable]
FROM sys.columns AS c
JOIN sys.tables AS t ON c.object_id = t.object_id
JOIN sys.types AS d ON c.system_type_id = d.system_type_id
)
SELECT CASE
WHEN ct.ID = 1
THEN ct.[Table]
ELSE ''
END AS 'Table'
,ct.[Column]
,ct.[Data type]
,ct.Length
,ct.Precision
,ct.Scale
,ct.Nullable
FROM CTE ct