简体   繁体   中英

SQL Select and Update record during Insert

Trying to SELECT and UPDATE a record inside a SELECT statement.
The Initial SELECT:

INSERT INTO dbo.ATable(VALUE1, VALUE2)
SELECT B.VALUE1, dbo.FUNCTION(B.VALUE1)
FROM dbo.BTable B

The FUNCTION:

SELECT @nextNum = C.VALUE2
FROM dbo.CTABLE C
WHERE C.VALUE1= @VALUE1
RETURN @nextNum

What I am trying to do:

  1. Insert a record into Table1.
  2. Get data from Table2.
  3. Also get a value for VALUE2 from another Table(CTable).

Problem:

I need to increment VALUE2 each time this function executes so multiple calls to that record have a unique value.

I can't have a stored proc from inside the function, I can't have a stored proc inside the SELECT , and I can't call UPDATE from inside the FUNCTION .

Am I out of options? Do I need a different implementation?

INSERT INTO dbo.ATable(VALUE1, VALUE2)
SELECT B.VALUE1, C.VALUE2
FROM dbo.BTable B 
JOIN dbo.CTable C
ON B.VALUE1 = C.VALUE1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM