我可以在单个命令中执行多个sqls吗?

时间:2015-06-23 05:24:11

标签: oracle

使用mssql sqlcommand允许在单个sql命令文本中执行多个查询。

例如

strSQL = @"
DECLARE @strCOMPCD VARCHAR(4) SET @strCOMPCD = '{2}'
DECLARE @strLANGCD VARCHAR(2) SET @strLANGCD = '{3}'
DECLARE @strTMYEAR VARCHAR(4) SET @strTMYEAR = '{4}'
DECLARE @strTMTERM VARCHAR(4) SET @strTMTERM = '{5}'

DECLARE @strFromDate VARCHAR(10) SET @strFromDate = @strTMYEAR + '-' + @strTMTERM + '-' + '01'
DECLARE @strToDate VARCHAR(10) SET @strToDate = CONVERT(VARCHAR(10), DATEADD(DAY, -1, DATEADD(MONTH, 1, @strFromDate)) ,120)

DECLARE @strFromDate1 VARCHAR(10) SET @strFromDate1 = @strTMYEAR + '-01-01'
DECLARE @strToDate1 VARCHAR(10) SET @strToDate1 = CONVERT(VARCHAR(10), DATEADD(DAY, -1, @strFromDate) ,120)

SELECT      
        A.*
        , H.OBTEXT
        , I.NAMEKO
        , ISNULL(B.USECNT, 0) AS USECNT
        , ISNULL(C.DAFCNT, 0) - ISNULL(D.USECNT, 0) AS CARCNT
        , ISNULL(E.DAFCNT, 0) - ISNULL(B.USECNT, 0) AS NUSCNT
        , ISNULL(F.WRKCNT, 0) AS WRKCNT
        {7}
...
INSERT ...

DELETE ...
";

但是,在oracle中,我无法在单个命令中执行多个查询。

有什么解决方案吗? (不拆分每个oracle sql命令)

        using (OracleConnection connection = new OracleConnection(connString))
        {
            try
            {
                string strSQL = @"
DECLARE 
:VAR1  VARCHAR(4)
:VAR1  VARCHAR(4)

SELECT FIELD1 INTO :VAR1 
  FROM test1
 WHERE COMPCD = 'XXXX' 
   AND EMPNUM = 'YYYY'  

SELECT TEST2 INTO :VAR2 
  FROM test2
 WHERE COMPCD = 'XXXX' 
   AND EMPNUM = 'YYYY'  
   ... 

 IF :VAR1 <> 'XXXX' 
    :VAR1 = 'NEWVAL'


 SELECT  FIELD1, FIELD2, 
         FUNC_A(:VAR1),
         FUNC_b(:VAR2)
    FROM TEST3

1 个答案:

答案 0 :(得分:0)

您需要创建一个匿名块。示例如下:

DECLARE 
  VAR1 number;
  VAR2 number;
BEGIN

  select FIELD1  
    into VAR1
    from test1
   where COMPCD = 'XXXX' 
     AND EMPNUM = 'YYYY';  

  select TEST2   
    into VAR2
    from test2
   where COMPCD = 'XXXX' 
     AND EMPNUM = 'YYYY';     

  IF VAR2 > 0 THEN
    dbms_output.put_line('2: '|| to_char(VAR2)); 
    var2 := 64;
  END IF; 
END;