好的,所以这是我的查询…我刚刚添加了ACCOUNTID和@accountID部分,这显然不起作用
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
我想做的是先插入帐户表中并获取该ID,然后将插入ID添加到此潜在客户表中。那有可能吗
因此,基本上,对于Temp表中的每个记录,我都需要在account表中插入一条没有值的记录,只需要account_id,所以当我在Leads表中插入时,我具有要进行插入的帐号ID
设置:
USE TempDB; GO CREATE TABLE dbo.Leads ( LeadID VARCHAR(64), CreateUser VARCHAR(32), CreateDate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, FirstName VARCHAR(32), AccountID INT ); CREATE TABLE dbo.Accounts ( AccountID INT IDENTITY(1,1), name VARCHAR(32) /* , ... other columns ... */ ); CREATE TABLE dbo.Temp(name VARCHAR(32)); INSERT dbo.Temp SELECT 'foo' UNION SELECT 'bar';
询问:
INSERT dbo.Accounts ( name ) OUTPUT 'Q' + cast(floor(999997 * RAND(convert(varbinary, newid()))) as varchar(20)), 'U6UJ9000S', CURRENT_TIMESTAMP, inserted.name, inserted.AccountID INTO dbo.Leads SELECT name FROM dbo.Temp;
查看:
SELECT * FROM dbo.Accounts; SELECT * FROM dbo.Leads;
清理:
USE tempdb; GO DROP TABLE dbo.Temp, dbo.Accounts, dbo.Leads;