Solved

build temp table using exec

Posted on 2013-01-03
2
583 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 500 total points
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
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.
Viewers will learn how the fundamental information of how to create a table.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now