Ok so this is the query I have...I just added the ACCOUNTID and the @accountID portion which is obviosly not working
INSERT INTO Leads (
LEADID,
CREATEUSER,
CREATEDATE,
FIRSTNAME,
MODIFYDATE,
ACCOUNTID
)
SELECT
'Q' + cast(floor(999997 * RAND(convert(varbinary, newid()))) as varchar(20))
,'U6UJ9000S'
,CURRENT_TIMESTAMP
,'U6UJ9000S'
,name
,@accountID
FROM Temp
What I am trying to do is do an insert into the account table first and get that id and add the insert id to this insert into the leads table. Is that even possible
SO basically for each record in the Temp table i need to insert a record in the account table with no values just need the account_id so when i insert in the leads table i have the account id to make that insert
set a variable to Scope_identity() (which returns the last id that was created) and use that
Setup:
Query:
Check:
Cleanup:
The problem you will probably end up hitting in practice with Aaron's use of composable DML is that chances are in reality you will have an FK defined to constrain
Leads(AccountId)
to a valid value in which case you will hit the error.To avoid this issue you can use
It is not working because you are inserting 6 values but you are specifying only 5 columns:
These are 5 columns:
Ant these are 6 values:
I don't know where you get the @accountID from, but I imagine you define it somewhere else above.
You can get
@accountID
as follows, after you do the insert to the Account table:And then execute the insert into the Leads table.
UPDATE: EXAMPLE:
You can declare an variable, set it to the desired id and the use the variable in the insert.
With SQL Server 2005 or higher you can use the OUTPUT clause.