sql – Azure数据仓库中的表变量

前端之家收集整理的这篇文章主要介绍了sql – Azure数据仓库中的表变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sql Server数据库中,可以使用如下表变量:
  1. declare @table as table (a int)

在Azure数据仓库中,会引发错误.

Parse error at line: 1,column: 19: Incorrect Syntax near ‘table’

在Azure数据仓库中,您可以使用临时表:

  1. create table #table (a int)

但不是内部功能.

Msg 2772,Level 16,State 1,Line 6 Cannot access temporary tables
from within a function.

来自微软的This document说,

◦Must be declared in two steps (rather than inline): ◾CREATE TYPE
my_type AS TABLE …;,then ◾DECLARE @mytablevariable my_type;.

但是当我尝试这个时:

  1. create type t as table (a int);
  2. drop type t;

我明白了:

Msg 103010,Line 1 Parse error at line: 1,column:
8: Incorrect Syntax near ‘type’.

我的目标是在Azure数据仓库中使用一个使用临时表的函数.它可以实现吗?

编辑从这里开始

请注意,我不是在寻找其他方法来创建一个特定的功能.我实际上已经这样做了并继续前进.我是一名资深程序员,但是Azure数据仓库的新手.我想知道是否可以在Azure数据仓库功能中加入一些临时表的概念.

解决方法

不,你不能.无法在用户定义函数(UDF)中创建对象.请改用表变量.

如果您希望使用用户定义的类型,请首先在UDF外部创建它,并将其用作UDF中的变量类型.

  1. -- Create the data type
  2. CREATE TYPE TestType AS TABLE
  3. (
  4. Id INT NOT NULL,Col1 VARCHAR(20) NOT NULL)
  5. GO
  6.  
  7. -- Create the tabled valued function
  8. CREATE FUNCTION TestFunction
  9. ()
  10. RETURNS
  11. @Results TABLE
  12. (Result1 INT,Result2 INT)
  13. AS
  14. BEGIN
  15. -- Fill the table variable with the rows for your result set
  16. DECLARE @Var1 TestType;
  17.  
  18. RETURN
  19. END
  20. GO

猜你在找的MsSQL相关文章