2013-02-04 60 views
0

我想執行一個sql文本文件來在sql服務器數據庫上創建存儲過程。我也使用這種方法來創建用戶定義的表類型,存儲過程將使用這些表類型。使用ExecuteSqlCommand從文本文件創建存儲過程

表格類型的創建完美。但是,當我創建存儲過程時,出現錯誤, 'CREATE/ALTER PROCEDURE'必須是查詢批處理中的第一條語句。

這裏是一個讀取文件,並執行它靠在分貝的代碼:

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是calle首先是LoadStoredProcedures。表格創建時沒有問題。存儲過程不會被創建並生成上述錯誤。我已經刪除了'if exists ...'這一行,並且存儲過程將被創建,但是,如果還有其他人想要在同一個文件中創建它們,它們將會出錯並且不會被創建。我希望能夠用一個文件來管理這個文件,而不是每個存儲過程的多個文件。

有沒有人知道這方面的工作?希望我提供了充足的信息。提前致謝。

回答

0

基本上你就錯過了一堆命令之間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] 

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] 
+0

難道他還需要一個'格蘭特之前去過嗎? – Pete

+0

是的,當然可以! – Avitus

+0

作爲OP的說明,您可能已經發現了將腳本放入查詢窗口並在SSMS中執行它們的問題。通常,如果您傳遞的代碼失敗的SQL語句,這些相同的語句將無法在SSMS中運行。 – Pete