Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 extract value from select

Author  Topic 

rama108
Posting Yak Master

115 Posts

Posted - 2013-02-26 : 08:24:07
Hello,
I have the following:

INSERT INTO TABLEB VALUES(col1, col2, col3,.....)
SELECT col1, col2, col3,.... FROM TABLEA

I want to insert col1 value into a log table as well so can
I extract col1 into @col1?

Thanks.

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-02-26 : 08:34:44
Means you want to insert col1 of TABLEA to TABLEB table...?

INSERT INTO TABLEB VALUES(col1)
SELECT col1 FROM TABLEA

If TABLEA is existed in another Database, you can use fully qualified name like

INSERT INTO TABLEB VALUES(col1)
SELECT col1 FROM DatabaseName.dbo.TABLEA


--
Chandu
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2013-02-26 : 08:40:50
[code]
INSERT INTO TableB (col1, col2, col3,.....)
OUTPUT inserted.Col1 INTO LogTable(Col1)
SELECT col1, col2, col3,....
FROM TableA;
[/code]
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-02-26 : 14:11:54
What is :
OUTPUT inserted.Col1 INTO LogTable(Col1)

it complains about the word "inserted"

Thanks.
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-02-26 : 14:30:02
I am getting the following when i run the query with output inserted:

A remote table cannot be used as a DML target in a statement which includes an OUTPUT clause or a nested DML statement.
Thanks.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-02-26 : 15:13:36
Then either tableB is on another server, or your query has a derived table in it. Either way, you can't use the OUTPUT operator. If it's a derived table you can make that a temp table and you'll be okay, but if TableB or LogTable is on another server, you're out of luck. You'll just have to do two different INSERT INTO statements.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-02-26 : 15:25:53
Thank you Jim.
Go to Top of Page
   

- Advertisement -