在sql Server数据库中,可以使用如下表变量:
- declare @table as table (a int)
在Azure数据仓库中,会引发错误.
Parse error at line: 1,column: 19: Incorrect Syntax near ‘table’
在Azure数据仓库中,您可以使用临时表:
- 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;.
但是当我尝试这个时:
- create type t as table (a int);
- drop type t;
我明白了:
Msg 103010,Line 1 Parse error at line: 1,column:
8: Incorrect Syntax near ‘type’.
我的目标是在Azure数据仓库中使用一个使用临时表的函数.它可以实现吗?
编辑从这里开始
请注意,我不是在寻找其他方法来创建一个特定的功能.我实际上已经这样做了并继续前进.我是一名资深程序员,但是Azure数据仓库的新手.我想知道是否可以在Azure数据仓库功能中加入一些临时表的概念.
解决方法
不,你不能.无法在用户定义函数(UDF)中创建对象.请改用表变量.
如果您希望使用用户定义的类型,请首先在UDF外部创建它,并将其用作UDF中的变量类型.
- -- Create the data type
- CREATE TYPE TestType AS TABLE
- (
- Id INT NOT NULL,Col1 VARCHAR(20) NOT NULL)
- GO
- -- Create the tabled valued function
- CREATE FUNCTION TestFunction
- ()
- RETURNS
- @Results TABLE
- (Result1 INT,Result2 INT)
- AS
- BEGIN
- -- Fill the table variable with the rows for your result set
- DECLARE @Var1 TestType;
- RETURN
- END
- GO