sql 表变量与条件变量同是使用,语句写法
declare @ls table (kmdm varchar(20,no int identity PRIMARY key)
declare @cc varchar(20)
insert into @ls1( kmdm ) select kmdm from kmbm where kmdm>'415' order by kmdm
set @cc='''415'',''4154'''
select * from @ls1 where kmdm in (@cc)
以上最后一条 in (@cc) 不起作用,而下面语句报@ls未声明,试问如何写正确(两个变量同时用)
declare @ls table (kmdm varchar(20,no int identity PRIMARY key)
declare @cc varchar(20),@sql varchar(100)
insert into @ls1( kmdm ) select kmdm from kmbm where kmdm>'415' order by kmdm
set @cc='''415'',''4154'''
set @sql='select * from '+@ls+'where kmdm in ('+@cc+')'
exec(@sql)
------解决方案--------------------我怎么觉得我给第一个你你跑去搞第二个了?你的定义都写错了。用这个,这个是改你第一个的。
declare @ls1 table (kmdm varchar(20),no int identity PRIMARY key)
declare @cc varchar(20)
insert into @ls1( kmdm ) select kmdm from kmbm where kmdm>'415' order by kmdm
set @cc='''415'',''4154'''
select * from @ls1 where kmdm in (@cc)
------解决方案--------------------我没这样说,我只说外部变量(表变量也是变量)对于EXEC内部过程不可见,通过过EXEC实现你的需求行不通。
要么改变变量改临时表,要么用like或charndex。
declare @cc varchar(20)
declare @ls1 table (kmdm varchar(20),no int identity PRIMARY key)
insert into @ls1( kmdm ) select '415'
insert into @ls1( kmdm ) select '4154'
set @cc=',415,4154,'
select * from @ls1 where @cc like '%,'+kmdm+',%'