Returning identity from a function

I'm using this really nice function borrowed from stackoverflow.com/questions/10914576/tsql-split-string, and when I utilize it, I'm able to return Orders and UserIDs below.

OrderID      UserID
1              22
1              23
1              24

However, I'd like to also return the identity to each row. How can this be done using the function?

Thanks
barnescoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DcpKingCommented:
Firstly, define "identity". Remember that SQL has no real concept of sequentiality, so there should be no "first", "second", etc.

Go back to your StackOverflow page and look at the first example. You can add another field - ident, say - to the table declaration, making it an int and an identity, and it will give you a new number value every time you add a record.

Remember that the order in which you retrieve the elements may not be in your definition of order by "identity"!

hth

Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
barnescoAuthor Commented:
Mike, I see what you mean. You answered the question exactly as I asked, but I should have rephrased the question another way--my bad. What I need is to find the @@identity of the values from the table I'm inserting into, but I'll ask that in another question. Thanks for your help.
0
DcpKingCommented:
You can do that, np! Look up the OUTPUT clause for the INSERT statement:
create table dbo.Test (Id bigint IDENTITY(1,1) NOT NULL,
                                        Name varchar(50) NULL                        )
declare @intInsertedId BIGINT

insert into Test
    output @intInsertedId = inserted.Id
    select 'DcpKing'

select @intInsertedId;

Open in new window


SQL Server maintains a virtual table called Inserted when you do an insert. Likewise for update, merge, and delete statements. This lets you insert a number of rows (so long as they're all in the same statement) and then pull back the assigned ID of each!

hth

Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.