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
NA
1.6k
278.8k
How to create dynamic sql based on optional values from temp
Jan 24 2020 3:58 AM
I have #TempMaster temp table have 3 fields
with optional values on 3 fields SourceGeneralTypeID AND StatusGeneralTypeID AND DailyLogId
meaning
may be SourceGeneralTypeID have values and other 2 fields not have value
may be StatusGeneralTypeID have values and other 2 fields not have value
may be DailyLogId have values and other 2 fields not have value
so my problem
How to write on statement after join on ? = ? where ?
problem how to write On ? = ? where ???????????????
and what i write on where
based on details above
what i have tried
select
M.MasterDataID,M.TrackingNumber,M.StatusDate
from
#TempMaster tmp
INNER
join
[MasterData] M
on
???=?????
where
??????
CREATE
TABLE
[MasterData](
[MasterDataID] [
int
] IDENTITY(1,1)
NOT
NULL
,
[SourceGeneralTypeID] [
int
]
NULL
,
[StatusGeneralTypeID] [
int
]
NULL
,
[DailyLogId] [
int
]
NULL
,
CONSTRAINT
[PK_MasterData]
PRIMARY
KEY
CLUSTERED
(
[MasterDataID]
ASC
)
WITH
(PAD_INDEX =
OFF
, STATISTICS_NORECOMPUTE =
OFF
,
IGNORE_DUP_KEY =
OFF
, ALLOW_ROW_LOCKS =
ON
,
ALLOW_PAGE_LOCKS =
ON
)
ON
[
PRIMARY
]
)
ON
[
PRIMARY
]
create
table
#TempMaster(
SourceGeneralTypeID
int
,
StatusGeneralTypeID
int
,
DailyLogId
int
)
insert
into
#TempMaster
(SourceGeneralTypeID,StatusGeneralTypeID,DailyLogId)
values
(
Null
,10,20),
(2,
Null
,30),
(2,30,
Null
)
insert
into
[MasterData]
([SourceGeneralTypeID],[StatusGeneralTypeID],[DailyLogId])
values
(2,30,20),
(2,30,30),
(2,30,10)
Reply
Answers (
5
)
Incorrect syntax near end
How to use out parameter in SQL server stored procedure