-1

我想编写一个SELECT返回值但排除出现在文本列表中的任何语句的语句

所以我有一个文本列表COL1, COl2, COL4, COL5(称为myString

而我想说...

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'INTERFACE' AND COLUMN_NAME NOT IN(myString);

我该怎么做?

我试图在存储过程中解决这个问题,尽管回到基础会帮助我理解。我的 SP 代码如下所示...

INSERT INTO @excludeFieldTable VALUES ('CASENO'),('INTLINENO'),('INTMEMBNO'),('INTSTATUS'),('RECTYPE'),('EFFDTE'),('INTERR'),('INTERR2'),
('INTWARN'),('AUTHORISED'),('EXCLUDE'),('UPDATED'),('INTMEMBNO2'),('INTSTATUS2'),('ALTKEY');

    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);

但是由于NOT IN某种@excludeFieldTable原因,反对者没有评估(所以intColumnList仍然有值'CASENO','INTLINENO'等)

4

4 回答 4

1

您的方法没有理由不起作用。再次检查您的代码:

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 |
于 2013-10-04T14:32:59.853 回答
1
Exec('
SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME=''INTERFACE'' 
AND COLUMN_NAME NOT IN(' + @myString + ')')

另一个流行的选择是使用拆分函数,然后加入它:请参见此处: http: //ole.michelsen.dk/blog/split-string-to-table-using-transact-sql/

于 2013-10-04T14:02:54.787 回答
1

您不能将单个逗号分隔的字符串放入IN子句中。要么使用单独的值,要么使用动态地将其组合在一起EXEC

exec('SELECT COLUMN_NAME 
      FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE TABLE_NAME=''INTERFACE'' 
      AND COLUMN_NAME NOT IN (' + @myString + ')')
于 2013-10-04T14:02:40.207 回答
0

向所有人道歉(非常感谢您的时间)-我的 SP 代码中有一个小错字

于 2013-10-04T15:04:26.270 回答