Finding UID of Inserted Record

Hi guys,

Is it possible to have in an INSERT statement, an extra field which is something like this:

'Material:'+@UID_OF_THIS_RECORD

So basically, mixing text, with the INT, UID of the record being Inserted, or would I need to do it in an UPDATE after the INSERT? If so, how would I know what the UID of the Inserted record is?

Cheers guys!
LVL 4
Cyber-DrugsAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
alter table
  add  ComputedField as 'Material:'+ cast ( UID_FIELD as varchar(400) )

0
 
Ved Prakash AgrawalDatabase Consultant/Performance ArchitectCommented:
you need to update the fields after insert.
when you insert the records then you can get the unique value which is inserted.

and then you can update this value in the record which is inserted.


0
 
Cyber-DrugsAuthor Commented:
OK, and what code is used to get the unique value of the Inserted Record?
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
will the value ever change once the record is created?
-> if no, make this a computed field and not a "true" field.

-> yes
  is it identity or uniqueidentified?

0
 
Cyber-DrugsAuthor Commented:
angelIII

The value will never change, it is done once, and that is it. :)
0
 
Cyber-DrugsAuthor Commented:
Would something like this work?


INSERT INTO dbo.tblModule
(
      DeviceID,
      ModuleCatID,
      SlotID,
      [Serial Number],
      Module_FL,
      SAP_EQ
) VALUES (
      @DeviceUID,
      @ModuleCatUID,
      @SlotUID,
      @serial,
      @data,
      @equipment
)
UPDATE dbo.tblModule
SET NodeName = 'Module:'+@@IDENTITY
WHERE UID = @@IDENTITY
0
 
Cyber-DrugsAuthor Commented:
I only just noticed your post after I made mine AngelIII.

Will try your code out first.
0
 
DireOrbAntConnect With a Mentor Commented:
Change your Update to:
UPDATE dbo.tblModule
SET NodeName = 'Module:'+CAST(SCOPE_IDENTITY() AS VARCHAR(10))
WHERE UID = SCOPE_IDENTITY()
0
 
DireOrbAntCommented:
If you are expecting bigint as the ID, cast as VARCHAR(19).
AngelIII's method is much more integrated. Unsure about speed for a computed field though.
0
 
Cyber-DrugsAuthor Commented:
Two good answers, both work, points will be split.

Cheers guys!
0
All Courses

From novice to tech pro — start learning today.