1

我希望它在执行时显示变量之一或所有变量或变量 2。我该怎么做?

    declare @sql varchar(8000)
declare @sql_where varchar(8000)

declare @ww varchar(10)
declare @plant varchar(10)
declare @plnr_id varchar(10)

set @plant = 'CS58'
set @ww = '201240'  
set @plnr_id = ''

set @sql_where = ''
set @sql = 'select * from  rout_sub_doi '

if len(@plant) > 0
    set @sql_where = 'where plant IN('''+ @plant +''') '

if len(@ww) > 0
    set @sql_where = @sql_where + 'AND sys_intel_ww IN ('''+  @ww +''') '

if len(@plnr_id) > 0 

    set @sql_where = @sql_where + 'AND  plnr_id IN (''' + @plnr_id + ''') '


print @sql
print @sql_where

exec (@sql + @sql_where )       
4

1 回答 1

0

试试这个代码:

SET SERVEROUTPUT ON
declare 
  vsql varchar(8000);
  sql_where varchar(8000);
  ww varchar(10);
  plant varchar(10);
  plnr_id varchar(10);
begin
  plant := 'CS58';
  ww := '201240';  
  plnr_id := '';
  sql_where := '';
  vsql := 'select * from  rout_sub_doi ';

  if (length(plant) > 0)
  then
    sql_where := 'where plant IN('''||plant||''') ';
  end if;

  if (length(ww) > 0)
  then
    sql_where := sql_where ||'AND sys_intel_ww IN ('''||ww||''')';
  end if;

  if (length(plnr_id) > 0 )
  then
    sql_where := sql_where + 'AND  plnr_id IN ('''||plnr_id||''') ';
  end if;

  dbms_output.put_line(vsql);  
  dbms_output.put_line(sql_where);  

  vsql:=vsql+vsql_where;

  execute immediate vsql;    
end;
/
于 2012-10-16T16:57:48.510 回答