关于存储过程中的情况

发布于 2024-08-13 06:58:58 字数 1655 浏览 3 评论 0原文

我有以下查询。

 declare @Prm_CourseId int
declare @Prm_SpecializationId int
set @Prm_CourseId=5
set @Prm_SpecializationId=0
declare @WhrStr varchar(500)


set @WhrStr =  case @Prm_CourseId
                when 0 then
                    'e.CourseId is null or e.CourseId is not  null'
                when -1 then
                    'e.CourseId is null or e.CourseId is not  null'
                when isnull(@Prm_CourseId,0)  then
                     'e.CourseId is null or e.CourseId is not  null'
                else
                     'e.CourseId= '+Convert(varchar,@Prm_CourseId)

                 end
set @WhrStr  = case @Prm_SpecializationId
                when 0 then
                    'e.SpecializationId is null or e.SpecializationId is not  null'
                when -1 then
                    'e.SpecializationId is null or e.SpecializationId is not  null'
                when isnull(@Prm_SpecializationId,0)  then
                     'e.SpecializationId is null or e.SpecializationId is not  null'
                else
                     'e.SpecializationId= '+Convert(varchar,@ Prm_SpecializationId)
                end
print @WhrStr
    exec(
            'select f.EnquiryID,
            e.[Name],
            f.AttendedBy,
            f.Remarks,
            f.CreatedDate
            from STD_FollowUp f
                inner join
                    STD_Enquiry e
                     on f.EnquiryId=e.EnquiryId
            where'+' '+@WhrStr
    )

这里的问题是我想在 @WhrStr 中的第一个和第二个 case 语句中获得结果。但是 WhrStr 中传入的值是“e.SpecializationId 为 null 或 e.SpecializationId 不为 null”意味着第一个被第二个覆盖。无论如何将第二个附加到第一个。

I have following query.

 declare @Prm_CourseId int
declare @Prm_SpecializationId int
set @Prm_CourseId=5
set @Prm_SpecializationId=0
declare @WhrStr varchar(500)


set @WhrStr =  case @Prm_CourseId
                when 0 then
                    'e.CourseId is null or e.CourseId is not  null'
                when -1 then
                    'e.CourseId is null or e.CourseId is not  null'
                when isnull(@Prm_CourseId,0)  then
                     'e.CourseId is null or e.CourseId is not  null'
                else
                     'e.CourseId= '+Convert(varchar,@Prm_CourseId)

                 end
set @WhrStr  = case @Prm_SpecializationId
                when 0 then
                    'e.SpecializationId is null or e.SpecializationId is not  null'
                when -1 then
                    'e.SpecializationId is null or e.SpecializationId is not  null'
                when isnull(@Prm_SpecializationId,0)  then
                     'e.SpecializationId is null or e.SpecializationId is not  null'
                else
                     'e.SpecializationId= '+Convert(varchar,@ Prm_SpecializationId)
                end
print @WhrStr
    exec(
            'select f.EnquiryID,
            e.[Name],
            f.AttendedBy,
            f.Remarks,
            f.CreatedDate
            from STD_FollowUp f
                inner join
                    STD_Enquiry e
                     on f.EnquiryId=e.EnquiryId
            where'+' '+@WhrStr
    )

here the problem is i want to get result in 1st and 2nd case statement in @WhrStr .But the value coming in WhrStr is 'e.SpecializationId is null or e.SpecializationId is not null' means 1st is overwritten by second.Is there anyway to append second to first.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-08-20 06:58:58

这在几个层面上都行不通。

  • 您无法将字符串附加到查询中。它要么全部是动态的,要么全部不是。
  • CASE WHEN 位需要位于字符串分隔符“...THEN”中
    '(e.CourseId 为 null 或 e.CourseId 不为 null)'...

  • “e.CourseId is null or e.CourseId is not null”是相同的“总是给我数据”< /p>“e.CourseId

您需要的是:

select f.EnquiryID,
    e.[Name],
    f.AttendedBy,
    f.Remarks,
    f.CreatedDate
from STD_FollowUp f
            inner join
                    STD_Enquiry e
                    on f.EnquiryId=e.EnquiryId

This won't work in several levels.

  • You can't append a string to a query. It's either all dynamic or all not.
  • The CASE WHEN bit needs to be in string delimiters "...THEN
    '(e.CourseId is null or e.CourseId is not null)'...

  • "e.CourseId is null or e.CourseId is not null" is the same "always give me data"

All you need is:

select f.EnquiryID,
    e.[Name],
    f.AttendedBy,
    f.Remarks,
    f.CreatedDate
from STD_FollowUp f
            inner join
                    STD_Enquiry e
                    on f.EnquiryId=e.EnquiryId
北方的韩爷 2024-08-20 06:58:58
if (isnull(@Prm_CourseId, 0) = 0 or isnull(@Prm_CourseID, 0) = -1)
    set @Prm_CourseId = null

if (isnull(@Prm_SpecializationId, 0) = 0 or isnull(@Prm_SpecializationId, 0) = -1)
    set @Prm_SpecializationId = null

select f.EnquiryID,
e.[Name],
f.AttendedBy,
f.Remarks,
f.CreatedDate
from STD_FollowUp f
inner join
STD_Enquiry e
on f.EnquiryId=e.EnquiryId
where (@Prm_CourseId is null or e.CourseId = @Prm_CourseId)
or (@Prm_SpecializationId is null or e.SpecializationId = @Prm_SpecializationId)
if (isnull(@Prm_CourseId, 0) = 0 or isnull(@Prm_CourseID, 0) = -1)
    set @Prm_CourseId = null

if (isnull(@Prm_SpecializationId, 0) = 0 or isnull(@Prm_SpecializationId, 0) = -1)
    set @Prm_SpecializationId = null

select f.EnquiryID,
e.[Name],
f.AttendedBy,
f.Remarks,
f.CreatedDate
from STD_FollowUp f
inner join
STD_Enquiry e
on f.EnquiryId=e.EnquiryId
where (@Prm_CourseId is null or e.CourseId = @Prm_CourseId)
or (@Prm_SpecializationId is null or e.SpecializationId = @Prm_SpecializationId)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文