Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Insert into temp table - sp_executesql

Posted on 2012-03-20
3
800 Views
Last Modified: 2012-04-25
I know that the below will not work - I will have no way of knowing how many columns may be in Table1 at any given time - so in theory - was trying to do something like the below???
		DECLARE @sqlX nvarchar(max)
		SET @sqlX = 'SELECT * FROM dbmasterdata.dbo.table1
		
				SELECT  * 
				INTO    #tmpTable 
				FROM    OPENQUERY(dataprod, 'EXEC sp_executesql  @sqlX')

Open in new window

0
Comment
Question by:tbaseflug
3 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 37744154
try

DECLARE @sqlX nvarchar(max)
SET @sqlX = 'SELECT * FROM dbmasterdata.dbo.table1'

SELECT  *
INTO    #tmpTable
FROM    OPENQUERY(dataprod, @sqlX)
0
 
LVL 39

Expert Comment

by:lcohan
ID: 37744654
Why does it need to be so complicated and not just:

SELECT  * INTO  #tmpTable FROM dbmasterdata.dbo.table1
SELECT  * FROM #tmpTable


Are you running against some linked server or something that you need OPENQUERY?
0
 
LVL 12

Accepted Solution

by:
kselvia earned 500 total points
ID: 37745026
If you must use EXEC to get data from a remote server, try;

SELECT * into #helplogins FROM
OPENROWSET('SQLOLEDB', 'Trusted_Connection=yes;Data Source=<yourservername>;',
'SET FMTONLY OFF;EXEC master.dbo.sp_helplogins')
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question