如果我是你,我会非常熟悉 SqlCmd.exe(命令行实用程序)和使用变量。
我将尝试将 5 个文件的代码放在下面。我将在代码(文件的内容)之前放置一个标签,文件名如下:
||||||||||||||||||MyFileName.txt||||||||||||||||||
你不会把它放在文件的内容中,但是这个“标记”行下面的所有东西都是文件的内容。您需要完全按照我的方式命名文件。您会将所有文件放在同一目录中。
创建完所有文件后,您需要编辑(一个).bat 文件并更新一些信息。(主要是您的机器上存在 sqlcmd.exe 的位置,以及您有权使用集成身份验证创建数据库的 sqlserver/实例的名称。
以下是常见的位置:
%ProgramFiles%\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe
%ProgramFiles%\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe
%ProgramFiles% (x86)\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe
我们走吧!
||||||||||||||||||MasterRunMeBatFile.bat|||||||||||||||||||
REM Find the location of your SQLCMD.EXE
set __sqlCmdLocation=c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\SQLCMD.EXE
REM Set your servername/instancename here
set __sqlServerNameAndInstance=MyServerName\MyInstanceName
REM Create the database
"%__sqlCmdLocation%" -i .\DatabaseCreate.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_DatabaseCreateOutput.txt" -v DBName="MyFirstCommandLineDB"
REM Create the multiple Schemas
"%__sqlCmdLocation%" -i .\SchemasCreate.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_SchemasCreate_01.txt" -v DBName="MyFirstCommandLineDB" SchemaName="Schema01"
"%__sqlCmdLocation%" -i .\SchemasCreate.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_SchemasCreate_02.txt" -v DBName="MyFirstCommandLineDB" SchemaName="Schema02"
"%__sqlCmdLocation%" -i .\SchemasCreate.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_SchemasCreate_03.txt" -v DBName="MyFirstCommandLineDB" SchemaName="Schema03"
REM Create the DDL (tables)
"%__sqlCmdLocation%" -i .\OrganizationDDL.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_OrganizationDDL_01.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema01" MyUniqueNumber="01" DBUSERNAME="public"
"%__sqlCmdLocation%" -i .\OrganizationDDL.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_OrganizationDDL_02.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema02" MyUniqueNumber="02" DBUSERNAME="public"
"%__sqlCmdLocation%" -i .\OrganizationDDL.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_OrganizationDDL_03.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema03" MyUniqueNumber="03" DBUSERNAME="public"
REM Create some stored procedures against the multiple schemas
"%__sqlCmdLocation%" -i .\TSQL_USP_UDF_TRG.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_TSQL_USP_UDF_TRG_01.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema01" MyUniqueNumber="01" DBUSERNAME="public"
"%__sqlCmdLocation%" -i .\TSQL_USP_UDF_TRG.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_TSQL_USP_UDF_TRG_02.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema02" MyUniqueNumber="02" DBUSERNAME="public"
"%__sqlCmdLocation%" -i .\TSQL_USP_UDF_TRG.sql -b -S "%__sqlServerNameAndInstance%" -E -o ".\ZZZ_TSQL_USP_UDF_TRG_03.txt" -v DBName="MyFirstCommandLineDB" MySchemaVariable="Schema03" MyUniqueNumber="03" DBUSERNAME="public"
set __sqlCmdLocation=
set __sqlServerNameAndInstance=
|||||||||||||||||||DatabaseCreate.sql||||||||||||||||||
Use [master];
GO
if exists (select * from sysdatabases where name='$(DBName)')
BEGIN
DROP DATABASE [$(DBName)];
END
GO
Create Database $(DBName)
GO
||||||||||||||||||SchemasCreate.sql||||||||||||||||||
Use [$(DBName)]
GO
IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = '$(SchemaName)')
BEGIN
-- The schema must be run in its own batch!
EXEC( 'CREATE SCHEMA $(SchemaName)' );
END
IF EXISTS (SELECT 1 FROM sys.schemas WHERE name = '$(SchemaName)')
BEGIN
PRINT 'SCHEMA $(SchemaName) Exists!' ;
END
ELSE
BEGIN
PRINT 'Oh My : SCHEMA $(SchemaName) does not exist.' ;
END
GO
||||||||||||||||||组织DDL.sql||||||||||||||||||
Use [$(DBName)]
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[$(MySchemaVariable)].[Employee$(MyUniqueNumber)]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [$(MySchemaVariable)].[Employee$(MyUniqueNumber)]
END
GO
CREATE TABLE [$(MySchemaVariable)].[Employee$(MyUniqueNumber)]
(
EmployeeUUID [UNIQUEIDENTIFIER] NOT NULL DEFAULT NEWSEQUENTIALID() ,
SSN varchar(11) ,
LastName varchar(24) ,
FirstName varchar(24) ,
DateOfBirth smalldatetime
)
ALTER TABLE [$(MySchemaVariable)].[Employee$(MyUniqueNumber)] ADD CONSTRAINT PK_Employee$(MyUniqueNumber)
PRIMARY KEY NONCLUSTERED (EmployeeUUID)
ALTER TABLE [$(MySchemaVariable)].[Employee$(MyUniqueNumber)] ADD CONSTRAINT CK_Employee$(MyUniqueNumber)_SSN_Unique
UNIQUE (SSN)
GRANT SELECT , INSERT, UPDATE, DELETE ON [$(MySchemaVariable)].[Employee$(MyUniqueNumber)] TO $(DBUSERNAME)
GO
PRINT 'Select * from [$(MySchemaVariable)].[Employee$(MyUniqueNumber)]'
Select * from [$(MySchemaVariable)].[Employee$(MyUniqueNumber)]
||||||||||||||||||TSQL_USP_UDF_TRG.sql||||||||||||||||||
Use [$(DBName)]
GO
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[$(MySchemaVariable)].[uspEmployeeGetAll]') AND type in (N'P', N'PC'))
DROP PROCEDURE [$(MySchemaVariable)].[uspEmployeeGetAll]
GO
CREATE PROCEDURE [$(MySchemaVariable)].[uspEmployeeGetAll]
AS
SET NOCOUNT ON
SELECT
EmployeeUUID ,
SSN ,
LastName ,
FirstName ,
DateOfBirth
FROM
[$(MySchemaVariable)].[Employee$(MyUniqueNumber)] e
SET NOCOUNT OFF
GO
GRANT EXECUTE ON $(MySchemaVariable).[uspEmployeeGetAll] TO $(DBUSERNAME)
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[$(MySchemaVariable)].[uspEmployeeGetAll]') AND type in (N'P', N'PC'))
PRINT '[$(MySchemaVariable)].[uspEmployeeGetAll] has been created!'
GO
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[$(MySchemaVariable)].[uspEmployeeGetByUUID]') AND type in (N'P', N'PC'))
DROP PROCEDURE [$(MySchemaVariable)].[uspEmployeeGetByUUID]
GO
/*
declare @EmployeeUUID uniqueidentifier
select @EmployeeUUID = NEWID()
exec [$(MySchemaVariable)].[uspEmployeeGetByUUID] @EmployeeUUID
*/
CREATE PROCEDURE [$(MySchemaVariable)].[uspEmployeeGetByUUID]
@EmployeeUUID uniqueidentifier
AS
SET NOCOUNT ON
SELECT
EmployeeUUID ,
SSN ,
LastName ,
FirstName ,
DateOfBirth
FROM
[$(MySchemaVariable)].[Employee$(MyUniqueNumber)] e
WHERE
e.EmployeeUUID = @EmployeeUUID
SET NOCOUNT OFF
GO
GRANT EXECUTE ON $(MySchemaVariable).[uspEmployeeGetByUUID] TO $(DBUSERNAME)
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[$(MySchemaVariable)].[uspEmployeeGetByUUID]') AND type in (N'P', N'PC'))
PRINT '[$(MySchemaVariable)].[uspEmployeeGetByUUID] has been created!'
GO
=============== 结束文件和文件内容======================
行。
在这个练习结束时......你应该有这样的东西。
三表:(在同一个数据库内)
[Schema01].[Employee01] ,
[Schema02].[Employee02] ,
[Schema03].[Employee03]
并且存储过程类似于下面的一个。(注意,存储过程的模式名称和它从中提取的表。)
ALTER PROCEDURE [Schema01].[uspEmployeeGetAll]
AS
SET NOCOUNT ON
SELECT
EmployeeUUID ,
SSN ,
LastName ,
FirstName ,
DateOfBirth
FROM
[Schema01].[Employee01] e
恕我直言。使用带变量的 sqlcmd.exe 是确保不同环境之间完美可重复性的最佳方式。
另一个人的想法:
http://blogs.msdn.com/tomholl/archive/2008/04/29/thoughts-on-being-a-solution-architect.aspx
最大限度地减少开发人员需要编写的代码量 开发人员编写代码是有报酬的,而且他们通常都很擅长。但是,一旦为开发人员分配了一大堆需求或故事,他们就需要着手处理这些特定需求,而且他们很难在任何细节级别上跟上其他人正在做的事情。
这可以包括发现不同需求之间的协同作用或宏观级代码重用和重构的机会。架构师工作的很大一部分是在这些机会出现时抓住它们,并确保开发人员不会在他们自己的世界中重新发明轮子。
理想情况下,这应该导致模式、组件和框架允许开发人员通过专注于那些独特的部分来用更少的代码完成他们的需求。
额外的:
http://www.yaldex.com/sql_server_tutorial_3/ch06lev1sec5.html
这就是您开发脚本的方式。但不要忘记注释掉变量设置(在 .sql 文件中),因为文件内容中的变量优先于通过命令行传入的变量。
请就这个问题投票!
http://connect.microsoft.com/sqlserver/feedback/details/382007/in-sqlcmd