1

我不知道这个问题是否可以理解,也许这个问题很容易。

我将举个例子来说明我想要什么:

我有一个 .sql 文件,例如:

CREATE TABLE Instituição(
//stuff inside
GO

CREATE TABLE Tutor(
//stuff inside
GO

CREATE TABLE Funcao(
//stuff inside
GO

几乎有一堆类似的代码,我想要的第一件事是查找“CREATE TABLE (something)”,然后在另一个句子中替换这个(something),这就是:

IF OBJECT_ID(N'SQL_DB..(something)', N'U') IS NOT NULL 
DROP TABLE (something);
GO

要在 CREATE TABLE 之前添加,因此代码几乎会导致:

IF OBJECT_ID(N'SQL_DB..Instituição', N'U') IS NOT NULL 
DROP TABLE Instituição;
GO
CREATE TABLE Instituição(
//stuff inside
GO

IF OBJECT_ID(N'SQL_DB..Tutor', N'U') IS NOT NULL 
DROP TABLE Tutor;
GO
CREATE TABLE Tutor(
//stuff inside
GO

IF OBJECT_ID(N'SQL_DB..Funcao', N'U') IS NOT NULL 
DROP TABLE Funcao;
GO
CREATE TABLE Funcao(
//stuff inside
GO

当我查找时,正则表达式代码将是这样的:find:

CREATE TABLE {:q}

代替:

IF OBJECT_ID(N'SQL_DB..\1', N'U') IS NOT NULL\nDROP TABLE \1;\nGO\nCREATE TABLE \1

但它不起作用,我该如何正确地做到这一点?提前致谢。

4

1 回答 1

1

In notepad++ you can use this:

Find what:

CREATE TABLE (.*?)\(

Replace with:

IF OBJECT_ID\(N'SQL_DB..\1', N'U'\) IS NOT NULL\nDROP TABLE \1;\nGO\nCREATE TABLE \1\(

Move the cursor to the beginning of the file and hit Replace All.

This will turn your sample input into your sample output.

What did I do? I escaped the parenthesis in the Replace With expression. Also included the ( in the find what (so it is more accurate) and at the end of the Replace With.

于 2013-07-04T03:02:15.900 回答