更短的CASE ISNULL(qry,'')=''然后没有' ELSE'是'结束

时间:2015-06-12 15:56:39

标签: sql sql-server case isnull

是否有更短/更好的方法来编写此SQL语句?

编辑:内部sql select语句是占位符,实际语句更复杂。

P.S.: Is there an alternate way to do this with using json, since I am
dealing with nested dictionaries.

感觉非常好,因为它将select语句的结果与null进行比较,如果为null则设置为空字符串;只是为了检查它是否为空字符串,并将其设置为实际需要的字符:a' yes'或者没有'字符串。

2 个答案:

答案 0 :(得分:2)

这是一种更清洁的方法。

SELECT
    CASE WHEN
        (SELECT TOP 1 x FROM y) IS NULL
    THEN 'No'
    ELSE 'Yes'
    END AS BooleanValue

这会删除一个额外的命令,并且应该具有相同的输出。我刚刚在sql server 2012中测试过它。

答案 1 :(得分:1)

#connection is established
sql_insert_query = """ INSERT INTO output (sno,ip,ports,services)VALUES (%s,%s,%s,%%s)"""
last=cursor.lastrowid
sno=last
ip=self._firstname.value
for por in port_list:    #these two loops need to be corrected
    for ser in service_list:   #for 1st port_list item 1st service_list item shouls be accessed
        insert_tuple = (sno,ip,por,ser)
        result = cursor.execute(sql_insert_query,insert_tuple)