日期:2014-05-18  浏览次数:20557 次

存储过程嵌套IF语句写法
declare @str varchar(20)
set @str = 'A0'

create table #Test(ph varchar(20),mq varchar(20))
insert into #Test (ph , mq) 

/*执行时提示 IF 附件有语法错误*/
if ISNUMERIC(@str) = 1
begin
  select ph , mq from aa
end

else

begin
  select ph , mq from bbb
end


------解决方案--------------------
SQL code

declare @str varchar(20) set @str = 'A0'

create table #Test(ph varchar(20),mq varchar(20))
insert into #Test (ph , mq)  
    select ph , mq from aa where ISNUMERIC(@str) = 1
    union all
    select ph , mq from bbb where ISNUMERIC(@str) <> 1

------解决方案--------------------
SQL code


declare @str varchar(20)
set @str = 'A0'

create table #Test(ph varchar(20),mq varchar(20))  

/*执行时提示 IF 附件有语法错误*/
if ISNUMERIC(@str) = 1
begin
  insert into #Test (ph , mq)
  select ph , mq from aa
end
else

begin
  insert into #Test (ph , mq)
  select ph , mq from bbb
end

------解决方案--------------------
IF语法没问题哇.
是其他出错吧.
探讨
十分感谢,我想根据IF语句 将不同的结果集插入 临时表.

下面还有很长的语句.想知道中间如何嵌入IF语句.

------解决方案--------------------
SQL code

insert into #Test (ph , mq)

------解决方案--------------------
探讨

SQL code

insert into #Test (ph , mq)


目测这句有问题,请看看 PH,MQ 有没有问题.

------解决方案--------------------
insert into ... select ... 这个中间不能用if
如果用if的话,就是
if
insert into ... select ...
if
insert into ... select ...

不能
insert into ...
if
select ...