T-SQL使用动态SQL在存储过程中创建视图

时间:2019-05-28 10:40:22

标签: sql tsql

我有一个存储过程,可旋转查询结果并创建相应的视图。我停留在它实际创建视图的位置。请参见下面的代码

我已经尝试使用 execute(),sp_executesql()

如果在最初的 @query 中没有任何变量,则以下代码有效:

execute ('create view  AzureDataCatalog.temp AS ' + @query).

我必须使其与变量一起使用。

DECLARE @iterator INT = 0


declare @n int = (
                SELECT COUNT(DISTINCT c.name)  AS [ColumnName]
                FROM    sys.views  v 
                        INNER JOIN sys.all_columns c ON v.object_id = c.object_id
                        INNER JOIN sys.schemas s ON v.schema_id = s.schema_id
                )

WHILE @iterator< @n
BEGIN

Declare @cols nvarchar(max)
Declare @query nvarchar(max)

Select @cols = stuff((select ','+QuoteName([ColumnName]) from 
(
    SELECT  DISTINCT c.name  AS [ColumnName]
    FROM    sys.views  v 
            INNER JOIN sys.all_columns c ON v.object_id = c.object_id
            INNER JOIN sys.schemas s ON v.schema_id = s.schema_id
    ORDER BY [ColumnName] 
    OFFSET @iterator ROWS
    FETCH NEXT 1000 ROWS ONLY
) tb

    for xml path('')),1,1,'')
Select @query = N' Select * from (
    Select [ColumnName], RowN = Row_Number() over (order by [ColumnName]) 
    FROM 
        (
            SELECT  DISTINCT c.name  AS [ColumnName]
            FROM    sys.views  v 
                INNER JOIN sys.all_columns c ON v.object_id = c.object_id
                INNER JOIN sys.schemas s ON v.schema_id = s.schema_id
            ORDER BY [ColumnName] 
            OFFSET @iterator ROWS
            FETCH NEXT 1000 ROWS ONLY
        ) tb
    ) a
    pivot (MAX([RowN]) for [ColumnName] in (' + @cols + ')) p '

Exec sp_executesql @query, N'@iterator INT', @iterator


DECLARE @iteratorName NVARCHAR(20)
SET @iteratorName = CONVERT(NVARCHAR(20), @iterator)

DECLARE @viewName VARCHAR(100)
DECLARE @sqlCommand NVARCHAR(100)
SET @viewName = 'temp' + @iteratorName

-- Check if the view exists
IF EXISTS(SELECT 1 FROM sys.views WHERE name = @viewName)
BEGIN
    SET @sqlCommand = 'SELECT ''Yes'''
    EXEC(@sqlCommand)

    execute ('drop view  AzureDataCatalog.temp' + @iteratorName)

    -- This code works, if there aren't any variables inside @query
    execute ('create view  AzureDataCatalog.temp' + @iteratorName +' AS ' + @query)
END
ELSE
BEGIN
    SET @sqlCommand = 'SELECT ''No'''
    EXEC(@sqlCommand)

    SET @sqlCommand = 'create view  AzureDataCatalog.temp' + @iteratorName +' AS ' + @query

    Exec sp_executesql @query, N'@iterator INT', @iterator
END

SET @iterator += 1024


/*Execute a Stored Procedure*/
END

基本上,我需要运行以下代码:

DECLARE @sqlCommand NVARCHAR(100)
SET @sqlCommand = 'create view  AzureDataCatalog.temp' + @iteratorName +' AS ' + @query

Exec sp_executesql @query, N'@iterator INT', @iterator

但是我得到了以下错误

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'view'.

1 个答案:

答案 0 :(得分:0)

要更改标识符,您需要修改查询字符串,这似乎基本上是在做的。为了清楚起见,将所需的视图名称存储在字符串中,并在代码中使用该名称:

DECLARE @iteratorName NVARCHAR(20);
SET @iteratorName = CONVERT(NVARCHAR(20), @iterator);

DECLARE @viewName VARCHAR(100);
DECLARE @sqlCommand NVARCHAR(100);
SET @viewName = 'temp' + @iteratorName;

SET @sqlCommand = 'create view ' + @viewName + ' AS ' + @query;

EXEC sp_executesql @query;