Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding UID of Inserted Record

Posted on 2006-07-13
10
Medium Priority
?
381 Views
Last Modified: 2008-02-01
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!
0
Comment
Question by:Cyber-Drugs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:Ved Prakash Agrawal
ID: 17099959
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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17099971
OK, and what code is used to get the unique value of the Inserted Record?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17100016
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17100034
angelIII

The value will never change, it is done once, and that is it. :)
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 17100220
alter table
  add  ComputedField as 'Material:'+ cast ( UID_FIELD as varchar(400) )

0
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17100271
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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17100277
I only just noticed your post after I made mine AngelIII.

Will try your code out first.
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 1000 total points
ID: 17100638
Change your Update to:
UPDATE dbo.tblModule
SET NodeName = 'Module:'+CAST(SCOPE_IDENTITY() AS VARCHAR(10))
WHERE UID = SCOPE_IDENTITY()
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17100700
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
 
LVL 4

Author Comment

by:Cyber-Drugs
ID: 17106533
Two good answers, both work, points will be split.

Cheers guys!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question