您的方法没有理由不起作用。再次检查您的代码:
SQL小提琴
MS SQL Server 2008 架构设置:
CREATE TABLE INTERFACE
([ID] int,
[COL1] varchar(2),
[COL2] varchar(2),
[COL3] varchar(2),
[COL4] varchar(2),
[CASENO] int,
[INTLINENO] int,
[INTMEMBNO] int,
[INTSTATUS] int,
[RECTYPE] varchar(2),
[EFFDTE] date,
[INTERR] int,
[INTERR2] int,
[INTWARN] int,
[AUTHORISED] varchar(12),
[EXCLUDE] varchar(10),
[UPDATED] varchar(6),
[INTMEMBNO2] int,
[INTSTATUS2] int,
[ALTKEY] varchar(12),
[COL5] varchar(2))
;
查询 1:
DECLARE @excludeFieldTable TABLE
(
excludeFieldName varchar(100)
)
INSERT INTO @excludeFieldTable VALUES
('CASENO'),('INTLINENO'),('INTMEMBNO'),('INTSTATUS'),
('RECTYPE'),('EFFDTE'),('INTERR'),('INTERR2'),
('INTWARN'),('AUTHORISED'),('EXCLUDE'),('UPDATED'),
('INTMEMBNO2'),('INTSTATUS2'),('ALTKEY')
DECLARE @intColumnList TABLE
(
columnList varchar(100)
)
INSERT INTO @intColumnList
SELECT DISTINCT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='INTERFACE' AND
COLUMN_NAME NOT IN (SELECT DISTINCT(excludeFieldName) FROM @excludeFieldTable)
SELECT *
FROM @intColumnList
结果:
| COLUMNLIST |
|------------|
| COL1 |
| COL2 |
| COL3 |
| COL4 |
| COL5 |
| ID |
编辑:
使用字符串,它看起来像这样:
查询 2:
DECLARE @excludeFieldTable VARCHAR(1000)
SET @excludeFieldTable = 'CASENO, INTLINENO, INTMEMBNO, INTSTATUS, RECTYPE, EFFDTE, INTERR, INTERR2, INTWARN, AUTHORISED, EXCLUDE, UPDATED, INTMEMBNO2, INTSTATUS2, ALTKEY'
DECLARE @intColumnList TABLE
(
columnList varchar(100)
)
INSERT INTO @intColumnList
SELECT DISTINCT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='INTERFACE' AND
', '+@excludeFieldTable+', ' NOT LIKE '%, '+COLUMN_NAME+', %'
SELECT *
FROM @intColumnList
结果:
| COLUMNLIST |
|------------|
| COL1 |
| COL2 |
| COL3 |
| COL4 |
| COL5 |
| ID |
重新编辑:
SP工作也很好:
查询 3:
CREATE PROCEDURE ExcludeField1 AS
DECLARE @excludeFieldTable VARCHAR(1000)
SET @excludeFieldTable = 'CASENO, INTLINENO, INTMEMBNO, INTSTATUS, RECTYPE, EFFDTE, INTERR, INTERR2, INTWARN, AUTHORISED, EXCLUDE, UPDATED, INTMEMBNO2, INTSTATUS2, ALTKEY'
DECLARE @intColumnList TABLE
(
columnList varchar(100)
)
INSERT INTO @intColumnList
SELECT DISTINCT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='INTERFACE' AND
', '+@excludeFieldTable+', ' NOT LIKE '%, '+COLUMN_NAME+', %'
SELECT * FROM @intColumnList
结果:
问题 4:
CREATE PROCEDURE ExcludeField2 AS
DECLARE @excludeFieldTable TABLE
(
excludeFieldName varchar(100)
)
INSERT INTO @excludeFieldTable VALUES
('CASENO'),('INTLINENO'),('INTMEMBNO'),('INTSTATUS'),
('RECTYPE'),('EFFDTE'),('INTERR'),('INTERR2'),
('INTWARN'),('AUTHORISED'),('EXCLUDE'),('UPDATED'),
('INTMEMBNO2'),('INTSTATUS2'),('ALTKEY')
DECLARE @intColumnList TABLE
(
columnList varchar(100)
)
INSERT INTO @intColumnList
SELECT DISTINCT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='INTERFACE' AND
COLUMN_NAME NOT IN (SELECT DISTINCT(excludeFieldName) FROM @excludeFieldTable)
SELECT * FROM @intColumnList
结果:
问题 5:
EXEC ExcludeField1
结果:
| COLUMNLIST |
|------------|
| COL1 |
| COL2 |
| COL3 |
| COL4 |
| COL5 |
| ID |
问题 6:
EXEC ExcludeField2
结果:
| COLUMNLIST |
|------------|
| COL1 |
| COL2 |
| COL3 |
| COL4 |
| COL5 |
| ID |
如果您想在之前定义排除字符串,它将如下所示:
问题 7:
CREATE PROCEDURE ExcludeField1 @excludeFieldString varchar(1000) AS
DECLARE @intColumnList TABLE
(
columnList varchar(100)
)
INSERT INTO @intColumnList
SELECT DISTINCT(COLUMN_NAME)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='INTERFACE' AND
', '+@excludeFieldString+', ' NOT LIKE '%, '+COLUMN_NAME+', %'
SELECT * FROM @intColumnList
结果:
问题 8:
DECLARE @excludeFieldTable VARCHAR(1000)
SET @excludeFieldTable = 'CASENO, INTLINENO, INTMEMBNO, INTSTATUS, RECTYPE, EFFDTE, INTERR, INTERR2, INTWARN, AUTHORISED, EXCLUDE, UPDATED, INTMEMBNO2, INTSTATUS2, ALTKEY'
EXEC ExcludeField1 @excludeFieldString = @excludeFieldTable
结果:
| COLUMNLIST |
|------------|
| COL1 |
| COL2 |
| COL3 |
| COL4 |
| COL5 |
| ID |