如果我在页面上有一个包含多个可选输入的表单:库存编号,日期,构建,制造商等。如何使按下提交时发生的SQL查询,基于填充的内容的函数在表格上。 IE浏览器。如果他们只选择一个建筑并单击提交,它只返回建筑物信息,但如果他们选择建筑物和日期,则相同的提交按钮仍然有效。基本上,无论使用何种组合,查询都可以使用单个提交按钮。它们只返回结果,而不是使用表单将数据输入数据库。
以下查询是我用来返回所有结果并显示它们的方法。包含的表格目前无法使用,我只是将其包括在内,希望它有所帮助。
<form action="dblog.html" id="frmMain" method="post" name="frmMain">
Building:
<select name="building" id="building">
~[tlist_sql;SELECT DISTINCT location_id, location_description FROM u_locations ORDER BY location_description]
<option value="~(location_id)" >~(location_description) </option>
[/tlist_sql]
</select><p>
Start Date:
<input type="text" name="startdate" id="datetimepicker2"/><p>
End Date:
<input type="text" name="enddate" id="datetimepicker5"/><p>
Inventory Number:
<input type="text" name="Inventory Number" id="invnum"/><p>
Manufacturer:
<input type="text" name="Manufacturer" id="manfac"/><p>
<input name="btnSubmit" value="Continue" type="submit">
</form>
<div class="box-round">
<table border="0" cellspacing="0" cellpadding="4">
<tr bgcolor="#f6f6f6">
<td class="bold">#</td>
<td class="bold">Inv Number</td>
<td class="bold">Date</td>
<td class="bold">PO</td>
<td class="bold">PO Date</td>
<td class="bold">Cost</td>
<td class="bold">Model</td>
<td class="bold">Vendor</td>
<td class="bold">Location</td>
</tr>
~[tlist_sql;
SELECT
uc.inventory_number,
uc.date_inventoried,
uc.po_number,
upo.po_date,
upoli.cost,
udesc.model,
uv.vendor_name,
uloc.location_description
from u_computers uc
left outer join u_departments ud on uc.department_id = ud.department_id
left outer join u_description udesc on uc.description_id = udesc.description_id
left outer join u_locations uloc on uc.location_id = uloc.location_id
left outer join u_po upo on uc.po_number = upo.po_number
left outer join u_po_line_item upoli on upo.po_number = upoli.po_number
left outer join u_vendors uv on uc.vendor_id = uv.vendor_id
order by uc.inventory_number ASC;alternatecolor]
<tr bgcolor="#edf3fe">
<td>~(count;-)</td>
<td>~(uc.inventory_number;t)</td>
<td>~(uc.date_inventoried;d)</td>
<td>~(uc.po_number;t)</td>
<td>~(upo.po_date;d)</td>
<td>~(dupoli.cost;t)</td>
<td>~(udesc.model;t)</td>
<td>~(uv.vendor_name;t)</td>
<td>~(uloc.location_description;t)</td>
</tr>
[/tlist_sql]
</table>
</div>
答案 0 :(得分:0)
如果我正确理解了这个问题;声明传递给SQL语句的值(未设置的值定义为 null ),并检查 null 的每个输入值作为WHERE子句的一部分,将返回结果为描述
下面是如何使用存储过程实现这一目标的示例,但是应该能够翻译以满足您的需求。
CREATE PROCEDURE <owner>.<procedure name>
@inventory_number INT = null,
@date DATETIME = null,
@building NVARCHAR(50) = null,
@manufacturer NVARCHAR(50) = null
AS
SELECT DISTINCT name, location, floors
FROM buildings
WHERE (inventory_number = @inventory_number OR @inventory_number = null)
AND ([Date] = @date OR @date = null)
AND (building = @building OR @building = null)
AND (manufacturer = @manufacturer OR @manufacturer = null);
我希望这会有所帮助。