更改 Sql Server 2005 中的触发器

发布于 2024-07-07 17:14:52 字数 943 浏览 11 评论 0原文

我需要更改 sql server 2005 中的触发器,并且我想使用表适配器和 sql 语句而不是存储过程来完成此操作。 我知道我可以在查询浏览器中运行它并执行它,但我需要跨多个数据库部署它并在更新中使用表适配器。 这可能吗?

执行添加查询 -> 更新-> 粘贴以下代码-> 查询生成器查看它是否解析

print("USE [DataBaseName]
GO
/****** Object:  Trigger [dbo].[UpdateCurrentAddress]    Script Date: 10/30/2008 14:47:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UpdateCurrentAddress] ON [dbo].[PreviousAddresses] 
FOR INSERT, UPDATE, DELETE
AS
-- Check to see if there was an insert/update or a deletion.
IF (SELECT COUNT(*) FROM inserted) >= 1
BEGIN
    IF (SELECT CountryID FROM inserted) <> 181

...moar...");

错误... 不支持 USE [DataBaseName] SQL 构造或语句。

如果我删除顶部部分并从 ALTER TRIGGER 开始,

则不支持 ALTER TRIGGER SQL 构造或语句。

我对此还很陌生,并且不会感到惊讶,因为我要么以错误的方式处理这个问题,要么没有存储过程就不可能做到这一点。

编辑:是的,我正在 C# 中执行此操作。

谢谢。 我可以从 ALTER TRIGGER 开始这样做,这将完成工作。

I need to alter a trigger in sql server 2005 and I want to do it using a table adapter and a sql statement, not a stored proc. I know I could run this in the query browser and execute it but I need to deploy it across several databases and use a table adapter in the update. Is this possible?

Doing Add Query -> Update -> pasting the below code -> Query Builder to see if it parses

print("USE [DataBaseName]
GO
/****** Object:  Trigger [dbo].[UpdateCurrentAddress]    Script Date: 10/30/2008 14:47:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[UpdateCurrentAddress] ON [dbo].[PreviousAddresses] 
FOR INSERT, UPDATE, DELETE
AS
-- Check to see if there was an insert/update or a deletion.
IF (SELECT COUNT(*) FROM inserted) >= 1
BEGIN
    IF (SELECT CountryID FROM inserted) <> 181

...moar...");

error...
The USE [DataBaseName] SQL construct or statement is not supported.

and if i remove the top part and start at just ALTER TRIGGER

The ALTER TRIGGER SQL construct or statement is not supported.

I'm still fairly new to this and would not be surprised that either I'm either going about this the wrong way and/or it's impossible to do without a stored proc.

EDIT: Yeah I'm doing this in C#.

thanks. I can do it that way starting at ALTER TRIGGER, this will get the job done.

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

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

发布评论

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

评论(1

追星践月 2024-07-14 17:14:52

TableAdapter 需要一个结果集,而不是一个实际的查询。 要成功完成此操作,您将需要使用 SqlCommand 对象来实际执行更新。

如果您以前没有使用过,那么这很简单,首先声明您的连接,然后使用该连接创建命令。 创建命令后,将命令文本设置为等于您的脚本,然后您可以在打开连接后调用 ExecuteNonQuery() 方法来运行脚本。 如果你说你使用什么语言,我可以尝试提供一个例子。

编辑

这是一个 C# 示例,虽然快速但很脏,但它说明了要点。 注意,是凭记忆完成的,但应该是正确的。

using(SqlConnection oConnection = new SqlConnection("Yourconnectionhere"))
using(SqlCommand oCommand = new SqlCommand(oConnection))
{
    //Configure the command object
    oCommand.CommandText = "Your script here";

    //Open connectin and run script
    oConnection.Open();
    oCommand.ExecuteNonQuery();
    oConnection.Close();
}

The TableAdapter is expecting a resultset, not an actual query. To do this successfully you will need to use the SqlCommand object to actually peform your update.

If you have not used one before it is quite simple, first you declare your connection, then you create your command using the connection. Once the command is created set the commandtext equal to your script, and then you can call the ExecuteNonQuery() method to run the script after opening the connection. If you say what language you are using, I can try to provide an example.

Edit

Here is a C# example, quick and dirty but it gets the point across. NOTE, done from memory, but should be correct.

using(SqlConnection oConnection = new SqlConnection("Yourconnectionhere"))
using(SqlCommand oCommand = new SqlCommand(oConnection))
{
    //Configure the command object
    oCommand.CommandText = "Your script here";

    //Open connectin and run script
    oConnection.Open();
    oCommand.ExecuteNonQuery();
    oConnection.Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文