Could really use your help!
I have a query:
Select *
from Customers
Where Customer_id in (001,002,003)
...you get the idea.
My problem is, if there is no record for customer_id 003 for example, no record is displayed. How can i display "003" and state that no record was found? I would prefer this than having no record display at all.
Thanking you in advance!
In SQL Server 2008+ you can use
SELECT V.Customer_id,
C.*
FROM (VALUES(1),
(2),
(3)) V(Customer_id)
LEFT JOIN Customers C
ON C.Customer_id = V.Customer_id
Non matching values will have NULL
in the C.Customer_id
column.
Usually for this sort of thing I have a function which takes a csv and returns a table for me. Then I do something like the following.
-- this is creating the temporary table which would normally be created by a function.
DECLARE @Temp TABLE (Customer_id int)
INSERT INTO @Temp(Customer_id)
SELECT 1
INSERT INTO @Temp(Customer_id)
SELECT 2
INSERT INTO @Temp(Customer_id)
SELECT 3
-- now do the select statement.
SELECT
T.Customer_id,
C.*
FROM
Customers C
RIGHT OUTER JOIN
@Temp T
ON
T.Customer_id = C.Customer_id
This will then give you results whereby if the C.Customer_id
is NULL
then it is not an Id
that exists in your Customer
table
. This does the same job as @MartinSmith did, but is supported by most SQL server versions.
In oracle you can do something like this:
with vals as
(SELECT '001' as val
from dual
union
select '002'
from dual
union
select '030' from dual)
select v.val, nvl(c.Customer_id, 'no Customer found') from vals v left join Customers c on c.Customer_id = v.val
What you would need to do is query against a list of the customer IDs you want to look up (including those that may not exist) and then left join this with your customers
table. Creating a temporary table of customer IDs that you want to query against is platform specific, but here is how I would do it in Oracle:
with customersToLookup as (
select '001' as customer_id from dual union all
select '002' as customer_id from dual union all
select '003' as customer_id from dual
)
select customersToLookup.customer_id,
customers.*
from customersToLookup
left join customers
on customers.customer_id = customersToLookup.customer_id;
It's very unusual, in my experience, to run a query like this: RDBMs aren't meant to be used in this way.
Probably not the best solution because of complexity and clearly not recommended for tables with a lot of rows, but.. I created 2 new temporary tables: #tempTab2(in which I saved the id's that have been sent) and #tempTabFinal (in which I saved the row coresponding to that ID if the ID exist, and a "not found message" if not).Here is a print screen to see if this is what you what to do, and the code (I used for test the 'questions' table, which has as columns: q_id and question - the actual question)
DECLARE @INSTR as VARCHAR(MAX)
SET @INSTR = '1,153,154,1555,111,'
DECLARE @SEPERATOR as VARCHAR(1)
DECLARE @SP INT
DECLARE @VALUE VARCHAR(1000)
SET @SEPERATOR = ','
CREATE TABLE #tempTab2 (id int not null,question varchar(60))
CREATE TABLE #tempTabFinal(id varchar(40) not null,question varchar(60))
WHILE PATINDEX('%' + @SEPERATOR + '%', @INSTR ) <> 0
BEGIN
SELECT @SP = PATINDEX('%' + @SEPERATOR + '%',@INSTR)
SELECT @VALUE = LEFT(@INSTR , @SP - 1)
SELECT @INSTR = STUFF(@INSTR, 1, @SP, '')
INSERT INTO #tempTab2 (id,question) VALUES (@VALUE,'question')
END
DECLARE @INDEX INT
SET @INDEX = 1
WHILE (@INDEX <=5)
BEGIN
IF((Select COUNT(*) FROM questions Where q_id= (SELECT TOP 1 id from #tempTab2)) >0 )
BEGIN
DECLARE @TEMP as varchar (80)
SET @TEMP = (Select question from questions where q_id = (SELECT TOP 1 id from #tempTab2))
DECLARE @ID INT
SET @ID = (SELECT TOP 1 id from #tempTab2)
Insert into #tempTabFinal Values(@ID,@Temp)
END
ELSE
BEGIN
DECLARE @NOTFND as Varchar(20)
SET @NOTFND = (SELECT TOP 1 id from #tempTab2)
Insert into #tempTabFinal Values('id ' +@NOTFND + ' Not found ','Not found')
END
DELETE TOP (1) from #tempTab2
SET @INDEX = @INDEX + 1
END
Select * from #tempTabFinal
DROP TABLE #tempTab2
DROP TABLE #tempTabFinal
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.