Try
http://msdn.microsoft.com/en-us/library/ms175098.aspx
IDENT_CURRENT
It's another one of the "flavors"...
IDENT_CURRENT( 'MyTableName' )
EDIT---------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Vegetable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[Vegetable]
END
GO
CREATE TABLE [dbo].[Vegetable] (
VegetableKey int IDENTITY (1001 , 1 ) not null
, VegetableName varchar(64) not null
)
GO
ALTER TABLE dbo.Vegetable ADD CONSTRAINT PK_Vegetable_VegetableKey
PRIMARY KEY CLUSTERED (VegetableKey)
GO
ALTER TABLE dbo.Vegetable ADD CONSTRAINT CK_Vegetable_VegetableName_UNIQUE
UNIQUE (VegetableName)
GO
GRANT SELECT , INSERT, UPDATE, DELETE ON [dbo].[Vegetable] TO public
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FoodItem]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[FoodItem]
END
GO
CREATE TABLE [dbo].[FoodItem] (
FoodItemKey int IDENTITY (2001 , 1 ) not null
, FoodItemName varchar(64) not null
)
GO
ALTER TABLE dbo.FoodItem ADD CONSTRAINT PK_FoodItem_FoodItemKey
PRIMARY KEY CLUSTERED (FoodItemKey)
GO
ALTER TABLE dbo.FoodItem ADD CONSTRAINT CK_FoodItem_FoodItemName_UNIQUE
UNIQUE (FoodItemName)
GO
GRANT SELECT , INSERT, UPDATE, DELETE ON [dbo].[FoodItem] TO public
GO
CREATE TRIGGER CopyVegetableToFoodItemTrigger on dbo.Vegetable
FOR INSERT , UPDATE AS
INSERT into dbo.FoodItem (FoodItemName)
Select i.VegetableName from
inserted i
where not exists ( select null from dbo.FoodItem innerRealTable where innerRealTable.FoodItemName = i.VegetableName )
GO
declare @MyIdentity int
INSERT INTO dbo.Vegetable ( VegetableName ) select 'Pumpkin'
select @MyIdentity = IDENT_CURRENT( '[dbo].[Vegetable]' )
print @MyIdentity
INSERT INTO dbo.Vegetable ( VegetableName ) select 'Tomato'
select @MyIdentity = IDENT_CURRENT( N'[dbo].[Vegetable]' )
print @MyIdentity
select * from dbo.Vegetable
select * from dbo.FoodItem