Triggers and Statistics in SQL Server
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 4.5k
- Forks
- 884
- PR merge metrics
- No merged PRs in 30d
Description
I am working on a project where database has both triggers and statistics and when I try to drop trigger and after that drop statistics I got an error that I don't have permission or statistcs does not exists.
I am using "sa" user and the same sql script works when running it on SQL Express or other DBMS.
Does anybody have any suggestion?
I also created a "Base migration" that extends AbstractMigration to implement the methods to check for statistics
<?php
declare(strict_types=1);
namespace App\Infrastructure\Database\Migrations;
use Phinx\Migration\AbstractMigration;
abstract class BaseMigration extends AbstractMigration
{
public function getStatistics(string $tableName, string $columnName)
{
return $this->query("SELECT
s.name AS statistics_name,
c.name AS column_name,
t.name AS table_name
FROM sys.stats AS s
JOIN sys.stats_columns AS sc ON s.stats_id = sc.stats_id AND s.object_id = sc.object_id
JOIN sys.columns AS c ON sc.object_id = c.object_id AND sc.column_id = c.column_id
JOIN sys.tables AS t ON s.object_id = t.object_id
WHERE
t.name = '{$tableName}' AND
c.name = '{$columnName}'")->fetchAll(\PDO::FETCH_OBJ);
}
/**
* Check if the informed table.column has statistics
* @param string $tableName
* @param string $columnName
* @return bool
*/
public function hasStatistic(string $tableName, string $columnName): bool
{
return count($this->getStatistics($tableName, $columnName)) > 0;
}
public function dropStatistic(string $tableName, string $columnName)
{
$return = true;
$statistics = $this->getStatistics($tableName, $columnName);
foreach ($statistics as $statistic) {
if ($statistic->table_name === $tableName && $statistic->column_name === $columnName) {
$this->query("DROP STATISTICS {$tableName}.{$columnName}")->execute();
}
}
return $return;
}
public function createStatistic(string $tableName, string $columnName)
{
return $this->query("CREATE STATISTICS {$columnName} ON {$tableName}({$columnName})")->execute();
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue provides a SQL Server reproduction and an App\Infrastructure\Database\Migrations\BaseMigration snippet, but names no Phinx source file or test. Start by reproducing the trigger and statistics operations on SQL Server and compare them with the reported SQL Express behavior; done means the expected migration behavior and required compatibility change are documented and covered by a test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100