Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 User Defined Functions Passing in a variable by ref

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-03-17 : 08:21:02
Chris writes "I am trying to generate a sequence from a select statement.
For each record that the select returns I am looking for an incremented value.


**********************************************
Example Outut:
40000000021125 1 012050 12000
40000000021125 2 013310 1500
**********************************************


Values 1 and 2 is an inline row counter.

I have acheived this result in a way that is clunky using the following code:


*************************************
CREATE TABLE #Q
(
TRANSACTIONID BIGINT NOT NULL,
AUTOID INT IDENTITY(1,1) PRIMARY KEY,
PRODUCTCODE VARCHAR(10) NOT NULL,
PRICE FLOAT NOT NULL
)

INSERT INTO #Q
Select TransactionId, ProductCode,(price*100) as Price from transactionItem WITH (NOLOCK) where TransactionId = 19

SELECT * FROM #Q

Drop Table #Q
*************************************


I figure there has to be away to do this with a function. I tried the following code only to get a large list of 1's in sequence column


*******************************************************
CREATE FUNCTION INCREMENTINC(@INCR INT) RETURNS INT
AS
BEGIN
RETURN (@INCR + 1)
END
*******************************************************

*********************************************
DECLARE @INC INT
SET @INC = 0

SELECT dbo.INCREMENTINC(@INC)
SELECT dbo.INCREMENTINC(@INC)
********************************************

OUTPUT=
1
1

What I wanted was output of
1
2


as I would be using the function in a "insert into table x select..." kind of statement

If the function allowed us to pass the variable in by reference (like in most programming languages, C#, VB, C++, Delphi,JAVA)this would work.

How do I accomplish this in a function in TSQL?

Thanks

Chris"

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-17 : 08:54:11
Hi Chris,

Bottom line is that sql server (2K and previous) does not provide this type of sequence number as part of any given result set. Here is an article comment thread with link to original article talking about this subject with some techniques to achieve what you are asking for.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6195

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -