4

当我运行以下书面程序时,出现以下错误。这有什么问题?

错误 :

消息 156,级别 15,状态 1,过程 tbv21Setup,第 11 行关键字“VIEW”附近的语法不正确。

**

CREATE PROCEDURE tbv21Setup

AS
BEGIN

    CREATE VIEW Cust_Address_View AS
        SELECT customer.individual_id   individual_id,
          customer.tb_customer_id   customer_id,
          customer.customer_category_lkp    customer_category_lkp,
          ADDRESS.postal_code   postal_code
        FROM CUSTOMER
        INNER JOIN INDIVIDUAL
        ON (CUSTOMER.individual_id = INDIVIDUAL.individual_id)
        JOIN
          (SELECT address_id ,
            a.individual_id ,
            postal_code ,
            obsolete_flag ,
            tb_modified_date
          FROM address a
          INNER JOIN
            (SELECT individual_id,
              MAX(ISNULL((tb_modified_date),('01/01/1900'))) AS max_tb_modified_date
            FROM ADDRESS
            WHERE obsolete_flag = 0
            GROUP BY individual_id
            ) AS xa
          ON (xa.individual_id                   = a.individual_id
          AND xa.max_tb_modified_date            = ISNULL((a.tb_modified_date),('01/01/1900')))
          WHERE a.obsolete_flag                  = 0
          ) AS ADDRESS ON (ADDRESS.individual_id = INDIVIDUAL.individual_id)
        AND customer.customer_category_lkp       =
          (SELECT lookup_id
          FROM lookup
          WHERE lookup_category_name = 'customer_category'
          AND code_string            ='prospect'
          )
        AND customer.obsolete_flag   =0
        AND ADDRESS.postal_code NOT IN ('UNKNOWN','unknown','U','u',' ','');

    ALTER TABLE tb_customer_attribute 
    ADD  tb_customer_attribute_id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
                obsolete_flag int,
                revision_number int;

    CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(16) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );


    --Logger table
    CREATE TABLE TBLogger (logDate DATETIME, logSQL VARCHAR(MAX), logRows INT, errKode INT);

    --Create a parameter table in CRB/STAGE database which will hold a row for crb/stage db prefix.
    CREATE TABLE crb_parameter (stgdb_prefix VARCHAR(255));

    INSERT INTO crb_parameter values ('tommydev1.dbo');
    --uncomment the below line and comment the above line in PRODUCTION
    --INSERT INTO crb_parameter values ('tb_stage.dbo');


END
4

3 回答 3

9

无法在 sp 中创建 DDL,只能批量创建。否则尝试动态 SQL -

CREATE PROCEDURE tbv21Setup

