Ver Mensaje Individual
  #2  
Antiguo 12-03-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola gorsan.

Te pongo un ejemplo desde el cliente usando TIBQuery y otro desde el servidor usando un stored procedure.

Cliente:
Código Delphi [-]
procedure TDataModule1.CreateTable(const TableName: string);
begin
 with TIBQuery.Create(nil) do
  try
    Close;
    Database:= IBDatabase1;
    Transaction:= IBTransaction1;
    SQL.Add('SELECT RDB$RELATION_NAME FROM RDB$RELATIONS');
    SQL.Add('WHERE RDB$VIEW_BLR IS NULL');
    SQL.Add('AND (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)');
    SQL.Add('AND RDB$RELATION_NAME = :PTABLE');
    ParamByName('PTABLE').AsString:= TableName;
    Open;
    if IsEmpty then
      with TIBQuery.Create(nil) do
      try
        Close;
        Database:= IBDatabase1;
        Transaction:= IBTransaction1;
        SQL.Add('CREATE TABLE '+ TableName + '(');
        SQL.Add('ID INTEGER, NOMBRE VARCHAR(30),');
        SQL.Add('etc, etc');
        SQL.Add(')');
        ExecSQL;
      finally
        Free;
      end
    else
      raise Exception.Create('Existe una tabla con ese nombre');
  finally
    Free;
    IBTransaction1.Commit;
  end;
end;

Servidor:
Código SQL [-]
SET TERM ^ ;

CREATE OR ALTER PROCEDURE SP_CREATE_TABLE (
    TABLENAME VARCHAR(100))
AS
DECLARE VARIABLE RESULT SMALLINT;
DECLARE VARIABLE SQL VARCHAR(300);
BEGIN
  SELECT COUNT(*)
  FROM RDB$RELATIONS
  WHERE RDB$VIEW_BLR IS NULL
  AND (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
  AND RDB$RELATION_NAME = TRIM(:TABLENAME) INTO RESULT;

  IF (RESULT = 0) THEN
  BEGIN
    SQL= 'CREATE TABLE' || ' ' || TRIM(TABLENAME) || '('
         || 'ID INTEGER, NOMBRE VARCHAR(30),'
         || 'etc, etc);';
    EXECUTE STATEMENT SQL;
  END
  SUSPEND;
END^

SET TERM ; ^

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita