SQL Server 查询编辑器 - 有警告要更改的行数的吗?

发布于 2024-08-27 13:29:07 字数 276 浏览 15 评论 0原文

我们使用的是 SQL Server 2000 查询分析器,我们遇到的一个问题是,当用户更新实时数据库时,他们偶尔会插入不正确/不(!) where 子句。我知道,这不太好,但它确实发生了。

是否有任何编辑器会警告可能更改的行数(如果可能的话),甚至是否有一种配置编辑器的方法(如果它连接到某个数据库),以在运行查询之前提示确认?

在运行错误查询的情况下,我们有办法恢复数据,但这需要时间,我只是看看是否有任何方法可以捕获错误,或者至少给用户第二次机会。

提前致谢。

We're using SQL Server 2000 Query Analyser, and one issue we have is that very occasionally, when a user is updating our live database, they insert the incorrect/no(!) where clause. I know, not good, but it happens.

Are there any editors that will warn of the number of rows that might be changed (if that is even possible) or even a way to configure an editor, if it is connected to a certain database, to prompt for confirmation before the query is run?

We have a way to recover our data in the cases where we run an incorrect query, but it takes time, and I'm just seeing if there are any ways to catch the error, or at least give the user a second chance.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独守阴晴ぅ圆缺 2024-09-03 13:29:07

唯一的方法是不让用户使用SQL Server 2000 查询分析器编辑数据!但是您必须编写一个应用程序并控制数据,然后您可以根据需要发出警告。

除此之外,您可以向每个表添加触发器并设置某种排序限制,如果受影响的行大于 X,您可以在其中发出 ROLLBACK。您甚至可以使用类似 SUSER_NAME() 将限制应用于某些用户。

示例触发器:

CREATE TRIGGER Trigger_YourTable ON YourTable
FOR INSERT, UPDATE, DELETE
AS
DECLARE @Limit    int
DECLARE @Message  varchar(100)
SET @Limit=5
SET @Message='ERROR, Not Permitted to alter more than '+CONVERT(varchar(5),@Limit)+' rows at any one time.'

