I have a UDF which returns table variable like
--
--
RETURNS @ElementTable TABLE
(
ElementID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
ElementValue VARCHAR(MAX)
)
AS
--
--
Is the order of data in this table variable guaranteed to be same as the order data is inserted into it. e.g. if I issue
INSERT INTO @ElementTable(ElementValue) VALUES ('1')
INSERT INTO @ElementTable(ElementValue) VALUES ('2')
INSERT INTO @ElementTable(ElementValue) VALUES ('3')
I expect data will always be returned in that order when I say
select ElementValue from @ElementTable --Here I don't use order by
EDIT:
If order by is not guaranteed then the following query
SELECT T1.ElementValue,T2.ElementValue FROM dbo.MyFunc() T1
Cross Apply dbo.MyFunc T2
order by t1.elementid
will not produce 9×9 matrix as
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
consistently.
Is there any possibility that it could be like
1 2
1 1
1 3
2 3
2 2
2 1
3 1
3 2
3 3
How to do it using my above function?
Given your update, you obtain it in the obvious way – you ask the system to give you the results in the order you want:
You are guaranteed that if you’re using inefficient single row inserts within your UDF, that the IDENTITY values will match the order in which the individual
INSERTstatements were specified.