用于检索非负整数数据的SQL查询

时间:2012-06-19 13:00:51

标签: sql

我需要一个查询,它只能从给定的表中检索非负的Integer值

假设我有一个表A,查询select* from A将从中检索所有内容。现在我想编写一个只给出非负值的查询。

假设:表格的所有列都包含负/正/零整数值以及字符。

6 个答案:

答案 0 :(得分:2)

Select * from A where col1 >=0 and col2 >=0 and .... and colN>=0

只需将col1 ... colN替换为列名。

答案 1 :(得分:1)

我认为这就是你所追求的:

SELECT FIELD1  
FROM TABLE
WHERE FIELD1 >= 0

UNION ALL

SELECT FIELD2  
FROM TABLE
WHERE FIELD2 >= 0

答案 2 :(得分:1)

我认为这是唯一的方法:

select * from A
where col1 > 0 and col2 > 0 and col3 > 0 and col4 > 0 ... and coln > 0

答案 3 :(得分:0)

只需?

SELECT * 
FROM TABLE
WHERE FIELD > 0

答案 4 :(得分:0)

select * from tablea where COLUMN > 0;

答案 5 :(得分:0)

如果您使用的是SQL Server ...

    DECLARE @name VARCHAR(50)
    DECLARE @tableName VARCHAR(50)
    DECLARE @whereClause VARCHAR(max)

    SET @tableName = 'TableName' --you can change text to other table name
    SET @whereClause = ' WHERE -1 >= 0'

    DECLARE db_cursor CURSOR FOR  
    SELECT c.name
    FROM sys.columns AS c
    WHERE OBJECT_NAME(c.OBJECT_ID) = @tableName

    OPEN db_cursor   
    FETCH NEXT FROM db_cursor INTO @name   

    WHILE @@FETCH_STATUS = 0   
    BEGIN   
           SET @whereClause = @whereClause + ' OR '  + @name + ' >= 0'
           FETCH NEXT FROM db_cursor INTO @name   
    END   

    CLOSE db_cursor   
    DEALLOCATE db_cursor

    EXEC ('SELECT * FROM ' + @tableName + @whereClause)