AS
BEGIN

    EXEC sp_executesql N'

    CREATE VIEW Cust_Address_View AS
        SELECT customer.individual_id   individual_id,
          customer.tb_customer_id   customer_id,
          customer.customer_category_lkp    customer_category_lkp,
          ADDRESS.postal_code   postal_code
        FROM CUSTOMER
        INNER JOIN INDIVIDUAL
        ON (CUSTOMER.individual_id = INDIVIDUAL.individual_id)
        JOIN
          (SELECT address_id ,
            a.individual_id ,
            postal_code ,
            obsolete_flag ,
            tb_modified_date
          FROM address a
          INNER JOIN
            (SELECT individual_id,
              MAX(ISNULL((tb_modified_date),(''01/01/1900''))) AS max_tb_modified_date
            FROM ADDRESS
            WHERE obsolete_flag = 0
            GROUP BY individual_id
            ) AS xa
          ON (xa.individual_id                   = a.individual_id
          AND xa.max_tb_modified_date            = ISNULL((a.tb_modified_date),(''01/01/1900'')))
          WHERE a.obsolete_flag                  = 0
          ) AS ADDRESS ON (ADDRESS.individual_id = INDIVIDUAL.individual_id)
        AND customer.customer_category_lkp       =
          (SELECT lookup_id
          FROM lookup
          WHERE lookup_category_name = ''customer_category''
          AND code_string            =''prospect''
          )
        AND customer.obsolete_flag   =0
        AND ADDRESS.postal_code NOT IN (''UNKNOWN'',''unknown'',''U'',''u'','' '','''');'

EXEC sp_executesql N'

    ALTER TABLE tb_customer_attribute 
    ADD  tb_customer_attribute_id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
                obsolete_flag int,
                revision_number int;

    CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(16) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );

    CREATE TABLE TBLogger (logDate DATETIME, logSQL VARCHAR(MAX), logRows INT, errKode INT);

    CREATE TABLE crb_parameter (stgdb_prefix VARCHAR(255));

    INSERT INTO crb_parameter VALUES (''tommydev1.dbo'');'


END

更新:

CREATE PROCEDURE tbv21Setup

AS
BEGIN

    EXEC sp_executesql N'

    IF EXISTS(
        SELECT 1 
        FROM dbo.sysobjects 
        WHERE id = OBJECT_ID(''dbo.Cust_Address_View'')
    ) DROP VIEW dbo.Cust_Address_View;

    CREATE VIEW dbo.Cust_Address_View AS
        SELECT customer.individual_id   individual_id,
          customer.tb_customer_id   customer_id,
          customer.customer_category_lkp    customer_category_lkp,
          ADDRESS.postal_code   postal_code
        FROM CUSTOMER
        INNER JOIN INDIVIDUAL
        ON (CUSTOMER.individual_id = INDIVIDUAL.individual_id)
        JOIN
          (SELECT address_id ,
            a.individual_id ,
            postal_code ,
            obsolete_flag ,
            tb_modified_date
          FROM address a
          INNER JOIN
            (SELECT individual_id,
              MAX(ISNULL((tb_modified_date),(''01/01/1900''))) AS max_tb_modified_date
            FROM ADDRESS
            WHERE obsolete_flag = 0
            GROUP BY individual_id
            ) AS xa
          ON (xa.individual_id                   = a.individual_id
          AND xa.max_tb_modified_date            = ISNULL((a.tb_modified_date),(''01/01/1900'')))
          WHERE a.obsolete_flag                  = 0
          ) AS ADDRESS ON (ADDRESS.individual_id = INDIVIDUAL.individual_id)
        AND customer.customer_category_lkp       =
          (SELECT lookup_id
          FROM lookup
          WHERE lookup_category_name = ''customer_category''
          AND code_string            =''prospect''
          )
        AND customer.obsolete_flag   =0
        AND ADDRESS.postal_code NOT IN (''UNKNOWN'',''unknown'',''U'',''u'','' '','''');'

EXEC sp_executesql N'

    ALTER TABLE dbo.tb_customer_attribute 
    ADD  tb_customer_attribute_id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
                obsolete_flag int,
                revision_number INT;

    IF EXISTS(
        SELECT 1 
        FROM dbo.sysobjects 
        WHERE id = OBJECT_ID(''dbo.tb_comm_hist_xfer'')
    ) DROP TABLE dbo.tb_comm_hist_xfer;

    CREATE TABLE dbo.tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(16) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );

    IF EXISTS(
        SELECT 1 
        FROM dbo.sysobjects 
        WHERE id = OBJECT_ID(''dbo.TBLogger'')
    ) DROP TABLE dbo.TBLogger;

    CREATE TABLE dbo.TBLogger (logDate DATETIME, logSQL VARCHAR(MAX), logRows INT, errKode INT);

    IF EXISTS(
        SELECT 1 
        FROM dbo.sysobjects 
        WHERE id = OBJECT_ID(''dbo.crb_parameter'')
    ) DROP TABLE dbo.crb_parameter;

    CREATE TABLE dbo.crb_parameter (stgdb_prefix VARCHAR(255));

    INSERT INTO dbo.crb_parameter VALUES (''tommydev1.dbo'');'

END
于 2013-04-24T06:44:49.647 回答
1

提示:直接创建视图而不是使用存储过程。

使用存储过程创建视图

于 2013-04-24T06:50:10.707 回答
1

您不能在过程中创建视图。

所以你可以先单独创建一个视图,如下所示:

