Sql azure 中全局临时表的替代方案

发布于 2024-11-27 02:17:50 字数 1358 浏览 1 评论 0原文

我的 sp 的一部分包含以下代码。该代码在我的本地 SQL 数据库中成功执行。但是当我在 sql azure 中运行它时,它给出错误:

“此版本的 SQL Server 不支持全局临时对象。”

我如何更改此查询以使其在天蓝色中工作。

    SET @query = N'SELECT STUDENT_ID, ROLL_NO, TITLE, STUDENT_NAME, EXAM_NAME, '+
                @cols +
                ' INTO ##FINAL 
                FROM
                (
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #AVERAGES
                    UNION 
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #MARKS 
                    UNION
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #GRACEMARKS
                    UNION
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #TOTAL
                    ) p
                    PIVOT
                    (
                    MAX([MARKS])
                    FOR SUBJECT_ID IN
                    ( '+
                    @cols +' )
                ) AS FINAL
                ORDER BY STUDENT_ID ASC, DISPLAYORDER ASC, EXAM_NAME ASC;'

    EXECUTE(@query)

A part of my sp contains following code . The code executes successfully in my local sql database . but when i run it in sql azure it gives the error saying:

"Global temp objects are not supported in this version of SQL Server."

How can i change this query to make it work in azure.

    SET @query = N'SELECT STUDENT_ID, ROLL_NO, TITLE, STUDENT_NAME, EXAM_NAME, '+
                @cols +
                ' INTO ##FINAL 
                FROM
                (
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #AVERAGES
                    UNION 
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #MARKS 
                    UNION
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #GRACEMARKS
                    UNION
                    SELECT ROLL_NO, TITLE, STUDENT_ID, SUBJECT_ID, STUDENT_NAME, EXAM_NAME, DISPLAYORDER, MARKS
                    FROM #TOTAL
                    ) p
                    PIVOT
                    (
                    MAX([MARKS])
                    FOR SUBJECT_ID IN
                    ( '+
                    @cols +' )
                ) AS FINAL
                ORDER BY STUDENT_ID ASC, DISPLAYORDER ASC, EXAM_NAME ASC;'

    EXECUTE(@query)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音栖息无 2024-12-04 02:17:50

迟到总比不到好。 Azure SQL DB 公共预览版中的数据库范围全局临时表

与 SQL Server 的全局临时表类似,以 ##table_name 为前缀的表,Azure SQL DB 的全局临时表存储在 tempdb 中并遵循相同的语义。 但是,它们不是在服务器上的所有数据库之间共享,而是局限于特定数据库,并在同一数据库内的所有用户会话之间共享。来自其他 Azure SQL 数据库的用户会话无法访问全局作为连接到给定数据库的运行会话的一部分创建的临时表。任何用户都可以创建全局临时对象。

CREATE TABLE ##test(a int, b int);
INSERT INTO ##测试值(1,1);

并来自 MSDN CREATE TABLE

数据库范围的全局临时表(Azure SQL 数据库)

SQL Server 的全局临时表(以 ## 表名启动)存储在 tempdb 中,并在整个 SQL Server 实例的所有用户会话之间共享。有关 SQL 表类型的信息,请参阅上面有关创建表的部分。

Azure SQL 数据库支持也存储在 tempdb 中且范围仅限于数据库级别的全局临时表。这意味着全局临时表由同一 Azure SQL 数据库中的所有用户会话共享。来自其他 Azure SQL 数据库的用户会话无法访问全局临时表。

Azure SQL DB 的全局临时表遵循 SQL Server 用于临时表的相同语法和语义。同样,全局临时存储过程的范围也仅限于 Azure SQL DB 中的数据库级别。 Azure SQL 数据库还支持本地临时表(以 # 表名称启动),并遵循 SQL Server 使用的相同语法和语义。

Better late than never. Database Scoped Global Temporary Tables in public preview for Azure SQL DB:

Similar to global temporary tables for SQL Server, tables prefixed with ##table_name, global temporary tables for Azure SQL DB are stored in tempdb and follow the same semantics. However, rather than being shared across all databases on the server, they are scoped to a specific database and are shared among all users’ sessions within that same database. User sessions from other Azure SQL databases cannot access global temporary tables created as part of running sessions connected to a given database. Any user can create global temporary objects.

CREATE TABLE ##test ( a int, b int);
INSERT INTO ##test values (1,1);

And from MSDN CREATE TABLE:

Database scoped global temporary tables (Azure SQL Database)

Global temporary tables for SQL Server (initiated with ## table name) are stored in tempdb and shared among all users’ sessions across the whole SQL Server instance. For information on SQL table types, see the above section on Create Tables.

Azure SQL Database supports global temporary tables that are also stored in tempdb and scoped to the database level. This means that global temporary tables are shared for all users’ sessions within the same Azure SQL database. User sessions from other Azure SQL databases cannot access global temporary tables.

Global temporary tables for Azure SQL DB follow the same syntax and semantics that SQL Server uses for temporary tables. Similarly, global temporary stored procedures are also scoped to the database level in Azure SQL DB. Local temporary tables (initiated with # table name) are also supported for Azure SQL Database and follow the same syntax and semantics that SQL Server uses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文