简体   繁体   English

插入新记录时是否触发(可能是存储过程)替换文本?

[英]Trigger (possibly stored procedure) to replace text when a new record is inserted?

I am running into a snag. 我遇到了障碍。 An application we've developed allows users to create links to files, but the links are being created relative to the server the SQL database resides on, not relative to the user. 我们开发的应用程序允许用户创建文件链接,但是链接是相对于SQL数据库所在的服务器而不是相对于用户创建的。

This is some pseudocode of what I need to happen. 这是我需要发生的一些伪代码。

when dbo.File_.Link_to_File has a prefix of '\\dc\App Share'
replace '\\dc\App Share' with 'Z:'

Essentially the file links are being stored relative to the server ( \\\\dc\\App Share ), but I have mapped this as a network drive (Z:) on all of the users' computers. 本质上,文件链接是相对于服务器( \\\\dc\\App Share )存储的,但是我已将其映射为所有用户计算机上的网络驱动器(Z :)。 I need a trigger that will replace the server path prefix with Z: 我需要一个触发器,该触发器将服务器路径前缀替换为Z:

Keep in mind this is the prefix (path) to a file ( \\\\dc\\App Share\\myfolder\\myfile.txt ), so the code needs to find just this prefix and replace it. 请记住,这是文件( \\\\dc\\App Share\\myfolder\\myfile.txt )的前缀(路径),因此代码只需查找此前缀并替换它即可。

This is running on SQL Server 2008 R2. 它在SQL Server 2008 R2上运行。

Any help is appreciated. 任何帮助表示赞赏。 Thanks much! 非常感谢!

I don't know why companies insist on using transient mapped drives instead of UNC paths, but this should work... 我不知道为什么公司坚持使用瞬态映射驱动器而不是UNC路径,但这应该可行。

UPDATE
    dbo.File_
SET
    Link_to_File = 'Z:' + SUBSTRING(Link_to_File, 15, LEN(Link_to_File) - 14)
WHERE
    Link_to_File LIKE '\\dc\App Share%'

By the way, if you're looking for a to make sure that the data is correct going forward, then you should put that logic in your application. 顺便说一句,如果您正在寻找a来确保以后的数据正确无误,那么您应该将该逻辑放入您的应用程序中。 Ideally, your application already uses stored procedures to update the data, so you can add code similar to the above to update the incoming parameter that holds the Link_to_File. 理想情况下,您的应用程序已经使用存储过程来更新数据,因此您可以添加类似于上面的代码来更新保存Link_to_File的传入参数。 Something like: 就像是:

IF (@Link_to_File LIKE '\\dc\App Share%')
    SET @Link_to_File = 'Z:' + SUBSTRING(Link_to_File, 15, LEN(Link_to_File) - 14)
CREATE TRIGGER File_Insert ON dbo.File
INSTEAD OF INSERT
AS
DECLARE @LinkColumn varchar(50)
SET @LinkColumn = (SELECT Link_to_File FROM INSERTED)
IF (LEFT(@LinkColumn, 1) <> 'Z')
BEGIN
     SELECT * INTO #Inserted FROM Inserted
     UPDATE #Inserted SET Link_to_File = REPLACE(Link_to_File,'\\dc\App Share','Z:')
     INSERT INTO dbo.File_ SELECT * FROM #Inserted
END
ELSE
     INSERT INTO dbo.File_SELECT * FROM Inserted

I don't have any way to test this. 我没有任何测试方法。 Replace database with the db name, table with your table name and urlcolumn with your column name 用db名称替换数据库,用表名称替换表,用列名称替换urlcolumn

CREATE TRIGGER trigger_name ON database.table
INSTEAD OF INSERT
AS
DECLARE @UriColumn varchar(150)
SET @UriColumn = (SELECT urlcolumn FROM INSERTED)
IF (@UriColumn like '\\dc\App Share' + '%')
BEGIN
     SET @UriColumn = replace(@UriColumn, '\\dc\App Share', 'Z:'
     SELECT * INTO #Inserted FROM Inserted
     UPDATE #Inserted SET urlcolumn = @UriColumn
     INSERT INTO table SELECT * FROM #Inserted
END
ELSE
     INSERT INTO table SELECT * FROM Inserted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM