$sql_query = str_replace("SELECT DISTINCT", "SELECT DISTINCT TOP 50", strtoupper($sql_query));
			}
		}
		elseif(GetDatabaseType()==1) //Oracle
		{
			//
		}
		elseif(GetDatabaseType()==4) //PostgreSQL
		{
			//
		}
	}
}
$num_rows=50;
$errstr="";
$rs=db_query_safe($sql_query,$conn,$errstr);
if(!$rs || !$sql_query)
{	
	if(GetDatabaseType()==1 && !$sql_query)
		$errstr="Query was empty";
	if(postvalue("sql")!="makesql")
	{
		$xt->assign("sql_error_block", true);
		$xt->assign("sql_error", $errstr);
	}
	else
		$result=$errstr;
}
else
{
	$flag=true;
	return;
}

if(postvalue("name")=="deletesql")
{
	if(postvalue("idsql"))
		db_exec("delete from ".AddTableWrappers("webreport_sql")."  where ".AddFieldWrappers("id")."=".postvalue("idsql"),$conn);
	echo "OK";
	exit();
}

if(postvalue("name")=="sqledit")
{
	$sqlcontent=postvalue("sqlcontent");
	$_SESSION["object_sql"]=$sqlcontent;
	$rs=db_query_safe($sqlcontent,$conn,$errstr);
	if(!$rs)
	{
		echo $errstr;
		exit();
	}
	if($_SESSION["idSQL"])
	{
		db_exec("update ".AddTableWrappers("webreport_sql")." set ".AddFieldWrappers("sqlname")."=".db_prepare_string(DecodeUTF8(postvalue("namesql"))).",".AddFieldWrappers("sqlcontent")."=".db_prepare_string($sqlcontent)." where ".AddFieldWrappers("id")."=".$_SESSION["idSQL"],$conn);
		db_exec("update ".AddTableWrappers("webreport_admin")." set ".AddFieldWrappers("tablename")."=".db_prepare_string(DecodeUTF8(postvalue("namesql")))." where ".AddFieldWrappers("tablename")."=".db_prepare_string($_SESSION["nameSQL"]),$conn);
		$_SESSION["nameSQL"]=DecodeUTF8(postvalue("namesql"));
	}
	else
	{
		$sname=DecodeUTF8(postvalue("namesql"));
		$prefix=0;