使用ExecuteSqlCommand从文本文件创建存储过程
我正在尝试执行一个sql文本文件,以便在sql server db上创建存储过程。我还使用此方法创建存储过程将使用的用户定义的表类型 表类型的创建非常有效。但是,当我去创建存储过程时,我得到了一个错误, “CREATE/ALTER PROCEDURE”必须是查询批处理中的第一条语句 下面是读取文件并针对db执行的代码:使用ExecuteSqlCommand从文本文件创建存储过程,sql,sql-server,entity-framework,stored-procedures,Sql,Sql Server,Entity Framework,Stored Procedures,我正在尝试执行一个sql文本文件,以便在sql server db上创建存储过程。我还使用此方法创建存储过程将使用的用户定义的表类型 表类型的创建非常有效。但是,当我去创建存储过程时,我得到了一个错误, “CREATE/ALTER PROCEDURE”必须是查询批处理中的第一条语句 下面是读取文件并针对db执行的代码: public static void LoadStoredProcedures() { const string procedureLocation =
public static void LoadStoredProcedures()
{
const string procedureLocation = "C:\\StoredProcedures.txt";
var reader = File.ReadAllText(procedureLocation);
var context = new prismEntities();
context.Database.ExecuteSqlCommand(reader);
}
public static void CreateTables()
{
const string tableLocation = "C:\\CreateTables.txt";
var reader = File.ReadAllText(tableLocation);
var context = new prismEntities();
context.Database.ExecuteSqlCommand(reader);
}
用户定义表的一个示例:
if not exists (select * from sys.table_types
where name like 'TextbookTable')
create type [dbo].[TextbookTable] as table (
[TXUID] [int] NOT NULL,
[SKU] [int] NOT NULL,
[UsedSKU] [int] NOT NULL,
[BindingID] [int] NOT NULL,
[TextStatusID] [int] NOT NULL,
[StatusDate] [datetime] NULL,
[Author] [char](45) NOT NULL,
[Title] [char](80) NOT NULL,
[ISBN] [char](30) NULL,
[Imprint] [char](10) NULL,
[Edition] [char](2) NULL,
[Copyright] [char](2) NULL,
[Type] [char](10) NULL,
[Bookkey] [varchar](10) NULL,
[Weight] [decimal](10, 4) NULL,
[ImageURL] [char](128) NULL,
primary key clustered
(
[TXUID] ASC
) with (ignore_dup_key = on)
)
我尝试创建的存储过程的一个示例:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddTextbook]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[AddTextbook]
create procedure [dbo].[AddTextbook]
(
@textbook TextbookTable readonly
)
as
begin
set nocount on;
set identity_insert textbook on
begin try
merge Textbook txt
using (select * from @textbook) as source
on txt.TXUID = source.TXUID
when not matched then
insert (TXUID, SKU, UsedSKU, BindingID, TextStatusID, StatusDate, Author,
Title, ISBN, Imprint, Edition, Copyright, Type, Bookkey, Weight, ImageURL)
values ( source.TXUID, source.SKU, source.UsedSKU, source.BindingID, source.TextStatusID,
source.StatusDate, source.Author, source.Title, source.ISBN,
source.Imprint, source.Edition,
source.Copyright, source.Type, source.Bookkey, source.Weight, source.ImageURL);
set identity_insert textbook off
end try
begin catch
declare @message varchar(128) = error_message()
select
ERROR_NUMBER() as ErrorNumber,
ERROR_SEVERITY() as ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_LINE() as ErrorLine,
ERROR_MESSAGE() as ErrorMessage;
raiserror(@message, 16, 10)
end catch
end
grant execute on [dbo].[AddTextbook] to [public]
现在,调用的顺序是先调用CreateTables,然后调用LoadStoredProcess。创建表时没有任何问题。存储过程不会被创建并生成上述错误。我已经删除了“if exists…”行,存储过程将被创建,但是,如果在同一个文件中有其他我尝试创建的存储过程,它们将出错而不会被创建。我希望能够用一个文件来管理它,而不是每个存储过程都有多个文件
有人知道这方面的解决办法吗?希望我提供了充分的信息。提前感谢。基本上,您在命令之间缺少一系列的
GO
语句,例如:
你必须改变
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddTextbook]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[AddTextbook]
create procedure [dbo].[AddTextbook]
将来
在授予之前,他不也需要一个
GO
?作为OP的注意事项,您可能已经发现了问题,方法是将脚本放在查询窗口中,然后在SSMS中执行它们。通常,如果您通过代码传递失败的SQL语句,这些语句在SSMS中运行时也会失败。GO会生成以下错误:“GO附近的语法不正确”。我直接从SSMS复制了SP。他们在那里工作得很好。好吧,既然我还不能回答我自己的问题,我确实想出了一个解决办法。我刚刚写了一个方法,可以根据GO语句将文本文件分解成一个列表。然后成功地执行了每条语句。谢谢大家。
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[AddTextbook]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[AddTextbook]
GO
create procedure [dbo].[AddTextbook]