Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

build temp table using exec

Posted on 2013-01-03
2
Medium Priority
?
635 Views
Last Modified: 2013-01-07
apparently can't use parameter in an open query so have to use exec query

I am trying to create temp table using the results of the open query but am running into problems

here is what I have so far:

(for testing purposes I am hard coding user value. )




Declare @username varchar(50)
Set @username = 'dryan'

Declare @query varchar(500)



Set @query =

' WITH TempTableTierCreditsMonth1 (
      department, pager
      )
AS (

select department, pager
 from openquery
(AD1,' +  char(39) + 'SELECT  department, pager
FROM '  + char(39) + char(39) + 'LDAP://test.local'  + char(39) + char(39) +
 ' WHERE objectCategory =  '  + char(39) + char(39) + 'Person'  + char(39) + char(39) +
' AND objectClass = '  + char(39) + char(39) + 'user'  + char(39) + char(39) + ' and samaccountname =  '  + char(39) + char(39) +
@username  + char(39) + char(39) + char(39) +  ');'


exec(@query)
0
Comment
Question by:johnnyg123
2 Comments
 
LVL 25

Accepted Solution

by:
TempDBA earned 2000 total points
ID: 38740993
there is a little tweak here. You need to build your openquery thing as a dynamic. See the following link. Had the same issue before and it worked for me:-
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=59179
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38742375
You could also create a global temp table on the local server.  

First, insure the name is absolutely unique -- I use code like:: '##' + replace(cast(newid() AS varchar(60)), '-', '') :: for that.  [The only disadvantage is that forces all code that references the table to be dynamic SQL.]

Create or load that table using OPENQUERY.

Build index(es) on the table as needed.

Do the local queries that need that data.

Drop the global temp table.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

916 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