我现在想动态创建一个存储过程,然后调用它,请问这样对吗?不对应该如何改呢?
//想数据表StudentInfo中插入记录
procedure Button1Click(Sender: TObject);
var
  strSQL : string;
  str1,str2 :string;
begin  
  str1      := Edit1.text;
  str2 := Edit2.text;
  strSQL := 'CREATE PROCEDURE ProcTest'
     + ' @str_name char(50),@str_profession char(50)'     
     + ' AS'
     + ' INSERT INTO StudentInfo([Name],[profession])'
     + ' VALUES(@str_name,@str_profession)
     + ' exec ProcTest str1,str2';  ADOQuery.Close;
  ADOQuery.SQL.Clear ;
  ADOQuery.ParamCheck := False;
  ADOQuery.SQL.Add(strSQL);
  ADOQuery.ExecSQL;end;

解决方案 »

  1.   

    多用户同时使用时,要先判断数据库中存储过程是否存在,不存在时才创建。为什么要动态创建存储过程?这样还不如在程序中直接ExecSQL。
      

  2.   

    调存储过程不要用adoquery,有专门的ADOStoredProc
      

  3.   

    我现在这段代码就是在程序里执行啊,但老是说我SQL语句有错。到底怎么回事?
      

  4.   

    strSQL := 'CREATE PROCEDURE ProcTest'
         + ' @str_name char(50),@str_profession char(50)'     
         + ' AS'
         + ' INSERT INTO StudentInfo([Name],[profession])'
         + ' VALUES(@str_name,@str_profession)
         + ' exec ProcTest ''' + str1 + ''',''' + str2 + ''';
      

  5.   

    to DebugXP(NULL) strSQL := 'CREATE PROCEDURE ProcTest'
         + ' @str_name char(50),@str_profession char(50)'     
         + ' AS'
         + ' INSERT INTO StudentInfo([Name],[profession])'
         + ' VALUES(@str_name,@str_profession) '//此處少一個單引號
         + ' exec ProcTest ''' + str1 + ''',''' + str2 + ''';
      

  6.   

    檢查存儲過程是否存在,可以用
    if not Exists(select * from sysobjects where xtype='P' and name='YourProductName')
    //然後做你的事
      

  7.   

    错误如下:Project RemoteII.exe raised exception class EOleException with message '无效的 SQL 句法:需要的符号:AS。'. Process stopped. Use Step or Run to continue.
      

  8.   

    还有个问题忘了告诉大家了,我是用的ACCESS数据库。我用ADOQuery动态创建了这个存储过程来完成几千条记录的插入。就出现这个错误。
      

  9.   

    ACCESS不是真正意义上的存储过程
      

  10.   

    怎么就是没人详细解答呢?我按各位的改了代码,但还是出现上面的错误啊。到底怎么回事?
    还是这个错误
    Project RemoteII.exe raised exception class EOleException with message '无效的 SQL 句法:需要的符号:AS。'. Process stopped. Use Step or Run to continue.
      

  11.   

    你想用T-SQL在Access创建存储过程?。。
      

  12.   

    檢查存儲過程是否存在,可以用
    if not Exists(select * from sysobjects where xtype='P' and name='YourProductName')
                                                 |
                                                 |
                                                 type就行吧为什么要用xtype?
      

  13.   

    TO DebugXP(NULL) ( ) :
    我用的是ACCESS数据库啊,现在我要一次向表中添加几千条数据,我应该如何做速度才快一点?我现在一条一条的添加,速度很慢,而且CPU资源耗用也很大,有时到100% ,不能用存储过程吗?那又有什么办法呢?
      

  14.   

    动态组合下面的sql
    insert into StudentInfo([Name],[profession])
    select 'A1' as [t1],'A2' as [t2] from StudentInfo
    union all select 'A3' as [t1],'A4' as [t2] from StudentInfo
    union all select 'A5' as [t1],'A6' as [t2] from StudentInfo
    ...
    union all select 'A1000' as [t1],'A1001' as [t2] from StudentInfo'
    然后
    adoQuery.execSql用这种方法添加。看看速度是否提升。
      

  15.   

    动态组合下面的sql
    insert into StudentInfo([Name],[profession])
    select 'A1' as [t1],'A2' as [t2] from StudentInfo
    union all select 'A3' as [t1],'A4' as [t2] from StudentInfo
    union all select 'A5' as [t1],'A6' as [t2] from StudentInfo
    ...
    union all select 'A1000' as [t1],'A1001' as [t2] from StudentInfo'
    然后
    adoQuery.execSql
    这个是什么意思啊?我的数据是存在一个变量中,所以要用循环才能取到每个数。
    也就是插入的数都是存在变量中,
      

  16.   

    to blitre(瘦死的老鼠比象大) 
    是不是你給我留了言?我求求你下次給我留言的時候,
    注明是哪個一個帖子好不好?我可是找了N久才找到這裡的呀  insert into StudentInfo([Name],[profession])
        select 'A3','A4' union
        select 'A5','A6'
    這個SQL語句的意思是向StudentInfo表中插入
    下面兩條記錄'A3','A4'
    'A5','A6'你可以加入很多筆,記得每個Select用union連接
      

  17.   

    我試過,在Access中好象不支持
    Insert into tablename(field1,field2)
      select '1','1' union
      select '2','2'所以用到AdoQuery的緩存更新
    //下面是緩存更新的例子
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I : Integer;
      dtStart, dtEnd: TDateTime;
    begin
      dtStart := Now;
      AdoQuery1.LockType := ltBatchOptimistic;
      AdoQuery1.DisableControls;
      //記錄數:10000
      for I := 0 to StrToIntDef(Edit1.text, -1) do
      begin
        AdoQuery1.Append;
        AdoQuery1.FieldByName('name').AsString := Edit1.Text + '_' + IntToStr(i);
        AdoQuery1.FieldByName('datebegin').AsDateTime := Now;
        AdoQuery1.FieldByName('dateend').AsDateTime := Now;
        AdoQuery1.Post;
      end;
      AdoQuery1.EnableControls;
      dtEnd := Now;
      Memo1.Lines.Add(FloatToStr((dtEnd - dtStart) / ( 1 / 24 / 3600)));
      //Result = 0.985000189393759end;procedure TForm1.Button2Click(Sender: TObject);
    var
      dtStart, dtEnd: TDateTime;
    begin
      dtStart := Now;
      AdoQuery1.UpdateBatch();
      dtEnd := Now;
      Memo1.Lines.Add(FloatToStr((dtEnd - dtStart) / ( 1 / 24 / 3600)));
      //Result =   9.13099993485957
    end;