TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
ahmed elbarbary
597
1.6k
282.4k
how to create query based on optional field on temp table ?
Jan 22 2020 7:41 AM
problem
how to create query based on optional field on temp table ?
I work on sql server 2012
I have temp table #tempsupplier have two fields
SupplierId mandatory have value
TradeCode optinal have value may be have or may be not have value
so i need
if(tradecode have value
then
execute
)
do specific
select
select
p.PartID, p.PartNumber,m.SupplierId,m.TradeCode
from
#tempsupplier m
inner
join
#parts p
on
p.SupplierId=m.SupplierId
where
not
exists (
select
1
from
#TradeCodes t
where
t.TradeCode=m.TradeCode)
if (tradecode
not
have value meaning
is
null
then
execute
depend
on
supplier
only
)
select
p.PartID, p.PartNumber,m.SupplierId,m.TradeCode
from
#tempsupplier m
inner
join
#parts p
on
p.SupplierId=m.SupplierId
where
not
exists (
select
1
from
#TradeCodes t
where
t.PartID=p.PartID )
details data
as
below
create
table
#tempsupplier
(
SupplierId
int
,
TradeCode
int
)
insert
into
#tempsupplier(SupplierId,TradeCode)
values
(10,15),
(11,
null
)
create
table
#parts
(
PartID
int
,
PartNumber nvarchar(200),
SupplierId
int
,
TradeCode
int
)
insert
into
#parts
(PartID,PartNumber,SupplierId)
values
(100,
'silicon'
,10),
(200,
'motherboard'
,10),
(300,
'iron'
,10),
(400,
'plastic'
,10),
(500,
'Car'
,11),
(600,
'Bicycle'
,11),
(700,
'plan'
,11)
create
table
#TradeCodes
(
PartID
int
,
TradeCode
int
)
insert
into
#TradeCodes
(PartID,TradeCode)
values
(300,10),
(400,10),
(500,20)
Reply
Answers (
1
)
How to make group by Revision_ID and when repeated display l
Permission is deprecated only for compatibility