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)
 INSERT from EXEC stored and other fixed fields

Author  Topic 

Ciupaz
Posting Yak Master

232 Posts

Posted - 2013-06-11 : 10:19:17
Hello all,
I have a table like this:

ID - FKID - Title1 - Title2 - Value - ModifiedDate

Now I have to perform an INSERT in this table
where the fields Title1,Title2,Value come from
a stored procedure calling, and FKID is a
variable in the scope of a stored procedure
where this Insert is performed (ModifiedDate is GETUTCDATE()).

How can I perform this?

Thanks in advance.

Luigi

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-11 : 10:41:20
The first thing that comes to mind - INSERT-EXEC would not work in your case because the columns in the insert part of the statement must exactly match the columns coming out of the EXEC part (the stored proc).

You could try open rowset. There are few examples here http://msdn.microsoft.com/en-us/library/ms190312.aspx

If you have not already, you would need to enable Ad Hoc Distributed queries

sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO


Another option is to insert the data from the stored proc into a temp table with columns that exactly match the stored procedure record set and then move that into your target table appending the additional columns as required.
Go to Top of Page

Ciupaz
Posting Yak Master

232 Posts

Posted - 2013-06-11 : 11:21:33
Maybe the solution with a temporary table could be better.

I'll try.

Lugi
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-06-11 : 12:32:03
yep..that would be recommended approach as otherwise you may have to use distributed query methods

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/11/26/select-columns-from-exec-procedure-name-is-this-possible.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -