sql存储过程中 Where条件中的case when
SELECT[Fiscal_Period],left(Datename(MONTH,convert(datetime,[Close_Date])),3)AS[Month]...
SELECT
[Fiscal_Period],
left(Datename(MONTH,convert(datetime,[Close_Date])),3) AS [Month]
FROM T_SFDC_KK_UPDATE
Where
1 = 1
Case When @SALESNAME <> '' Then And Opportunity_Owner = @SALESNAME End
Case When @CHANNELPARNTNER <> '' Then And Primary_Channel_Partner = @CHANNELPARNTNER End
Case When @REPORTTYPE In('SAMS','Indirect') Then And Opportunity_Owner = @SALESNAME End
GROUP BY [Fiscal_Period],left(Datename(MONTH,convert(datetime,[Close_Date])),3)
ORDER BY charindex(left(Datename(MONTH,convert(datetime,[Close_Date])),3),'Nov,Dec,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct')
存储过程中含有3个参数 请问大神我这样写存储过程中的SQL对不对呢 展开
[Fiscal_Period],
left(Datename(MONTH,convert(datetime,[Close_Date])),3) AS [Month]
FROM T_SFDC_KK_UPDATE
Where
1 = 1
Case When @SALESNAME <> '' Then And Opportunity_Owner = @SALESNAME End
Case When @CHANNELPARNTNER <> '' Then And Primary_Channel_Partner = @CHANNELPARNTNER End
Case When @REPORTTYPE In('SAMS','Indirect') Then And Opportunity_Owner = @SALESNAME End
GROUP BY [Fiscal_Period],left(Datename(MONTH,convert(datetime,[Close_Date])),3)
ORDER BY charindex(left(Datename(MONTH,convert(datetime,[Close_Date])),3),'Nov,Dec,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct')
存储过程中含有3个参数 请问大神我这样写存储过程中的SQL对不对呢 展开
1个回答
2015-10-06 · 知道合伙人软件行家
关注
展开全部
Case When 只能简单的返回一个值,不能让你拼SQL条件
追问
那么如果要拼接SQL条件我应该怎么写呢
追答
你这种只能拼字符串
declare @sql varchar(4000)
set @sql='select .....'
if @SALESNAME ''
set @sql=@sql+' 你的条件'
一个个条件判断,再拼上去
最后
exec @sql
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询