循环当前 table 以查找列是否为主键?
Loop through current table to find if column is a primary key?
我有一个名为#ColumnList 的临时文件 table,它包含一个列名列表。我需要遍历此 table 中的每一行并查看该列是否为主键。如果它是一个键,那么我需要将该列名添加到名为 PRIMARYKEYCOLUMN 的列中。
这是我目前所拥有的,但它不起作用。它抛出一个错误
Cannot insert the value NULL into column 'DataType', table 'tempdb.dbo.#ColumnList__________________0000000B0D8C'; column does not allow nulls. INSERT fails.
ALTER TABLE #ColumnList
ADD PRIMARYKEYCOLUMN VARCHAR(50);
INSERT INTO #ColumnList ([PRIMARYKEYCOLUMN])
(SELECT DISTINCT KU.column_name as PRIMARYKEYCOLUMN
FROM
sys.columns c
JOIN sys.types t ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN sys.tables tab ON c.object_id = tab.object_id
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME AND
KU.table_name='myTableName'
JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME)
上面的子查询(没有 JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME)工作正常并且 returns 下面的结果。但是插入不起作用。
[PRIMARYKEYCOLUMN]
Column1
Column2
Column3
我需要将上面那些相同的列插入到存在这些相同列名的临时 table 中
更新语句可能有效,例如:
update
#ColumnList
set
PRIMARYKEYCOLUMN = ColumnName
where
ColumnName in (
SELECT
DISTINCT KU.column_name
FROM
sys.columns c
JOIN
sys.types t
ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN
sys.tables tab
ON c.object_id = tab.object_id
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
AND KU.table_name='myTableName'
)
我有一个名为#ColumnList 的临时文件 table,它包含一个列名列表。我需要遍历此 table 中的每一行并查看该列是否为主键。如果它是一个键,那么我需要将该列名添加到名为 PRIMARYKEYCOLUMN 的列中。
这是我目前所拥有的,但它不起作用。它抛出一个错误
Cannot insert the value NULL into column 'DataType', table 'tempdb.dbo.#ColumnList__________________0000000B0D8C'; column does not allow nulls. INSERT fails.
ALTER TABLE #ColumnList
ADD PRIMARYKEYCOLUMN VARCHAR(50);
INSERT INTO #ColumnList ([PRIMARYKEYCOLUMN])
(SELECT DISTINCT KU.column_name as PRIMARYKEYCOLUMN
FROM
sys.columns c
JOIN sys.types t ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN sys.tables tab ON c.object_id = tab.object_id
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME AND
KU.table_name='myTableName'
JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME)
上面的子查询(没有 JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME)工作正常并且 returns 下面的结果。但是插入不起作用。
[PRIMARYKEYCOLUMN]
Column1
Column2
Column3
我需要将上面那些相同的列插入到存在这些相同列名的临时 table 中
更新语句可能有效,例如:
update
#ColumnList
set
PRIMARYKEYCOLUMN = ColumnName
where
ColumnName in (
SELECT
DISTINCT KU.column_name
FROM
sys.columns c
JOIN
sys.types t
ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN
sys.tables tab
ON c.object_id = tab.object_id
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
AND KU.table_name='myTableName'
)