Solved

Finding UID of Inserted Record

Posted on 2006-07-13
10
373 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 250 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 250 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is there a better way to combine two queries 7 27
Download ms sql express. 2 28
SQL Quer 4 22
Help With SQL Query 9 32
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

828 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