CREATE VIEW Cust_Address_View AS
        SELECT customer.individual_id   individual_id,
          customer.tb_customer_id   customer_id,
          customer.customer_category_lkp    customer_category_lkp,
          ADDRESS.postal_code   postal_code
        FROM CUSTOMER
        INNER JOIN INDIVIDUAL
        ON (CUSTOMER.individual_id = INDIVIDUAL.individual_id)
        JOIN
          (SELECT address_id ,
            a.individual_id ,
            postal_code ,
            obsolete_flag ,
            tb_modified_date
          FROM address a
          INNER JOIN
            (SELECT individual_id,
              MAX(ISNULL((tb_modified_date),('01/01/1900'))) AS max_tb_modified_date
            FROM ADDRESS
            WHERE obsolete_flag = 0
            GROUP BY individual_id
            ) AS xa
          ON (xa.individual_id                   = a.individual_id
          AND xa.max_tb_modified_date            = ISNULL((a.tb_modified_date),('01/01/1900')))
          WHERE a.obsolete_flag                  = 0
          ) AS ADDRESS ON (ADDRESS.individual_id = INDIVIDUAL.individual_id)
        AND customer.customer_category_lkp       =
          (SELECT lookup_id
          FROM lookup
          WHERE lookup_category_name = 'customer_category'
          AND code_string            ='prospect'
          )
        AND customer.obsolete_flag   =0
        AND ADDRESS.postal_code NOT IN ('UNKNOWN','unknown','U','u',' ','');

然后根据您的要求在程序中使用此视图

或使用动态 SQL:

CREATE PROCEDURE tbv21Setup

AS
BEGIN

    EXEC sp_executesql N'

    CREATE VIEW Cust_Address_View AS
        SELECT customer.individual_id   individual_id,
          customer.tb_customer_id   customer_id,
          customer.customer_category_lkp    customer_category_lkp,
          ADDRESS.postal_code   postal_code
        FROM CUSTOMER
        INNER JOIN INDIVIDUAL
        ON (CUSTOMER.individual_id = INDIVIDUAL.individual_id)
        JOIN
          (SELECT address_id ,
            a.individual_id ,
            postal_code ,
            obsolete_flag ,
            tb_modified_date
          FROM address a
          INNER JOIN
            (SELECT individual_id,
              MAX(ISNULL((tb_modified_date),(''01/01/1900''))) AS max_tb_modified_date
            FROM ADDRESS
            WHERE obsolete_flag = 0
            GROUP BY individual_id
            ) AS xa
          ON (xa.individual_id                   = a.individual_id
          AND xa.max_tb_modified_date            = ISNULL((a.tb_modified_date),(''01/01/1900'')))
          WHERE a.obsolete_flag                  = 0
          ) AS ADDRESS ON (ADDRESS.individual_id = INDIVIDUAL.individual_id)
        AND customer.customer_category_lkp       =
          (SELECT lookup_id
          FROM lookup
          WHERE lookup_category_name = ''customer_category''
          AND code_string            =''prospect''
          )
        AND customer.obsolete_flag   =0
        AND ADDRESS.postal_code NOT IN (''UNKNOWN'',''unknown'',''U'',''u'','' '','''');'

IF EXISTS(select * from sys.objects where name='Cust_Address_View')
drop view Cust_Address_View 
EXEC sp_executesql N'

    ALTER TABLE tb_customer_attribute 
    ADD  tb_customer_attribute_id UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
                obsolete_flag int,
                revision_number int;

    CREATE TABLE tb_comm_hist_xfer (
        tb_comm_hist_xfer_id binary(16) NOT NULL,
        tb_old_customer_id int NOT NULL,
        tb_customer_id int NULL,
        date_entered datetime NOT NULL
    );

    CREATE TABLE TBLogger (logDate DATETIME, logSQL VARCHAR(MAX), logRows INT, errKode INT);

    CREATE TABLE crb_parameter (stgdb_prefix VARCHAR(255));

    INSERT INTO crb_parameter VALUES (''tommydev1.dbo'');'


END
于 2013-04-24T06:41:02.190 回答