使用SQL过程在MSSQL中创建数据库

时间:2018-04-03 07:19:10

标签: sql-server python-3.x pyodbc

您好我正在尝试使用以下SQL过程创建数据库

Create_SQL_Proc_Statement = """
CREATE PROCEDURE db_creation_script
           @DBName nvarchar(50),
           @data_path nvarchar(50) ='',
           @data_file_size nvarchar(50) = '5000KB',
           @data_file_maxsize nvarchar(50) = 'UNLIMITED',
           @data_file_filegrowth nvarchar(50) = '1024KB',
           @log_path nvarchar(50) ='',
           @log_file_size nvarchar(50) = '5000KB',
           @log_file_maxsize nvarchar(50) = '1048576KB',
           @log_file_filegrowth nvarchar(50) = '1024KB'
AS

begin

           declare @l_variable_data as nvarchar(500);
           declare @l_variable_log as nvarchar(500);
           declare @complete_command as nvarchar(1000);
           declare @print_message as nvarchar(1000);
           declare @error as int;

           --Fetch data path

           if (@data_path ='' OR @data_path is null )
                          SELECT @data_path=cast(SERVERPROPERTY('InstanceDefaultDataPath') as nvarchar);
           if (@data_path ='' OR @data_path is null )

           begin
                          set @error=@@ERROR
                          if (@error<>0)
                          begin
                                         set @print_message=N'Error '+ RTRIM(cast(@error as nvarchar(10))) + ' - Message: '+ ERROR_MESSAGE();
                                         print(@print_message);
                          end
                          else
                                         print('No error is generated');
           end

           --Fetch log path

           if (@log_path ='' OR @log_path is null )
                          SELECT @log_path=cast(SERVERPROPERTY('InstanceDefaultLogPath') as nvarchar);
           if (@log_path ='' OR @log_path is null )
           begin
                          set @error=@@ERROR
                          if (@error<>0)
                          begin
                                         set @print_message=N'Error '+ RTRIM(cast(@error as nvarchar(10))) + ' - Message: '+ ERROR_MESSAGE();
                                         print(@print_message);
                          end
                          else
                                         print('No error is generated');
           end


            set @l_variable_data = concat('NAME =', '''',@DBName, '_DATA','''', ', FILENAME = ', '''', @data_path,'\',@DBName, '.mdf', '''', ', SIZE = ', @data_file_size, ', MAXSIZE = ', @data_file_maxsize,', FILEGROWTH = ',@data_file_filegrowth)
            set @l_variable_log  = concat('NAME =', '''',@DBName, '_LOG', '''', ', FILENAME = ', '''', @log_path, '\',@DBName, '.ldf', '''', ', SIZE = ', @log_file_size, ', MAXSIZE = ',  @log_file_maxsize,', FILEGROWTH = ',@log_file_filegrowth)

           set @complete_command = concat(' CREATE DATABASE ', @DBName , ' CONTAINMENT = NONE ON PRIMARY (', @l_variable_data, ')' , ' LOG ON (' , @l_variable_log , ')');


           exec(@complete_command);

end
"""

,以下是我处理proc的方式:

cursor.execute(Create_SQL_Proc_Statement)
cursor.execute('Exec db_creation_script  ?, ?, ?, ?, ?, ?, ?, ?, ?',(DB_Name,Data_Path,Data_File_Size,Data_File_Maxsize,Data_File_Filegrowth,Log_Path,Log_File_Size,Log_File_Maxsize,Log_File_Filegrowth))

我也试过以下内容:

cursor.execute('{ call db_creation_script  (?, ?, ?, ?, ?, ?, ?, ?, ?)}',(DB_Name,Data_Path,Data_File_Size,Data_File_Maxsize,Data_File_Filegrowth,Log_Path,Log_File_Size,Log_File_Maxsize,Log_File_Filegrowth))
cursor.execute('exec db_creation_script  {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}'.format(DB_Name,Data_Path,Data_File_Size,Data_File_Maxsize,Data_File_Filegrowth,Log_Path,Log_File_Size,Log_File_Maxsize,Log_File_Filegrowth))

但没有运气,我总是得到以下例外:

 cursor.execute('exec db_creation_script  {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}'.format(DB_Name,Data_Path,Data_File_Size,Data_File_Maxsize,Data_File_Filegrowth,Log_Path,Log_File_Size,Log_File_Maxsize,Log_File_Filegrowth))
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near ','. (102) (SQLExecDirectW)")

每次运行上面的代码时,都会收到不成功的消息,请您帮我理解我脚本中的问题究竟在哪里?如果有任何人可以让我知道是否还有其他方法可以做到这一点,那将是很棒的。

注意:我使用pyodbc python模块来实现相同的

1 个答案:

答案 0 :(得分:1)

您需要确保该字符串对SQLServer

有效

这里我已经进行了必要的更改,并且在我的实例上创建了存储过程,无论它是否有效是另一个问题: - )

我故意遗漏了exec(完整命令)上面的3行,因为要输入很多引号: - )

此脚本适用于SSMS

       DECLARE @Create_SQL_Proc_Statement NVARCHAR(MAX);
       SELECT @Create_SQL_Proc_Statement = N'
       CREATE PROCEDURE db_creation_script
       @DBName nvarchar(50),
       @data_path nvarchar(50) ='''',
       @data_file_size nvarchar(50) = ''5000KB'',
       @data_file_maxsize nvarchar(50) = ''UNLIMITED'',
       @data_file_filegrowth nvarchar(50) = ''1024KB'',
       @log_path nvarchar(50) ='''',
       @log_file_size nvarchar(50) = ''5000KB'',
       @log_file_maxsize nvarchar(50) = ''1048576KB'',
       @log_file_filegrowth nvarchar(50) = ''1024KB''
       AS

       begin

       declare @l_variable_data as nvarchar(500);
       declare @l_variable_log as nvarchar(500);
       declare @complete_command as nvarchar(1000);
       declare @print_message as nvarchar(1000);
       declare @error as int;

       --Fetch data path

       if (@data_path ='''' OR @data_path is null )
                      SELECT @data_path=cast(SERVERPROPERTY(''InstanceDefaultDataPath'') as nvarchar);
       if (@data_path ='''' OR @data_path is null )

       begin
                      set @error=@@ERROR
                      if (@error<>0)
                      begin
                                     set @print_message=N''Error ''+ RTRIM(cast(@error as nvarchar(10))) + '' - Message: ''+ ERROR_MESSAGE();
                                     print(@print_message);
                      end
                      else
                                     print(''No error is generated'');
       end

       --Fetch log path

       if (@log_path ='''' OR @log_path is null )
                      SELECT @log_path=cast(SERVERPROPERTY(''InstanceDefaultLogPath'') as nvarchar);
       if (@log_path ='''' OR @log_path is null )
       begin
                      set @error=@@ERROR
                      if (@error<>0)
                      begin
                                     set @print_message=N''Error ''+ RTRIM(cast(@error as nvarchar(10))) + '' - Message: ''+ ERROR_MESSAGE();
                                     print(@print_message);
                      end
                      else
                                     print(''No error is generated'');
       end



       exec(@complete_command);

       end
       '           

       EXECUTE sp_executesql @Create_SQL_Proc_Statement