Stored procedure error "ambiguous column"
hey guys im creating a stored procedure for search form in the asp.net im getting error, code is given below
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, Custom开发者_运维知识库erVendorAddressDetails.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
im getting this error
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'
can any help me please
In general, you'll get an "ambiguous column name" error when you're performing a select that joins multiple tables together that have at least one column with the same name, and then you reference that column without prefixing it with the table.
So I your case I would assume that the customervendorname
column appears in more than one table (perhaps CustomerVendorDetails
and CustomerVendorAddressDetails
?) so when you use it in the where clause you'll need to prefix it with the table name. I see in fact that you've already done this in the list of columns to be selected; you just need to do the same throughout the statement.
Thus
WHERE (@customervendortype is null or
customervendortype like @customervendortype)
needs to become
WHERE (@customervendortype is null or
CustomerVendorAddressDetails.customervendortype like @customervendortype)
and possibly similar changes for the other constraints.
The problem must be in this part:
or (@customervendorid is null or customervendorid like @customervendorid)
or (@customervendorname is null or customervendorname like @customervendorname)
You should check if you have customervendorid and customervendorname columns in your CustomerVendorAddressDetails and ContactPersonDetails tables
(@customervendorid is null or customervendorid like @customervendorid) or (@customervendorname is null or customervendorname like @customervendorname)
check these two lines ..
please post your table structures for customer and other table you are using. It will be easy to guess the error
replace customervendorid by CustomerVendorDetails.customervendorid and customervendorname by CustomerVendorAddressDetails.customervendorname in where condition will work.
精彩评论