A common insert statement is this..

INSERT INTO tbl_name (ID) VALUES (1)

What I wanted to achieve is to Insert an ID using another insert statement from another table.. It would look like this

INSERT INTO tbl_name VALUES (INSERT INTO tbl_name2 (ID) VALUES (1))

I have tried it but it's giving me errors..

INSERT INTO tblReport_OPA (ID_Main) VALUES (INSERT INTO tblReport_OPF (ID_Main) VALUES (1))

I'm currently developing under vb.net 2010 and sql express 2005

没有正确的解决方案

其他提示

You probably can use OUTPUT clause, like this:

INSERT INTO tblReport_OPF (ID_Main) 
    OUTPUT Inserted.Id_Main
    INTO tblReport_OPA
SELECT 1 as Id_Main

Note you'll have to use SELECT instead of VALUES

Opyionally a merge can be used.

merge into #a T1
using (select -1 as ID)Q on Q.ID=T1.ID
WHEN NOT matched by target then
insert(id) values(1)
output
inserted.id
INTO #b;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top