Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6166083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:16:42+00:00 2026-05-23T22:16:42+00:00

I have a UDF which returns table variable like — — RETURNS @ElementTable TABLE

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:16:43+00:00Added an answer on May 23, 2026 at 10:16 pm

    Given your update, you obtain it in the obvious way – you ask the system to give you the results in the order you want:

    SELECT T1.ElementValue,T2.ElementValue FROM dbo.MyFunc() T1
    Cross join dbo.MyFunc() T2
    order by t1.elementid, t2.elementid
    

    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 INSERT statements were specified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a table-valued UDF that starts by a CTE to return a
I have a table that stores user information. The table has a userid (identity)
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have a table where I'm storing Lat/Long coordinates, and I want to make
I have a relatively complex query, with several self joins, which works on a
Environment: SQL Server 2005 I have a stored proc which receives comma separated value
I have this great function that parses a field into a pivot table consisting
DBMS: MS Sql Server 2005, Standard I'd like to make a table constraint to
I have quite a large nvarchar which I wish to pass to the HashBytes
I have the following tables in a SQL Server 2008 db: tblItem , which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.