如果通过执行动态 SQL 命令在 table 中可用列,则当 select 列为 return 值时列名不明确
Ambiguous column name when select a column to return value if column is available in table by Execute Dynamic SQL commands
如果列在table中可用,我想return一个列值,如果没有,return一个默认值,那么当我遇到COLUMN_NAME模棱两可的错误时加入两个 table SHAIN1 和 RIREKI14。如果 select 来自一个 table,那么查询工作正常,但如果我加入两个 table,我就会遇到问题。
declare @sql nvarchar(max) = ' SELECT 1 as id, '+ (case when exists (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA ='dbo' and TABLE_NAME='RIREKI14' and COLUMN_NAME='KOM001') then 'KOM001' else 'NULL' end) + ' as day ' + ' From RIREKI14 join SHAIN1 on RIREKI14.INCODE = SHAIN1.INCODE '; exec sp_executesql @sql
请帮帮我!
这是你的逻辑:
declare @sql nvarchar(max) = '
SELECT 1 as id, '+
(case when exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' and TABLE_NAME = 'RIREKI14' and COLUMN_NAME = 'KOM001')
then 'KOM001'
else 'NULL'
end) + ' as day ' + '
From RIREKI14 join
SHAIN1
on RIREKI14.INCODE = SHAIN1.INCODE
';
exec sp_executesql @sql;
唯一可能出现歧义的列名是来自 case
的名称。那么,让我们限定一下:
declare @sql nvarchar(max) = '
SELECT 1 as id, '+
(case when exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' and TABLE_NAME = 'RIREKI14' and COLUMN_NAME = 'KOM001')
then 'r.KOM001'
else 'NULL'
end) + ' as day ' + '
From RIREKI14 r join
SHAIN1 s
on r.INCODE = s.INCODE
';
exec sp_executesql @sql;
如果列在table中可用,我想return一个列值,如果没有,return一个默认值,那么当我遇到COLUMN_NAME模棱两可的错误时加入两个 table SHAIN1 和 RIREKI14。如果 select 来自一个 table,那么查询工作正常,但如果我加入两个 table,我就会遇到问题。
declare @sql nvarchar(max) = ' SELECT 1 as id, '+ (case when exists (SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA ='dbo' and TABLE_NAME='RIREKI14' and COLUMN_NAME='KOM001') then 'KOM001' else 'NULL' end) + ' as day ' + ' From RIREKI14 join SHAIN1 on RIREKI14.INCODE = SHAIN1.INCODE '; exec sp_executesql @sql
请帮帮我!
这是你的逻辑:
declare @sql nvarchar(max) = '
SELECT 1 as id, '+
(case when exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' and TABLE_NAME = 'RIREKI14' and COLUMN_NAME = 'KOM001')
then 'KOM001'
else 'NULL'
end) + ' as day ' + '
From RIREKI14 join
SHAIN1
on RIREKI14.INCODE = SHAIN1.INCODE
';
exec sp_executesql @sql;
唯一可能出现歧义的列名是来自 case
的名称。那么,让我们限定一下:
declare @sql nvarchar(max) = '
SELECT 1 as id, '+
(case when exists (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dbo' and TABLE_NAME = 'RIREKI14' and COLUMN_NAME = 'KOM001')
then 'r.KOM001'
else 'NULL'
end) + ' as day ' + '
From RIREKI14 r join
SHAIN1 s
on r.INCODE = s.INCODE
';
exec sp_executesql @sql;