存储过程错误“模糊列”

时间:2010-11-13 13:08:23

标签: sql

嘿家伙我在asp.net中创建一个搜索表单的存储过程即时获取错误,代码如下:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[sp_searchcustomervendordetails] 
 @customervendortype varchar(30)=Null,
    @customervendorid   varchar(30)=Null,
    @customervendorname varchar(30)=Null,
    @state              varchar(30)=Null,
    @city               varchar(30)=Null
AS
BEGIN
 SET NOCOUNT ON;
--if @customervendortype is not null and len(@customervendortype)=0 set @customervendortype = null
--if @customervendorid is not null and len(@cuatomervendorid)=0 set @customervendorid = null
--if @customervendorname is not null and len(@customervendorname)=0 set @customervendorname = null
--if @city is not null and len(@city)=0 set @city = null
--if @state is not null and len(@state)=0 set @state = null

    -- Insert statements for procedure here
SELECT     CustomerVendorDetails.customervendorid AS CustomerVendorID,CustomerVendorAddressDetails.customervendorname, CustomerVendorAddressDetails.doorno, CustomerVendorAddressDetails.street, 
           CustomerVendorAddressDetails.city, CustomerVendorAddressDetails.state, CustomerVendorAddressDetails.country, 
           CustomerVendorAddressDetails.pincode,  CustomerVendorDetails.decidingauthority, 
           CustomerVendorDetails.landlineno1, CustomerVendorDetails.landlineno2, CustomerVendorDetails.faxno, ContactPersonDetails.contactno, 
           ContactPersonDetails.designation
FROM       CustomerVendorDetails INNER JOIN
           CustomerVendorAddressDetails ON CustomerVendorDetails.customervendorid = CustomerVendorAddressDetails.customervendorid INNER JOIN
           ContactPersonDetails ON CustomerVendorAddressDetails.customervendorid = ContactPersonDetails.customervendorid
WHERE      (@customervendortype is null or customervendortype like @customervendortype)
        or (@customervendorid is null or customervendorid like @customervendorid) 
        or (@customervendorname is null or customervendorname like @customervendorname)
        or (city is null or city like @city)
        or (state is null or state like @city)
END

我收到此错误

Msg 209, Level 16, State 1, Procedure sp_searchcustomervendordetails, Line 34
Ambiguous column name 'customervendorid'.
Msg 209, Level 16, State 1, Procedure sp_searchcustomervendordetails, Line 35
Ambiguous column name 'customervendorname'

可以帮助我吗

4 个答案:

答案 0 :(得分:1)

通常,当您执行将多个表连接在一起且至少有一个具有相同名称的列的选择时,您将收到“模糊列名称”错误,然后引用该列而不为其添加前缀桌子。

所以我的情况我会假设customervendorname列出现在多个表格中(可能是CustomerVendorDetailsCustomerVendorAddressDetails?)所以当你在where子句中使用它时我需要在它前面加上表名。事实上,我已经看到你已经在要选择的列列表中完成了这项工作;你只需要在整个声明中做同样的事情。

因此

WHERE (@customervendortype is null or
       customervendortype like @customervendortype)

需要成为

WHERE (@customervendortype is null or
       CustomerVendorAddressDetails.customervendortype like @customervendortype)

以及其他约束的类似变化。

答案 1 :(得分:0)

问题必须出在这一部分:

or (@customervendorid is null or customervendorid like @customervendorid) 
        or (@customervendorname is null or customervendorname like @customervendorname)

您应该检查CustomerVendorAddressDetails和ContactPersonDetails表中是否有customervendorid和customervendorname列

答案 2 :(得分:0)

(@ customervendorid为null或customervendorid,如@customervendorid)或(@customervendorname为null或customervendorname,如@customervendorname)

检查这两行..

请为您正在使用的客户和其他表格发布您的表格结构。很容易猜出错误

答案 3 :(得分:0)

通过CustomerVendorDetails.customervendorid替换customervendorid,并通过CustomerVendorAddressDetails.customervendorname替换customervendorname,其中条件将起作用。