IF SUSER_NAME() !='AwesomeSA'
BEGIN
    IF @Limit<(SELECT COUNT(*) FROM INSERTED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
    ELSE IF @Limit<(SELECT COUNT(*) FROM DELETED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
END

GO

要自动生成所有触发器脚本,请运行此脚本(实际上并不将它们添加到数据库中,只是生成您应该编辑然后运行的文本脚本):

DECLARE @SQL varchar(8000)
SET @SQL='PRINT ''CREATE TRIGGER [''+REPLACE(REPLACE(REPLACE(''Trigger_?'',''['',''''),'']'',''''),''.'',''_'')+''] ON ?''; PRINT ''FOR INSERT, UPDATE, DELETE
AS
DECLARE @Limit    int
DECLARE @Message  varchar(50)
SET @Limit=5
SET @Message=''''ERROR, Not Permitted to alter more than ''''+CONVERT(varchar(5),@Limit)+'''' rows at any one time.''''

IF SUSER_NAME() !=''''AwesomeSA''''
BEGIN
    IF @Limit<(SELECT COUNT(*) FROM INSERTED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
    ELSE IF @Limit<(SELECT COUNT(*) FROM DELETED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
END
GO'''
EXEC sp_msforeachtable @SQL

除非您以以下身份登录,否则所有表的受影响行数限制将为 5用户“AwesomeSA”。上面的脚本将生成代码,而不是实际创建触发器。您可以编辑此脚本的输出,设置良好的行限制、用户等,然后运行该脚本,然后将创建触发器。

The only way is to not to let user edit data using SQL Server 2000 Query Analyser! But then you'll have to write an application and control the data, and you can then issue warnings as necessary.

Short of that, you could add triggers to every table and set some sort limit where you issue a ROLLBACK if the rows affected is greater than X. You could even use something like SUSER_NAME() to apply the limit to certain users.

sample trigger:

CREATE TRIGGER Trigger_YourTable ON YourTable
FOR INSERT, UPDATE, DELETE
AS
DECLARE @Limit    int
DECLARE @Message  varchar(100)
SET @Limit=5
SET @Message='ERROR, Not Permitted to alter more than '+CONVERT(varchar(5),@Limit)+' rows at any one time.'

IF SUSER_NAME() !='AwesomeSA'
BEGIN
    IF @Limit<(SELECT COUNT(*) FROM INSERTED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
    ELSE IF @Limit<(SELECT COUNT(*) FROM DELETED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
END

GO

to automatically generate all the triggers scripts run this (does not actually add them in the database, just produce the text script which you should edit and then run):

DECLARE @SQL varchar(8000)
SET @SQL='PRINT ''CREATE TRIGGER [''+REPLACE(REPLACE(REPLACE(''Trigger_?'',''['',''''),'']'',''''),''.'',''_'')+''] ON ?''; PRINT ''FOR INSERT, UPDATE, DELETE
AS
DECLARE @Limit    int
DECLARE @Message  varchar(50)
SET @Limit=5
SET @Message=''''ERROR, Not Permitted to alter more than ''''+CONVERT(varchar(5),@Limit)+'''' rows at any one time.''''

IF SUSER_NAME() !=''''AwesomeSA''''
BEGIN
    IF @Limit<(SELECT COUNT(*) FROM INSERTED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
    ELSE IF @Limit<(SELECT COUNT(*) FROM DELETED)
    BEGIN
        ROLLBACK
        RAISERROR(@Message, 16, 1);
        RETURN
    END
END
GO'''
EXEC sp_msforeachtable @SQL

all tables will have an affected row limit of 5 unless you login as user "AwesomeSA". The above script will generate the code, not actually create the triggers. You can edit the output of this script, setting good row limits, users, etc. and then run that script and then the triggers will be created.

最后的乘客 2024-09-03 13:29:07

一种方法是创建一个模板,将语句包装在事务中,并在最后回滚。这样您就可以看到受影响的行并撤消。

如果您安装 SQL Server Management Studio 工具包,则单击“新建”时的默认行为(可配置) Query...' 是打开一个

BEGIN TRAN



ROLLBACK

其中包含的窗口。

One approach is to create a template that wraps statements in a transaction, and rollback at the end. That way you can see affected rows and undo.

If you install the SQL Server Management Studio Tools Pack, then the default behaviour (configurable) when clicking on 'New Query...' is to open a window with

BEGIN TRAN



ROLLBACK

in it.

余生再见 2024-09-03 13:29:07

为了采纳米奇·惠特的答案并进行扩展,我已经成功地使用了它。如果我要定期使用它,我会创建一个存储过程,它将获取预期的行数、from 语句和 where 语句作为输入,并使整个过程自动化。

begin tran

declare @err int
declare @cnt int

-- select total count of records to be deleted 
select @cnt = count(*)
  from dbo.table
 where delete_ind = 1    

-- show that in the results pane
select 'Count', @cnt

-- Delete it (note that the from and where statements are the same from count query)
Delete
  from dbo.table
 where delete_ind = 1

select @err = @@error

if @err <> 0  --check to see if query failed
BEGIN
   -- Return 99 to the calling program to indicate failure.
   raiserror('An error occurred deleting from dbo.table.',16,1)
   ROLLBACK
END     
ELSE if @cnt = 1168730  --yes this is a hard coded expected row count
BEGIN
   raiserror('Delete processed %i rows from dbo.table.',1,1,@cnt)
   COMMIT
END

To take Mitch Wheat's answer and expand I've used this with success. If I was going to use this regularly I'd make a stored procedure that would take the expected row count, from statement and where statement as input and automate the entire thing.

begin tran

declare @err int
declare @cnt int

-- select total count of records to be deleted 
select @cnt = count(*)
  from dbo.table
 where delete_ind = 1    

-- show that in the results pane
select 'Count', @cnt

-- Delete it (note that the from and where statements are the same from count query)
Delete
  from dbo.table
 where delete_ind = 1

select @err = @@error

if @err <> 0  --check to see if query failed
BEGIN
   -- Return 99 to the calling program to indicate failure.
   raiserror('An error occurred deleting from dbo.table.',16,1)
   ROLLBACK
END     
ELSE if @cnt = 1168730  --yes this is a hard coded expected row count
BEGIN
   raiserror('Delete processed %i rows from dbo.table.',1,1,@cnt)
   COMMIT
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文