In SQL Server (I'm not sure about Sybase, but I imagine it is similar or the
same) you can do what Arbon suggested (although the [] are only needed where
column names have spaces or unusual characters). The other option is:
SET @id = (SELECT MAX(id) FROM BV2_ISSUES)
The advantage of Ardon's method is you can assign multiple variables at
once.
eg SELECT @var1 = Field1, @Var2 = Field2 FROM MyTable WHERE id = 1
BTW if this is to get the ID after an insert statement there must be better
ways of doing it. This way opens the possibility of losing db integrity (eg
someone inserts a record after you insert a record, but before your select
statement). In SQL Server the approved method is:
INSERT <insert stuff here>
SET @id = @@IDENTITY
There may not be a global @@IDENTITY variable in Sybase but there is
probably another way of getting the id after an insert.
regards
David Cameron
nOw.b2b
dcameron@i...
-----Original Message-----
From: Arbon Reimer [mailto:arbon_reimer@h...]
Sent: Thursday, 9 August 2001 11:42 PM
To: sql language
Subject: [sql_language] Re: sproc
I don't have the most experience with Sybase but I would say to change your
select to this:
SELECT @id = MAX([id]) FROM BV2_ISSUES
HTH
Arbon Reimer
----- Original Message -----
From: <giswim1@a...>
To: "sql language" <sql_language@p...>
Sent: Thursday, August 09, 2001 12:23 AM
Subject: [sql_language] sproc
> When writing a sproc, how do I assign a variable the value returned by a
> SELECT statement, specifically-- (using Sybase and SQL Programmer)
> SELECT MAX(id) FROM BV2_ISSUES
>
> I figured you would declare a variable (say @id) like--
> DECLARE @id int
>
> in the BEGIN part and then just assign it like--
> @id = SELECT MAX(id) FROM BV2_ISSUES
>
> but SQL Programmer says that is invalid syntax on the assignment line...
> anyone have any ideas?
>
> Thanks
>
> Grant
>