如果存在很多表

时间:2018-11-08 16:11:24

标签: if-statement sql-server-2016

这听起来可能很愚蠢,但是我想知道在执行操作之前是否有一种方法可以验证表列表是否存在。如果我有12张桌子要验证,是否必须重复12次“如果存在bla bla bla”? 我尝试做...

[code]

import json
import requests

auth_header = {'Authorization': 'a4fca847d3f203bd7306ef5d1857ba67a2b3d66aa455e06fac0ad0be87b9d226'}
webhook_url = 'https://hooks.zapier.com/hooks/catch/3950922/efka9n/'
api_url = 'https://api.safetyculture.io/audits/'
audit_id = input['audit_id']
audit_doc = requests.get(api_url + audit_id, headers=auth_header).json()
failed_items = []
audit_author = audit_doc['audit_data']['authorship']['author']
conducted_on = audit_doc['audit_data']['date_completed']
conducted_on = conducted_on[:conducted_on.index('T')]
audit_title = audit_doc['template_data']['metadata']['name']
for item in audit_doc['items']:
    if item.get('responses') and item['responses'].get('failed') == True:
        label = item.get('label')
        if label is None:
            label = 'no_label'
        responses = item['responses']
        response_label = responses['selected'][0]['label']
        notes = responses.get('text')
        if notes is None:
            notes = ''
        failed_items.append({'label': label,
            'response_label': response_label,
            'conducted_on': conducted_on,
            'notes': notes,
            'author': audit_author
        })
for item in failed_items:
    r = requests.post(webhook_url, data = item)
    return response.json()
[/code]

但是它不起作用。有想法吗?

1 个答案:

答案 0 :(得分:0)

您的陈述将永远不会是正确的,因为表名不能是这些之一,而同时又不能是它们中的另一个。在这种情况下,可以使用OR代替AND,但这将检查这些表中是否至少有一个存在。您必须将12个EXISTS语句与具有AND条件的表的名称结合起来,或者选择表名称在列表中的行数,即:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'employee_id')
and EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'employee_address'
and EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'employee_division' )

IF ((SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME in ('employee_id', N'employee_address', N'employee_division' )) = 3)