我在SQL Server中的存储过程出错

时间:2018-08-28 12:06:53

标签: sql sql-server stored-procedures dynamic

我尝试编写将检查的存储过程:

  1. 如果存在表和列,则我们写入数据
  2. 如果表不存在,那么我们创建它并写入数据
  3. 如果表存在,那么我们检查现有列并将数据写入到存在中 列

这是代码:

CREATE PROCEDURE [dbo].[my_proc] @file_path VARCHAR(1000) = NULL
    ,@file_type VARCHAR(1000) = NULL
    ,@file_name VARCHAR(1000) = NULL
    ,@table_name VARCHAR(1000)
AS
BEGIN
    DECLARE @sql NVARCHAR(4000);

    SET @table_name = '[dbo].[' + @table_name + ']'
    SET @sql = '
    DECLARE @msg VARCHAR(max)

    --if column and table exist
    IF (
            EXISTS (
                SELECT *
                FROM sys.objects
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                ) --табличка
            AND EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND (
                        NAME = '' path_file ''
                        AND NAME = '' file_type ''
                        AND NAME = '' file_name ''
                        )
                )
            ) --поля
    BEGIN
        INSERT INTO ' + @table_name + ' (
            file_path
            ,file_type
            ,file_name
            )
        VALUES (
            ''' + isnull(@file_path, '') + '''
            ,''' + isnull(@file_type, '') + '''
            ,''' + isnull(@f ile_name, '') + '''
            )
    END

    --if table not exist
    IF NOT EXISTS (
            SELECT *
            FROM sys.objects
            WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
            )
    BEGIN
        CREATE TABLE ' + @table_name + ' (
            id INT NOT NULL identity(1, 1) PRIMARY KEY
            ,file_path TEXT
            ,file_name TEXT
            ,file_type TEXT
            )
    END

    --if table exist
    IF EXISTS (
            SELECT *
            FROM sys.objects
            WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
            )
    BEGIN
        --check for columns in table
        --file_path
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND (
                        NAME = ''path_file''
                        OR NAME = ''file_path''
                        )
                )
        BEGIN
            PRINT (''NOT COLUMN path_file'')
        END

        --file name
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', '' U '')
                    AND NAME = '' file_name ''
                )
        BEGIN
            PRINT ('' NOT COLUMN file_name '')
        END

        --file_type
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND (
                        NAME = ''file_type''
                        OR NAME = ''file_type''
                        )
                )
        BEGIN
            PRINT (''NOT COLUMN file_type'')
        END
        ELSE
            INSERT INTO ' + @table_name + ' (
                file_path
                ,file_name
                ,file_type
                )
            VALUES (
                ''' + isnull(@file_path, '') + '''
                ,''' + isnull(@file_name, '') + '''
                ,''' + isnull(@f ile_type, '') + '''
                )
    END';

    EXEC (@sql);
END

已经存在的表存在问题,但是我没有列

请帮助我

1 个答案:

答案 0 :(得分:0)

我试图运行您的脚本,唯一的错误是发生了一个未声明的变量。

您在用空白写“ @f ile_name”。

顺便说一句,我建议您拆分代码以进行阅读。我试图尽量减少混乱。

CREATE PROCEDURE [dbo].[my_proc] 
    @file_path VARCHAR(1000) = NULL
    ,@file_type VARCHAR(1000) = NULL
    ,@file_name VARCHAR(1000) = NULL
    ,@table_name VARCHAR(1000)
AS
BEGIN
    DECLARE @sql NVARCHAR(4000);
    DECLARE @msg VARCHAR(max);

    SET @table_name = '[dbo].[' + @table_name + ']'
    SET @sql = '
    --If table not exist then we create it
    IF NOT EXISTS (
            SELECT *
            FROM sys.objects
            WHERE object_id = OBJECT_ID(N''' + @table_name + ''')
                AND type IN (N''U'')
            )
    BEGIN
        CREATE TABLE ' + @table_name + ' (
            id INT NOT NULL identity(1, 1) PRIMARY KEY
            ,file_path TEXT
            ,file_name TEXT
            ,file_type TEXT
            )
    END';

    EXEC (@sql);

    SET @sql = '
    --If table exist then we check existing columns and we write data into exists columns
    IF EXISTS (
            SELECT *
            FROM sys.objects
            WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
            )
    BEGIN
        PRINT (''TABLE found'')

        --check for columns in table
        --file_path
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', '' U '')
                    AND (
                        NAME = '' path_file ''
                        OR NAME = ''file_path''
                        )
                )
        BEGIN
            PRINT (''NOT COLUMN path_file'')
        END

        --file name
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND NAME = ''file_name''
                )
        BEGIN
            PRINT (''NOT COLUMN file_name'')
        END

        --file_type
        IF NOT EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND (
                        NAME = ''file_type''
                        OR NAME = ''file_type''
                        )
                )
        BEGIN
            PRINT (''NOT COLUMN file_type'')
        END
        ELSE
            INSERT INTO ' + @table_name + ' (
                file_path
                ,file_name
                ,file_type
                )
            VALUES (
                ''' + isnull(@file_path, '') + '''
                ,''' + isnull(@file_name, '') + '''
                ,''' + isnull(@file_type, '') + '''
                )
    END';

    EXEC (@sql);

    SET @sql = '
    --If table and columns exists then we write data
    IF (
            EXISTS (
                SELECT *
                FROM sys.objects
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                ) --табличка
            AND EXISTS (
                SELECT *
                FROM sys.columns
                WHERE object_id = OBJECT_ID(''' + @table_name + ''', ''U'')
                    AND (
                        NAME = ''path_file''
                        AND NAME = ''file_type''
                        AND NAME = ''file_name''
                        )
                )
            ) --поля
    BEGIN
        INSERT INTO ' + @table_name + ' (
            file_path
            ,file_type
            ,file_name
            )
        VALUES (
            ''' + isnull(@file_path, '') + '''
            ,''' + isnull(@file_type, '') + '''
            ,''' + isnull(@file_name, '') + '''
            )
    END';

    EXEC (@sql);
END