- SQL code
USE [nature]GO/****** Object: StoredProcedure [dbo].[sc_yz] Script Date: 07/16/2012 09:40:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[sc_yz] @fun_table_name as varchar(5000), --参数 @bl int output, --数据使用量 @b_m_return varchar(100) output --返回表名ASBEGINdeclare @bm varchar(300),--表名@m int,--循环1@n int, --循环1@a int,--循环2@b int, --循环2@tj varchar(300),--条件@linshi varchar(300), --临时@sql nvarchar(4000), --sql@b_m_l_s varchar(300) --临时表名set @m=CHARINDEX(';',@fun_table_name) set @n=1 WHILE @m>0 BEGIN set @bm=substring(@fun_table_name,@n,@[email protected]) set @[email protected]+1 set @m=CHARINDEX(';',@fun_table_name,@n) set @a=CHARINDEX(',',@bm) set @b=1 WHILE @a>0 BEGIN set @linshi=substring(@bm,@b,@[email protected]) if @b=1 begin set @sql='select @bl = count(*) from [email protected] set @b_m_l_s = @linshi end else begin set @sql+=' [email protected] end exec sp_executesql @sql,[email protected] int Output ',@bl Output if(@bl>=1) begin set @b_m_return = @b_m_l_s end else begin set @b_m_return = '' end set @[email protected]+1 set @a=CHARINDEX(',',@bm,@b) print @sql END ENDEND
------解决方案--------------------
exec sp_executesql @sql,[email protected] int Output ',@bl Output
if(@bl>=1)
begin
set @b_m_return = @b_m_l_s
return --这里加个return试试
end