SQL Server 2000 - 我应该如何编写此脚本?

时间:2011-01-31 20:47:00

标签: sql sql-server tsql sql-server-2000

我写了我的SQL并遇到了一个问题,请参阅here

所以我要问正确的方法来做我想要做的事情,我认为,使用EXEC的黑客。如果EXEC是正确的方法,那么我遇到SQL问题但会使用它。

我正在编写一个SQL脚本来创建/更新数据库。我的结构如此:

  Declare SafetyCheck=0
  Declare DBVersion=0
  Declare NewInstall=0
  Declare DropTables=0

  IF SafetyCheck=1
  Begin
    IF DropTables = 1
      Drop all tables in the database

    IF Config table doesn't exist
    Begin
      Create the Config Table
      NewInstall = 1
    End
    IF Other Table doesn't exist
      Create the other table
    IF Other Table doesn't exist
      Create the other table

    DBVersion = CheckTheDBVersion
    IF DBVersion is old
    Begin
      Alter tables to make them +1 version higher
      Insert some stuff
    End

    DBVersion = CheckTheDBVersion
    IF DBVersion is old
    Begin
      Alter tables to make them +1 version higher
      Insert some stuff
    End

    IF NewInstall = 1
    Begin
      Insert default data
    End
  End

从逻辑上讲,上述应该可行,但不是因为上面我喜欢的问题。据我所知,我有EXEC或GO选项。 EXEC,如上所述,我认为是一个黑客。它执行一个字符串作为SQL,但我没有以动态的方式使用它,我有静态的SQL坐在它 - 我似乎错了。据我所知,GO会将其全部打破,因为它会重置变量。

我可以选择在这一点上重写SQL,所以如果我能以正确的方式做到这一点,我会的。我只是不知道我猜的正确方法。

编辑(似乎正在运行的示例脚本):

我不得不打破上面示例中的SafetyCheck检查,因为我无法在IF语句中放置GO。这让它变得更加难看。

-- -- --
-- Run this once to create the initial tables
-- -- --
CREATE TABLE Config (
    ID          [int] IDENTITY(1,1) NOT NULL,
    [Key]       [nvarchar](255) NOT NULL,
    Value       [nvarchar](255) NOT NULL
)
ALTER TABLE Config ADD PRIMARY KEY (ID) 
CREATE TABLE Roles (
    ID              [int] IDENTITY(1,1) NOT NULL,
    Name            [nvarchar](25) NOT NULL,
    Created         [DateTime] NOT NULL
)
ALTER TABLE Roles ADD PRIMARY KEY (ID)

-- -- --
-- Below, this is the script thats causing errors
-- -- --

IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '#v')
    DROP TABLE #v
CREATE TABLE #v (n char(30),v char(30))

INSERT INTO #v (n,v) VALUES ('RunSQL', '1')         -- Set to 1 or this does nothing
INSERT INTO #v (n,v) VALUES ('DropTables', '0')     -- Set to 1 to drop existing tables
                                                    -- dont do this for an upgrade
                                                    -- dont do this is there are custom tables

GO
IF (SELECT v FROM #v WHERE n='RunSQL') = 0
BEGIN
    SELECT 'Nothing has been done. Read First, Run Later' AS Error
END


IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    IF (SELECT v FROM #v WHERE n='DropTables') = 1
    BEGIN
        -- Remove all tables if they exist
        EXEC sp_MSforeachtable @command1 = 'DROP TABLE ?'
    END
END
GO

IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    IF NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Config')
    BEGIN
        CREATE TABLE Config (
            ID          [int] IDENTITY(1,1) NOT NULL,
            [Key]       [nvarchar](255) NOT NULL,
            Value       [nvarchar](255) NOT NULL
        )
        ALTER TABLE Config ADD PRIMARY KEY (ID) 

        -- Assume this is a new database
        INSERT INTO Config ([Key], Value) VALUES ('DBVersion', '3')
    END

    IF NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Roles')
    BEGIN
        CREATE TABLE Roles (
            ID              [int] IDENTITY(1,1) NOT NULL,
            Name            [nvarchar](25) NOT NULL,
            Created         [DateTime] NOT NULL,
            ModifiedDate    [DateTime] NOT NULL
        )
        ALTER TABLE Roles ADD PRIMARY KEY (ID)
    END
END
GO

-- Lookup the current verision of the database
IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    INSERT INTO #v (n,v) SELECT 'CMSDBVersion', Value FROM Config WHERE [Key]='DBVersion'
END
GO

-- Update old tables to be like the new ones
IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    IF (SELECT COUNT(v) FROM #v WHERE n='CMSDBVersion') = 0
    BEGIN
        -- Add newer fields to exiting tables
        ALTER TABLE Roles ADD ModifiedDate DateTime;
        INSERT INTO Config ([Key], Value) Values ('DBVersion', '3')
    END
END
IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    INSERT INTO #v (n,v) SELECT 'CMSDBVersion', Value FROM Config WHERE [Key]='DBVersion'
END
GO

IF (SELECT v FROM #v WHERE n='RunSQL') = 1
BEGIN
    IF (SELECT v FROM #v WHERE n='CMSDBVersion') = '3'
    BEGIN
        -- Add/Update the standard rules
        IF NOT EXISTS (SELECT * FROM Roles WHERE Name='CMS User Admin')
            INSERT INTO Roles (Name, Created, ModifiedDate) VALUES ('CMS User Admin', GETDATE(), GETDATE())
    END
END
GO

-- remove the temp table
DROP TABLE #v

1 个答案:

答案 0 :(得分:1)

编写它的一种方法是将所有部分分解为单独的支持SP。由于每个SP在执行时自行编译,因此在单个批处理块(由GO分隔)内,主SP上的依赖对象和所有支持SP的一致性没有静态检查。

除此之外,使用动态SQL重写查询以实现DDL更改,或使用GO语句将其分解为批次,如您所发现的那样。

修改

您显示的代码段不起作用,因为:

当RunSQL = 0时,此部分不运行

-- Update old tables to be like the new ones
IF (SELECT v FROM #v WHERE n='RunSQL') = 1

这导致不将ModifiedDate添加到角色。当我将RunSQL更改为1时,下一部分不起作用

IF (SELECT v FROM #v WHERE n='CMSDBVersion') = ''

因为先前的插入没有在表中放置任何内容(Config中没有记录)

INSERT INTO #v (n,v) SELECT 'CMSDBVersion', Value FROM Config WHERE [Key]='DBVersion'
GO

整个块应该只是

-- Update old tables to be like the new ones
    IF NOT EXISTS (select * from syscolumns where name='ModifiedDate' and ID = OBJECT_ID('Roles'))
    BEGIN
        -- Add newer fields to exiting tables
        ALTER TABLE Roles ADD ModifiedDate DateTime;
        UPDATE Config SET Value = '3' WHERE [Key]='DBVersion'
    END